title: Android: TimePickerDialog - TimePickerDialog.OnTimeSetListener
body: <p>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:</p>
  
  <p><strong>AddPeriod.java</strong></p>
  
  <pre><code>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);
                  }
              };
  }
  </code></pre>
  
  <p><strong>add_period.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;Button 
          android:id="@+id/btn"
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"
          android:text="Escoge hora" 
          android:onClick="showTimePickerDialog" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>TimePickerFragment.java</strong></p>
  
  <pre><code>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
  
      }
  }
  </code></pre>
  
  <p>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.</p>