Skip to content Skip to sidebar Skip to footer

Change Theme Of Timepickerdialog To Use Apptheme

I have implemented a TimePickerDialog using the following code as given in developer.android.com: public static class TimePickerFragment extends DialogFragment

Solution 1:

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

You need to define Dialog theme in styles.xml.

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

then use it like this:

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

I hope it helps you.

Solution 2:

we can change the theme as shown in the below example.

@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState){
    // Get a Calendar instancefinalCalendarcalendar= Calendar.getInstance();
    // Get the current hour and minuteinthour= calendar.get(Calendar.HOUR_OF_DAY);
    intminute= calendar.get(Calendar.MINUTE);

    /*
        Creates a new time picker dialog with the specified theme.

            TimePickerDialog(Context context, int themeResId,
                TimePickerDialog.OnTimeSetListener listener,
                int hourOfDay, int minute, boolean is24HourView)
     */// TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHTTimePickerDialogtpd=newTimePickerDialog(getActivity(),
            AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_DEVICE_DEFAULT_DARKTimePickerDialogtpd2=newTimePickerDialog(getActivity(),
            AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_HOLO_DARKTimePickerDialogtpd3=newTimePickerDialog(getActivity(),
            AlertDialog.THEME_HOLO_DARK,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_HOLO_LIGHTTimePickerDialogtpd4=newTimePickerDialog(getActivity(),
            AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false);

    // TimePickerDialog Theme : THEME_TRADITIONALTimePickerDialogtpd5=newTimePickerDialog(getActivity(),
            AlertDialog.THEME_TRADITIONAL,this,hour,minute,false);

    // Return the TimePickerDialogreturn tpd;
}

publicvoidonTimeSet(TimePicker view, int hourOfDay, int minute){
    // Do something with the returned timeTextViewtv= (TextView) getActivity().findViewById(R.id.tv);
    tv.setText("HH:MM\n" + hourOfDay + ":" + minute);
}

}

for more detailed example see this tutorial

i hope this will help you.

Solution 3:

You also may override the getTheme method like this:

publicclassTimePickerFragmentextendsDialogFragment {
    
    ...
  
    @OverridepublicintgetTheme() {
        return R.style.MyDialogTheme;
    }

    ...
}

Post a Comment for "Change Theme Of Timepickerdialog To Use Apptheme"