TimSim
TimSim

Reputation: 4036

How to style Android's TimePickerDialog?

I'm creating a TimePickerDialog in code and I want to have custom colors on it. It's coming out all gray and ugly by default and I want it to have specific colors.

I create the TimePickerDialog as follows:

TimePickerDialog time = new TimePickerDialog(
    context, 
    R.style.timePickerDialog,
    new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {}
    }, 10, 0, DateFormat.is24HourFormat(this)
);

But it's coming out in the wrong colors that I don't use anywhere in my app. Should I even have to specify style for this dialog? Why is it coming out grey and dark red?

I can't understand how there isn't a tutorial for this. Is there some other super secret way to pick time on Android?

Upvotes: 0

Views: 2333

Answers (1)

Abhi
Abhi

Reputation: 3511

You can change the style of the TimePicker by adding styles for it. You could do it in two ways, either by specifying the accent colors or by changing the color of each element in the Timepicker.

To change by specifying Accent Colors:

 <item name="colorAccent">yourColor</item>

To change by specifying color of each element:

Set the style using:

 <item name="android:timePickerStyle">@style/MyTimePicker</item>

Then create the style using:

<style name="MyTimePicker" parent="android:Widget.Material.Light.TimePicker">
<item name="android:timePickerMode">clock</item>
<item name="android:headerBackground">yourcolor</item>
<item name="android:numbersTextColor">your_color</item>
<item name="android:numbersInnerTextColor">your_color</item>
<item name="android:numbersSelectorColor">your_color</item>
<item name="android:numbersBackgroundColor">your_color</item>
<item name="android:amPmTextColor">?android:attr/textColorSecondary</item>
</style>

Upvotes: 1

Related Questions