androidx.preference.DialogPreference Java Examples

The following examples show how to use androidx.preference.DialogPreference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimePreferenceDialog.java    From openScale with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    timePicker = view.findViewById(R.id.timePicker);
    calendar = Calendar.getInstance();

    Long timeInMillis = null;
    DialogPreference preference = getPreference();

    if (preference instanceof TimePreference) {
        TimePreference timePreference = (TimePreference) preference;
        timeInMillis = timePreference.getTimeInMillis();
    }

    if (timeInMillis != null) {
        calendar.setTimeInMillis(timeInMillis);
        boolean is24hour = DateFormat.is24HourFormat(getContext());

        timePicker.setIs24HourView(is24hour);
        timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
    }
}
 
Example #2
Source File: TimePreferenceDialog.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onDialogClosed(boolean positiveResult) {
    if (positiveResult) {
        int hours;
        int minutes;

        if (Build.VERSION.SDK_INT >= 23) {
            hours = timePicker.getHour();
            minutes = timePicker.getMinute();
        } else {
            hours = timePicker.getCurrentHour();
            minutes = timePicker.getCurrentMinute();
        }

        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);

        long timeInMillis = calendar.getTimeInMillis();

        DialogPreference preference = getPreference();
        if (preference instanceof TimePreference) {
            TimePreference timePreference = ((TimePreference) preference);
            if (timePreference.callChangeListener(timeInMillis)) {
                timePreference.setTimeInMillis(timeInMillis);
                timePreference.setSummary(DateFormat.getTimeFormat(getContext()).format(calendar.getTime()));
            }
        }
    }
}