13

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?

Community
  • 1
  • 1
gcl1
  • 4,070
  • 11
  • 38
  • 55

6 Answers6

10

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>
shubham1g5
  • 407
  • 5
  • 13
5

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> 
Sean
  • 197
  • 2
  • 7
3

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>
Pei
  • 11,452
  • 5
  • 41
  • 45
1

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

mili
  • 3,502
  • 1
  • 29
  • 29
0

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);
Joan Wu
  • 25
  • 3
  • 1
    for android 4.1+,1- "getChildAt(0)" worked for me not "getChildAt(1)" ,2- text size changes back when you spin up or down, this is not helpful at all – john-salib Apr 20 '16 at 11:59
  • 1
    I would strongly suggest to not use this solution. the `getChildAt(1)` as it may brick when the order of the views changes inside the library. may work on a version and be broken with different implementations – carlo.marinangeli Feb 08 '17 at 12:17
-2

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);
Pinki
  • 21,723
  • 16
  • 55
  • 88