Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change theme of TimePickerDialog to use AppTheme

Tags:

android

I have implemented a TimePickerDialog using the following code as given in developer.android.com:

public static class TimePickerFragment extends DialogFragment
                        implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
}

}

I get a dialog like this one:

enter image description here

I want to change the theme of the TimePickerDialog to go with the AppTheme which extends an AppCompat theme. When i do use the other constructor as told by some, the theme works, but the Dialog goes fullscreen. The code i used for that was:

// Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), R.style.AppTheme, this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));

I get this:

enter image description here

How do i go about this? - I want the Dialog to remain the same, but change only the accent colors. Thanks!

like image 826
Sriniketh Avatar asked Jan 11 '16 03:01

Sriniketh


2 Answers

You have to use Theme.AppCompat.Light.Dialog as Parent Theme

You need to define Dialog theme in styles.xml.

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">@color/purple</item>
        <item name="colorControlNormal">@color/orange</item>
 </style>

then use it like this:

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), R.style.DialogTheme, this, hour, minute,
DateFormat.is24HourFormat(getActivity()));

I hope it helps you.

like image 110
Rajesh Avatar answered Sep 21 '22 14:09

Rajesh


You also may override the getTheme method like this:

public class TimePickerFragment extends DialogFragment {
    
    ...
  
    @Override
    public int getTheme() {
        return R.style.MyDialogTheme;
    }

    ...
}
like image 21
MeLean Avatar answered Sep 18 '22 14:09

MeLean