Android date picker dialog

When you work with an interface that allows the user to enter date into a text box, it is a good idea to have a date dialog from which she/he can pick up a date easily. In this tip, I will show how to show a date dialog when a text box is focused. After a date is selected from the dialog, this date will be displayed in the text box.


Now, you need to create a new Android project. Then modify the activity_main.xml file to add an EditText as shown below. The EditText is a text box to show the selected date.

<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/txtdate"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="20sp"
     android:hint="Touch here to select a date"
       />
</RelativeLayout>




To have a date picker dialog, you need to create a class that extends the DialogFragment and implements the DatePickerDialog.OnDateSetListener interface. There are two methods of the DatePickerDialog.OnDateSetListener that you need to override. They are onCreateDialog and onDateSet. In the onCreateDialog, you specify a default date to display when the dialog opens. Typically, it is the current date. In the onDateSet, you place the selected date in the text box. You can pass the reference to the text box through the constructor of the class as shown in the example code below.  

package com.example.andtip;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;


@SuppressLint("ValidFragment")
public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {
   EditText txtdate;
   public DateDialog(View view){
   txtdate=(EditText)view;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {

// Use the current date as the default date in the dialog
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);

}

public void onDateSet(DatePicker view, int year, int month, int day) {
   //show to the selected date in the text box
   String date=day+"-"+(month+1)+"-"+year;
   txtdate.setText(date);
}



}


Now you have a date picker dialog. To display the dialog to the user, in the MainActivity class adds onStart method. In the onStart method, you will reference to the EditText and add the OnFocusChangeListener to it. The code to show the date picker dialog is in the onFocusChange method.

public void onStart(){
   super.onStart();
  
   EditText txtDate=(EditText)findViewById(R.id.txtdate);
   txtDate.setOnFocusChangeListener(new OnFocusChangeListener(){
   public void onFocusChange(View view, boolean hasfocus){
      if(hasfocus){
        DateDialog dialog=new DateDialog(view);
        FragmentTransaction ft =getFragmentManager().beginTransaction();
        dialog.show(ft, "DatePicker");

}
}

});
}

Now run the project. You will see a date picker dialog as shown below.

Android-date-picker-dialog

Posted by: Dara | post date: 05-17-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.