Home > DeveloperSection > Articles > Time picker in Android Application

Time picker in Android Application


Android Android 
Ratings:
4 Comment(s)
 8062  View(s)
Rate this:

Time picker in Android Application

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(
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() {

             

              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.


Retrieving data from SQLite Database using DatePicker and TimePicker

By Ngang Sir Tat on   4 years ago
Hi, i was wondering how do you retrieve and display information from SQLite Database using TimePicker and DatePicker in Android? i've been trying for weeks and have been searching through the internet but i'm still stuck as it is. Any suggestions?

Retrieving data from SQLite Database

By Rohit Kesharwani on   4 years ago
Hi,

Here in TimePicker and DatePicker, I am not using SQLite Database.
If you want to retrieve and display information from SQLite Database, check this links:

http://www.mindstick.com/Articles/af5c031a-e435-4642-8464-9f7d375087c2/?SQLite%20in%20Android
http://www.mindstick.com/Articles/2643e627-4f5c-45cc-b166-9a9b4c9ff3b1/?Select%20query%20in%20Android%20Application

Thanks.

Retrieving data from SQLite Database using DatePicker and TimePicker

By Ngang Sir Tat on   4 years ago
hey, thx but i've read those articles and i've tried using those but i cant seem to retrieve and display information from my database using DatePicker and TimePicker. Was hoping you could help cause i've tried all sorts of methods and have come to a dead end. :/

Retrieving data from SQLite Database

By Rohit Kesharwani on   4 years ago
Hi,

If you are facing problem in retrieve and display information from SQLite database, you can paste your code here.

Thanks.

Don't want to miss updates? Please click the below button!

Follow MindStick