Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

android - Change TimePicker text color

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...