To provide a widget for selecting a time, use the TimePicker widget, which allows the user to select the hour and minute in a familiar interface.

In this article, I am going to explain how to create a TimePickerDialog, which presents the time picker in a floating dialog box at the press of a button. When the time is set by the user, a TextView will update with the new date.

·         Start a new project named TimePickerDemo.

·         Open the res/layout/main.xml file and insert the following:

<?xml  version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView
       android:id="@+id/timeDisplay" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="@string/hello"/>
<Button
       android:id="@+id/timePicker"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dp"
       android:text="Change the time"/>
</LinearLayout>

Open the activity file and add the following code as shown below:

import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
 
public class TimePickerActivity extends Activity {
   
       private TextView mTimeDisplay;
       private Button mPickTime;
       private int mHour;
       private int mMinute;
      
       static final int TIME_DIALOG_ID=0;
      
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // capture our View elements
        mTimeDisplay = (TextView) findViewById(R.id.timeDisplay);
        mPickTime=(Button)findViewById(R.id.timePicker);
       
        // add a click listener to the button
        mPickTime.setOnClickListener(new View.OnClickListener() {
                    
                     public void onClick(View v) {
                          
                           showDialog(TIME_DIALOG_ID);
                     }
              });
       
        // get the current time
        final Calendar c=Calendar.getInstance();
        mHour=c.get(Calendar.HOUR_OF_DAY);
        mMinute=c.get(Calendar.MINUTE);
       
        // display the current date
        updateDisplay();
    }
   
    // updates the time we display in the TextView
    private void updateDisplay()
    {
       mTimeDisplay.setText(new  
               StringBuilder().append(pad(
               StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)));
    }
 
       private Object pad(int mMinute2) {
             
              if(mMinute2>=10)
                     return String.valueOf(mMinute2);
              else
                     return "0"+String.valueOf(mMinute2);
       }
      
       // the callback received when the user "sets" the time in the dialog
       private TimePickerDialog.OnTimeSetListener mtimeSetListener=new  
                                            TimePickerDialog.OnTimeSetListener() {
                                            TimePickerDialog.OnTimeSetListener() {
             
              public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    
                     mHour=hourOfDay;
                     mMinute=minute;
                     updateDisplay();
              }
       };
      
       @Override
       protected Dialog onCreateDialog(int id)
       {
         switch(id)
         {
           case TIME_DIALOG_ID:
              return new TimePickerDialog(this,mtimeSetListener,mHour,mMinute,false);
         }
         return null;      
       }
}

·         Run the application.
Output will something like below:

Time picker in Android Application

When you click on the button (Change the time), a time picker dialog will open, you can set the time from the time picker dialog.

Time picker in Android Application

After setting the time, a new time will display in the TextView.

Leave Comment