Android time picker in dialog

When you need a dialog to allow the user to select a time in Android, this tip might be useful to you. In this tip, I will show how to show a time picker dialog when a text box is focused. After a time is selected from the dialog, this time will be displayed in the text box. In Android, you can create a time dialog using TimePickerDialog class. You will create a class that extends the DialogFragment and implements the TimePickerDialog.OnTimeSetListener interface. There are two methods that must be overridden: onCreateDialog, and onTimeSet. In on the onCreateDialog method, you get hour and minute from Calendar class, create an instance of the TimePickerDialog with the parent activity, hour, and minute parameters. In the onTimeState method, you can set the selected time to EditText or TextView to show on your activity.

Now to have a workable example program to demonstrate a time picker dialog you need to create a new Android project. Then modify the activity_main.xml file to add an EditText as shown below. The selected time shows in the EditText.

<RelativeLayout xmlns:android=""
   tools:context=".MainActivity" >

android:hint="Touch here to select a time"



Then create a class called TimeDialog. It extends the DialogFragment and implements the TimePickerDialog.OnTimeSetListener interface.

  package com.example.androidproject;

import java.util.Calendar;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

public class TimeDialog extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private static EditText txt_time;
public static TimeDialog newInstance(View view){
return(new TimeDialog());

public Dialog onCreateDialog(Bundle savedInstanceState) {

// Use the current time as the default time in the dialog
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,false);


public void onTimeSet(TimePicker picker,int hour, int minute){

To use the TimeDialog in the MainActivity activity, in the onStart method of the MainActivity class , you will write code to create an instance of the TimeDialog and display it as shown below:

  package com.example.androidproject;

import android.os.Bundle;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

public class MainActivity extends FragmentActivity{

protected void onCreate(Bundle savedInstanceState) {


public void onStart(){

  EditText txtTime=(EditText)findViewById(;
  txtTime.setOnFocusChangeListener(new OnFocusChangeListener(){
  public void onFocusChange(View view, boolean hasfocus){
   TimeDialog dialog=TimeDialog.newInstance(view);
     FragmentTransaction ft =getSupportFragmentManager().beginTransaction();, "TimeDialog");




Posted by: Dara | post date: 07-27-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.