Tgo1014 Tgo1014 - 3 months ago 11
Android Question

Get instance of a Listener in a Fragment

I'm implementing a Fragment that calls two

TimePickerDialog
's as answered here.

But that answer implements it in a activity and then uses a instance of the listener using

mListener = getActivity() instanceof TimePickerDialogListener ? (TimePickerDialogListener) getActivity() : null;


How can I get the Listener instance in my Fragment that implements the
TimePickerDialogListener
?

Answer

To create a time picker

If you simply want to use a time picker you can use TimePickerDialog and provide it with a TimePickerDialogListener

public void showTimePicker(int hour,int minute,boolean is24HourViews) {
        TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(),
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                        //Do your setup
                    }
                }, hour, minute,is24HourView);
        timePickerDialog.show(); //Show the dialog
}
Comments