Search code examples
androiddialogdatepickertimepickersettext

SetText and TimePickerDialog


I cannot seem to figure out what to place into the onTimeSet() method in order to setText to the button that it is associated with.

I would like the user to click the button in the layout and it show the TimePickerDialog. Once the user has selected the time they would like to use I would like the time to be set to the same buttons text.

Any ideas?

public 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


}

}

Solution

  • Yeah, it's a bit complicated passing data back from a Fragment to the containing Activity.

    Basically you have two options, one of which is simple and straigt forward, the other one is more sophisticated but on the long run (and for more complex requirements) would be the better one:

    Simple

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String timeString = hourOfDay+" "+minute; // <-- just for example, you'll want to do better time-formatting here.
        Button b = (Button)getActivity().findViewById(R.id.yourButtonsId);
        b.setText(timeString);
    }
    

    Sophisticated

    Use a Listener interface in your Fragment and make your Activity implement that interface and set itself as the Listener to your Fragment:

    Add this to your Fragment:

    public interface Listener {
        public void setTime(int hourOfDay, int minute);
    }
    
    // hold the listener
    private Listener mListener;
    
    // used by Activity to set itself as the listener for the fragment
    public void setListener(Listener listener) {
        mListener = listener;
    }
    

    Implement onTimeSet in your Fragment like so:

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        if (mListener != null) mListener.setTime(hourOfDay, minute);
    }
    

    Add implements TimePickerFragment.Listener to your Activity's class declaration.

    Implement the public void setTime(int hourOfDay, int minute) method of the Listener in your Activity, e.g. like so:

    public void setTime(int hourOfDay, int minute) {
        String timeString = hourOfDay+" "+minute; // <-- just for example, you'll want to do better time-formatting here.
        Button b = (Button)findViewById(R.id.yourButtonsId);
        b.setText(timeString);
    }
    

    Add this to your Activity after instantiating your Fragment:

    timePickerFragment.setListener(this);