How can I override TimePicker to change text color
Asked Answered
D

2

0

My view has a white background, and it has to stay that way. I have a TimePicker on that white background.Everything is woking fine with android 2.3.3 but android 4.0.3 has a new timePicker style. The numbers have a very bright color. It is very hard to see them on the white background, and I didn't find a direct way to change the textColor. I don't want to change the background because it would look not so good.

Is there any way to override this and set the color of the numbers to black?

Sincerly, Wolfen

Demean answered 10/6, 2012 at 14:30 Comment(0)
H
0

Take a look at the android source for styles.xml

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

You then set a style on your activity/timepicker it looks like you could do something like this:

<style name="MyTimePicker" parent="@android:style/Widget.TimePicker">
    <item name="android:textColor">#000000</item>
</style>

or maybe (3.0 and above only)

<style name="MyHoloTimePicker" parent="@android:style/Widget.Holo.TimePicker">
    <item name="android:textColor">#000000</item>
</style>

Then your xml would be:

<TimePicker
 style="@style/MyHoloTimePicker"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />
Horripilation answered 10/6, 2012 at 14:41 Comment(6)
@Demean yes, why would you think you couldn't?Horripilation
Okay I added this to my styles.xml:<resources> <!-- Global Theme Styles --> <eat-comment /> <style name="Widget.TimePicker"> <item name="android:layout">@android:layout/time_picker</item> </style> <style name="Widget.Holo.TimePicker" parent="Widget.TimePicker"> <item name="android:layout">@android:layout/time_picker_holo</item> </style> <style name="Widget.Holo.Light.TimePicker" parent="Widget.Holo.TimePicker"> </style> </resources> error: Error retrieving parent for item: No resource found that matches the given name 'Widget'Demean
@Demean all you've done is copy and paste out of the source, thats not what I said do. My answer shows you inherit from the source and override what you needHorripilation
inherit what? You should post actual working cod that should work. i tried your code snippet myself and tried to locate the timePicker class on styles/Widget but there is no resource for it.Treadle
it is there android.googlesource.com/platform/frameworks/base/+/refs/heads/…Horripilation
this answer is distracting ... -1Nosing
O
0

use this function

public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
final int count = numberPicker.getChildCount();
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;

}

Obtrusive answered 9/2, 2015 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.