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="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

  <EditText
android:id="@+id/txt_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Touch here to select a time"

/>
  
  

</RelativeLayout>


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.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import android.app.Dialog;
import android.app.TimePickerDialog;;

public class TimeDialog extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
private static EditText txt_time;
public static TimeDialog newInstance(View view){
txt_time=(EditText)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){
txt_time.setText(hour+":"+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.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void onStart(){
  super.onStart();

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

     }
}

});
}
}


Android-time-picker-in-dialog

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.