简体   繁体   中英

How to change textsize in datepicker?

The default textsize in datepicker is too big for my app. I've seen a way suggested to change it here , but fishing around for the textviews nested inside the datepicker class seems clunky and error prone.

Is there a better/cleaner way to do it?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM