How to change textsize in datepicker?

陌路散爱 提交于 2019-11-29 06:13:24

The easiest way to change the font size of datepicker/timepicker/numberpicker is customizing the theme.

In styles file, set the default font size in the following way.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textSize">22sp</item>
</style> 

Setting a theme to DatePicker layout and adding android:textSize to it works for me.

In your layout's xml add a DatePicker applying a theme as shown below -

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
            android:theme="@style/NumberPickerStyle"
            android:datePickerMode="spinner"
            android:calendarViewShown="false"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>

and then define the NumberPickerStyle in styles.xml specifying android:textSize like this -

<style name="NumberPickerStyle">
        <item name="android:textSize">@dimen/number_picker_text_size</item>
</style>

use below code:

ViewGroup childpicker = (ViewGroup)datePicker.findViewById(Resources.getSystem().getIdentifier("month", "id", "android"));

EditText mornthEt = (EditText)childpicker.getChildAt(1);// month widget

//change textsize and textcolor for mornthEt

mornthEt.setTextSize(30);

mornthEt.setTextColor(Color.GREEN);

Sean's approach made some glitches in the UI of picker itself in case there're a number of pickers and I think it's not perfect to apply text size to all the components under the picker. Below is what I've used and it works perfect without any UI glitches.

<style name="PickerTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:editTextStyle">@style/PickerEditText</item>
</style>

<style name="PickerEditText" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:textSize">24sp</item>
</style>

use below code

 DatePicker picker = (DatePicker)findViewById(R.id.dp_date);
    ViewGroup childpicker

     = (ViewGroup)
    findViewById(Resources.getSystem().getIdentifier("month" /*rest is:
    day, year*/, "id", "android"));

    EditText textview = (EditText)
    picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input",
    "id", "android"));
    textview.setTextSize(30);
    textview.setTextColor(Color.GREEN);

Sean's code works. but it is changing font size for all the other components also(textViews, buttons, etc...) So Here is the solution.

Create different style for time picker and use it on time picker theme.

<style name="my_time_picker_style">
    <item name="android:textSize">24sp</item>
</style>

and use it on your timepicker

android:theme="@style/my_time_picker_style"

Look at video, how I did https://youtu.be/JMJ2ujhk9c0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!