0

I am opening timepicker in dialogfragment in my fragment class.

var dialog = new TimePickerDialogFragment(_activity, DateTime.Now, new OnTimeSetListener());

                    dialog.Cancelable = false;
                    dialog.Show(_fg, null);

Here TimePickerDialogFragment extends DialogFragment.

public class TimePickerDialogFragment : DialogFragment    
{        
private Activity _activity;

private DateTime _date;


private readonly TimePickerDialog.IOnTimeSetListener _listener;

        public TimePickerDialogFragment(Activity activity, DateTime date, TimePickerDialog.IOnTimeSetListener listener)
        {
            _activity = activity;
            _date = date;
            _listener = listener;
        }

        public override Dialog OnCreateDialog(Bundle savedState)
    {
        var dialog = new TimePickerDialog(_activity, _listener, _date.Hour, _date.Minute, true);
        dialog.SetTitle("Enter Time"+ DateTime.Now.ToString());
        return dialog;
    }
    }

Here is my listener class

public class OnTimeSetListener : Java.Lang.Object, TimePickerDialog.IOnTimeSetListener 

    {
      public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
      {

      }
    }

Now I want to update the title as soon as user selects a new time while the pop up is opened. The OnTimeSet is called when I click on set button on popup.I don't want to update there. How can I achieve this? Is there any other way to open timepicker dialog apart from this that I can try? I am new to android. Any help is appreciated.

sushildlh
  • 8,986
  • 4
  • 33
  • 77

1 Answers1

0

In order to get notified when time is changed you need to create a subclass of TimePickerDialog and override OnTimeChanged method which is called when user changes time in the picker. Add a TimeChanged event in the new subclass and trigger it from the overridden method. In the event handler you will be able to set the dialog title based on the new time.

Giorgi
  • 30,270
  • 13
  • 89
  • 125
  • Thank you for replying. Can you tell me where to use subclass of TimePickerDialog in my code? I understood now that overriding OnTimeChanged method will solve my purpose but I can't still figure out where to pass the reference for the subclass created. – user6186610 May 02 '16 at 09:01
  • dialog will no longer be an instance of `TimePickerDialog`, but a `CustomTimePickerDialog` (your subclass) object. For instance: `var dialog = new CustomTimePickerDialog(_activity, _listener, _date.Hour, _date.Minute, true);` – Luis Beltran May 02 '16 at 09:10
  • @user6186610: You will use it in OnCreateDialog instead of TimePickerDialog – Giorgi May 02 '16 at 09:23
  • Thank you so much. That did it. – user6186610 May 02 '16 at 12:13