Change the TimePickerDialog theme to use AppTheme

I implemented TimePickerDialogusing the following code provided at 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:

enter image description here

I want to change the theme TimePickerDialogto go with AppTheme, which extends the theme AppCompat. When I use another constructor, as some have said, the theme works, but Dialoggoes into full-screen mode. The code I used for this 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 can I do it? “I want to Dialogstay the same, but change only the colors of the accent.” Thanks!

+4
source share
2 answers

You should use Theme.AppCompat.Light.DialogasParent Theme

Dialog styles.xml.

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

:

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

, .

+13

, .

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    // Get a Calendar instance
    final Calendar calendar = Calendar.getInstance();
    // Get the current hour and minute
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = 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_LIGHT
    TimePickerDialog tpd = new TimePickerDialog(getActivity(),
            AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);

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

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

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

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

    // Return the TimePickerDialog
    return tpd;
}

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

}

.

, .

0

Source: https://habr.com/ru/post/1623697/


All Articles