简体   繁体   中英

How to Change Time Picker Font Color in Android?

Hi I want to change the Font color of my Time Picker. I searched some other answers in Stackoverflow itself. But I cant able to help myself on this.

My targeted API is 19. Here is the list of things I tried.

  1. I tried adding styles in Styles.xml in values-v11 and values-v14 folders. Following is the code

<!-- language: lang-xml --> <style name="MyTimePicker" parent="@android:style/Widget.Holo.DatePicker"> <item name="android:textColor">@color/main_font</item> </style>
I tried adding it in values folder also. But i Get the following error.

@android:style/Widget.Holo.DatePicker requires API level 11 (current min is 8)

I added styles to my TimePicker like

<TimePicker
        android:id="@+id/alarm_timePicker"
        style="@style/MyTimePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

Nothing Changed.

2 . I tried adding some code from Stackoverflow within my activity. Following is the code.

public static boolean setNumberPickerTextColor(TimePicker numberPicker) {
    final int count = numberPicker.getChildCount();
    int color = R.color.main_font;
    for (int i = 0; i < count; i++) {
      View child = numberPicker.getChildAt(i);
      if (child instanceof EditText) {
        try {
          Field selectorWheelPaintField =
              numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");
          selectorWheelPaintField.setAccessible(true);
          ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
          ((EditText) child).setTextColor(color);
          numberPicker.invalidate();
          return true;
        } catch (NoSuchFieldException e) {
          //Log.w("setNumberPickerTextColor", e);
        } catch (IllegalAccessException e) {
          //Log.w("setNumberPickerTextColor", e);
        } catch (IllegalArgumentException e) {
          //Log.w("setNumberPickerTextColor", e);
        }
      }
    }
    return false;
    }

When I tried debugging it I got the error while setting the content view as

Attempt to invoke virtual method 'void android.widget.NumberPicker.setOnValueChangedListener(android.widget.NumberPicker$OnValueChangeListener)' on a null object reference

I made the same mistake. I suppose you found the answer yourself, but for others : Try to change your parent style from @android:style/Widget.Holo.DatePicker to @android:style/Widget.Holo.TimePicker .

Same trick for @android:style/Widget.Material.Light.DatePicker , change it for @android:style/Widget.Material.Light.TimePicker

The text colors are the textColorPrimary and textColorSecondary values of your theme. So you can create style like this one:

<style name="myTheme" parent="Theme.AppCompat.NoActionBar">

    <item name="android:textColorPrimary">#e90d8a</item>
    <item name="android:textColorSecondary">@color/white_primary_text</item>

   // The rest of your customization.

</style>

And on your activity use the setTheme method.

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