user1640506
user1640506

Reputation: 21

How can i change the textcolor of my timepicker and datepicker?

Currently I am working on one of my first applications. In this application I have a TimePicker and a DatePicker. My current Activity has a dark background. Now I want a white textcolor in my TimePicker/DatePicker.

In my layout I have defined my pickers:

<DatePicker android:id="@+id/dpDateOfValue" android:calendarViewShown="false" />
<TimePicker android:id="@+id/tpTimeOfValue" />

The solution should work on 2.3 - 4.1

Upvotes: 2

Views: 7699

Answers (3)

DeniSHow
DeniSHow

Reputation: 1414

For DatePicker I use this code:

public static void setDatePickerTextColor(DatePicker dp, int color) {
    LinearLayout l = (LinearLayout) dp.getChildAt(0);
    if (l != null) {
        l = (LinearLayout) l.getChildAt(0);
        if (l != null) {
            for (int i = 0; i < 3; i++) {
                NumberPicker np = (NumberPicker) l.getChildAt(i);
                if (np != null) {
                    setNumberPickerTextColor(np, color);
                }
            }
        }
    }
}

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("NumberPickerTextColor", e);
            } catch (IllegalAccessException e) {
                Log.w("NumberPickerTextColor", e);
            } catch (IllegalArgumentException e) {
                Log.w("NumberPickerTextColor", e);
            }
        }
    }
    return false;
}

Although, it was tested only on Lollipop.

Upvotes: 1

i.shadrin
i.shadrin

Reputation: 5057

Use:

<style name="MyHolo" parent="android:Theme.Holo.NoActionBar">

        ...

        <item name="android:editTextColor">#000000</item>
</style>

to set TimePicker text color for API >= 11

Upvotes: 1

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

I think you can do this using Coding, try following:

DatePicker your_picker = (DatePicker) findViewById(R.id.dpDateOfValue);
EditText edittext = (EditText) your_picker.findViewById(Resources.getSystem().getIdentifier("datepicker_input", "id",  "android"));

edittext.setTextColor(Color.BLUE);

Upvotes: -1

Related Questions