Home » Android » android – Change TimePicker text color

android – Change TimePicker text color

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’ve been trying to change the textcolor of my timepicker. But I can’t find where the parent style is located. I’ve tried both

<style name="MyTimePicker" parent="@android:style/Widget.Holo.TimePicker">
    <item name="android:textColor">@color/text</item>
</style>

and

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

My minSdkVersion is 15. My targetSdkVersion is 20. I have rebuilded and cleaned my project.
I think I’ve been through every similar question on SO and none of them really have provided a solution for me. The only answer that might work is using some sort of library, but I’m not a big fan of that solution. Is the path to the parent something different from what I’m using, because I’m pretty sure I should be able to access it somehow?

Edit
This is how the theme is applied;

<TimePicker
    style="@style/MyTimePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

On a note this is the error I receive (forgot to place it before):

Error:Error retrieving parent for item: No resource found that matches the given name ‘@android:style/Widget.Holo.TimePicker’.

Edit 2
A couple of the questions I’ve viewed to try to solve this:

  1. How can I override TimePicker to change text color – I think this question gets as close to an answer, but I’m not entirely sure what I need to do? Do I need to import the android TimePicker style into my project?
  2. https://stackoverflow.com/questions/24973586/set-textcolor-for-timepicker-in-customized-app-theme – No answer is given.
  3. How can i change the textcolor of my timepicker and datepicker? – Tried the 0 votes answer, but it didn’t work.
  4. How to change the default color of DatePicker and TimePicker dialog in Android? – Again can’t find the TimePicker in a similar way.
  5. Android – How do I change the textColor in a TimePicker? – Again, can’t find the actual TimePicker parent.

These are probably the best questions/answers to my problem, but none of them help me. It would be nice to get a definitive answer on this.

How to&Answers:

I have combined Paul Burke’s Answer and Simon’s Answer to succesfully edit the text colour of the TimePicker.

Here’s how it is accomplished:

TimePicker time_picker; //Instantiated in onCreate()
Resources system;

private void set_timepicker_text_colour(){
    system = Resources.getSystem();
    int hour_numberpicker_id = system.getIdentifier("hour", "id", "android");
    int minute_numberpicker_id = system.getIdentifier("minute", "id", "android");
    int ampm_numberpicker_id = system.getIdentifier("amPm", "id", "android");

    NumberPicker hour_numberpicker = (NumberPicker) time_picker.findViewById(hour_numberpicker_id);
    NumberPicker minute_numberpicker = (NumberPicker) time_picker.findViewById(minute_numberpicker_id);
    NumberPicker ampm_numberpicker = (NumberPicker) time_picker.findViewById(ampm_numberpicker_id);

    set_numberpicker_text_colour(hour_numberpicker);
    set_numberpicker_text_colour(minute_numberpicker);
    set_numberpicker_text_colour(ampm_numberpicker);
}

private void set_numberpicker_text_colour(NumberPicker number_picker){
    final int count = number_picker.getChildCount();
    final int color = getResources().getColor(R.color.text);

    for(int i = 0; i < count; i++){
        View child = number_picker.getChildAt(i);

        try{
            Field wheelpaint_field = number_picker.getClass().getDeclaredField("mSelectorWheelPaint");
            wheelpaint_field.setAccessible(true);

            ((Paint)wheelpaint_field.get(number_picker)).setColor(color);
            ((EditText)child).setTextColor(color);
            number_picker.invalidate();
        }
        catch(NoSuchFieldException e){
            Log.w("setNumberPickerTextColor", e);
        }
        catch(IllegalAccessException e){
            Log.w("setNumberPickerTextColor", e);
        }
        catch(IllegalArgumentException e){
            Log.w("setNumberPickerTextColor", e);
        }
    }
}

Please note that this answer might be outdated by now. I ran into this a while ago with something that might have been buggy (see my question for more details). Otherwise you should probably follow Vikram’s answer.

Answer:

Not sure why you would need to dive into Java Reflection API for this. Its a simple styling matter. The attribute that you need to override is: textColorPrimary.

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    ....
    <item name="android:textColorPrimary">#ff0000</item>
</style>

If you’re using the TimePicker inside a Dialog, override android:textColorPrimary in the dialog’s theme.

That’s about it.

Answer:

A TimePicker is really just two NumberPickers. Looking into the Widget.NumberPicker style and layout, you’ll find the it uses

@style/TextAppearance.Large.Inverse.NumberPickerInputText

Unfortunately, TextAppearance.Large.Inverse.NumberPickerInputText doesn’t use one of the attributes that you can set in your theme. So you have two options:

  1. Copy the necessary classes to make your own version of NumberPicker and TimePicker. (You might be able to extract something from libraries like HoloEverywhere)

  2. Use hacks.

If you want to go the second route, you can do this:

private int mNumberPickerInputId = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources system = Resources.getSystem();

    // This is the internal id of the EditText used in NumberPicker (hack)
    mNumberPickerInputId = 
        system.getIdentifier("numberpicker_input", "id", "android");

    // just used for full example, use your TimePicker
    TimePicker timePicker = new TimePicker(this);
    setContentView(timePicker);

    final int hourSpinnerId = 
        system.getIdentifier("hour", "id", "android");
    View hourSpinner = timePicker.findViewById(hourSpinnerId);
    if (hourSpinner != null) {
        setNumberPickerTextColor(hourSpinner, Color.BLUE);
    }

    final int minSpinnerId = 
        system.getIdentifier("minute", "id", "android");
    View minSpinner = timePicker.findViewById(minSpinnerId);
    if (minSpinner != null) {
        setNumberPickerTextColor(minSpinner, Color.BLUE);
    }

    final int amPmSpinnerId = 
        system.getIdentifier("amPm", "id", "android");
    View amPmSpinner = timePicker.findViewById(amPmSpinnerId);
    if (amPmSpinner != null) {
        setNumberPickerTextColor(amPmSpinner, Color.BLUE);
    }
}

private void setNumberPickerTextColor(View spinner, int color) {
    TextView input = (TextView) spinner.findViewById(mNumberPickerInputId);
    input.setTextColor(color);
}

EDIT

Upon further investigation, this hack doesn’t really work well. It won’t allow you to change the color of the NumberPicker above/below values. The color also resets after the use interacts with it. It seems that your only option will be to create your own copies of the necessary classes (option #1 above).

Answer:

@Vikram is right
This is so simple.You can try this way

<style name="abirStyle" parent="@android:style/Theme.Holo.Light.Dialog.NoActionBar">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:background">@null</item>
    <item name="android:textColorPrimary">@color/colorPrimary</item>
</style>

Answer:

You can try to set android:textColorPrimaryInverse and android:textColorSecondaryInverse. That should do the job without using Reflection.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textColorPrimaryInverse">@android:color/black</item>
    <item name="android:textColorSecondaryInverse">@color/colorLightGrey</item>
</style>

Answer:

Adding to Vikram answer , Set the theme to timePicker do not set it as style

in styles.xml

 <style name="timePickerOrange">
    <item name="android:textColorPrimary">@color/orange</item>
    <item name="android:textColor">@color/orange</item>
</style>

and for timePicker

<TimePicker
    android:theme="@style/timePickerOrange"
    android:id="@+id/timePicker_defaultTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="@font/hero_regular"
    android:timePickerMode="spinner"
    app:fontFamily="@font/hero_regular" />