Android: TimePickerDialog - TimePickerDialog.OnTimeSetListener

10,910

In AddPeriod.java you create a TimePickerDialog.OnTimeSetListener but as far as I can see that listener isn't used.

In TimePickerFragment.java you implement the TimePickerDialog.OnTimeSetListener interface and pass the fragment instance itself as a listener to the TimePickerDialog but you do nothing in the implemented onTimeSet() method in TimePickerFragment.java. Are you saying that onTimeSet() in TimePickerFragment.java isn't called? Have you set a breakpoint?

If you manage to get the onTimeSet() called in your fragment I assume what you're really asking for is how to pass this time back to the activity. If I'm correct I'd say that this is a perfect place to use an event bus, for instance using Otto.

UPDATE: Based on your comments to my questions I assume that what you really want is a way to get the picked time in TimePickerFragment back to AddPeriod. The better approach is in my opinion to use an event bus (as mentioned above). This will decouple your fragment from your activity. BUT to give you an answer that works with minimal changes to your code I suggest that you pass a listener to the fragment and use that listener when you create your dialog:

TimePickerFragment.java:

public class TimePickerFragment extends DialogFragment {

    private TimePickerDialog.OnTimeSetListener listener;

    public TimePickerFragment(TimePickerDialog.OnTimeSetListener listener) {
        super();
        this.listener = listener;
    }

    @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(), listener, hour, minute,
        DateFormat.is24HourFormat(getActivity()));
    }
}

As you can see I've added a constructor that takes a TimePickerDialog.OnTimeSetListener argument. This listener is used when the TimePickerDialog is created. Now in our AddPeriod activity we pass the mTimeSetListener to the fragment when it is created:

public class AddPeriod extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_period); 
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment(mTimeSetListener);
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }

    TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(android.widget.TimePicker view,
                        int hourOfDay, int minute) {
                    Log.i("",""+hourOfDay+":"+minute);
                }
            };
}
Share:
10,910
javifm
Author by

javifm

Updated on June 27, 2022

Comments

  • javifm
    javifm almost 2 years

    I am a newbie to Android. Here I am again with another "simple" question. I hope you can help me. I just want a button that displays a TimePicker in a dialog. When I set the time in the timepicker, I would like to get the selected time. I am using the Android support library and ActionbarSherlock. Here is my code:

    AddPeriod.java

    import android.app.AlertDialog;
    import android.app.TimePickerDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ListView;
    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.app.SherlockFragmentActivity;
    import com.actionbarsherlock.view.MenuItem;
    
    public class AddPeriod extends SherlockFragmentActivity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_period); 
        }
    
        public void showTimePickerDialog(View v) {
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getSupportFragmentManager(), "timePicker");
        }
    
        TimePickerDialog.OnTimeSetListener mTimeSetListener =
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(android.widget.TimePicker view,
                            int hourOfDay, int minute) {
                        Log.i("",""+hourOfDay+":"+minute);
                    }
                };
    }
    

    add_period.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button 
            android:id="@+id/btn"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="Escoge hora" 
            android:onClick="showTimePickerDialog" />
    
    </LinearLayout>
    

    TimePickerFragment.java

    import android.text.format.DateFormat;
    import java.util.Calendar;
    
    import android.app.Dialog;
    import android.support.v4.app.DialogFragment;
    
    import android.app.TimePickerDialog;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.TimePicker;
    
    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
    
        }
    }
    

    But the listener is not being called. Any suggestions? Perhaps this has been answered but after 30 minutes of searching, I didn't find the answer.Thanks a lot for helping me.

  • javifm
    javifm almost 11 years
    I don´t know how to make the call. Sorry for my expression. I don´t know hot to implement what I want. I have seen other examples but it was not whith fragments.
  • britzl
    britzl almost 11 years
    COuld you please confirm if you get a call to onTimeSet() in your fragment (ie where the comment "// Do something with the time chosen by the user" is)? Set a breakpoint, run your code, pick a time and see if the breakpoint triggers. It should. Once you have confirmed this I guess it's only a matter of passing the picked time back to the activity, because that is what you want right?
  • javifm
    javifm almost 11 years
    Yes! I think the problem is in this line: return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); There this is related to the default listener...