Displaying a TimePicker Dialog from a PreferenceFragment Operation

Hello!

I want my application to display the TimePicker dialog when the user clicks on the settings item (Preference). I already created a DialogFragment that creates a TimePicker dialog box, but I cannot associate this fragment with a user preference. How can I do that?

Here's the DialogFragment class that should be associated with user preference:

import java.util.Calendar; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.app.DialogFragment; import android.text.format.DateFormat; import android.widget.TimePicker; public class MyDialogFragment 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 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, 0); ((OpeningScreen) getActivity()).timerAlert(calendar.getTimeInMillis()); } } 

PS. I read the Android developer tutorial ( http://developer.android.com/guide/topics/ui/settings.html ), but this is a bit confusing to me. Thank you so much for your help!
+4
source share

All Articles