Android: Date picker in android example


Lets see how to code datepicker in android. You should have seen this various applications for example in ticket reservation apps. Its very simple to program this Datepicker dialogbox functionality. You need to have an editText, ImageButton components in activitymain.xml.
When you click on Imagebutton it should open a dialog box with todays date and while clicking on "set" it will set the value in editText component. To get todays date we need to create instance for Calendar class and get year, month and day.
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);

Then we need to set the ImageButton to invoke calendar dialogbox.
ib.setOnClickListener(this);

This will return the DatePickerDialog with todays date.
 return new DatePickerDialog(this, datePickerListener,year, month, day);


Datepicker dialogbox
This is a constructor with some define parameters.
datePickerListener - This is a callback to indicate the date has been set.
Object for this listener is created as follows,
DatePickerDialog.OnDateSetListener datePickerListener =new DatePickerDialog.OnDateSetListener()

Once the date is set this will invoke , "onDateSet" method to perform user define functionality where we have assigned the date value to editText.


activity_main.xml


<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"
    tools:context=".MainActivity"    
     >
     <EditText
         android:id="@+id/editText"
         android:layout_width="250dp"
         android:layout_marginTop="50dp"
         android:layout_marginLeft="5dp"
         android:layout_height="wrap_content"
         android:editable="false" >
</EditText>
     
     <ImageButton
         android:id="@+id/imageButton1"
         android:layout_width="wrap_content"
   android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/editText"
         android:layout_toRightOf="@+id/editText"
         android:contentDescription="@string/selectdate"
         android:cropToPadding="true"
         android:src="@drawable/calendar_icon" />

</RelativeLayout>


MainActivity.java


import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {
 private ImageButton ib;
 private Calendar cal;
 private int day;
 private int month;
 private int year;
 private EditText et;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // mDateButton = (Button) findViewById(R.id.date_button);
  ib = (ImageButton) findViewById(R.id.imageButton1);
  cal = Calendar.getInstance();
  day = cal.get(Calendar.DAY_OF_MONTH);
  month = cal.get(Calendar.MONTH);
  year = cal.get(Calendar.YEAR);
  et = (EditText) findViewById(R.id.editText);
  ib.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  showDialog(0);
 }

 @Override
 @Deprecated
 protected Dialog onCreateDialog(int id) {
  return new DatePickerDialog(this, datePickerListener, year, month, day);
 }

 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
  public void onDateSet(DatePicker view, int selectedYear,
    int selectedMonth, int selectedDay) {
   et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
     + selectedYear);
  }
 };

}

Source code of this application
AndroidDatePicker.rar

Thats it, simple..!! We are done with datepicker in Android.

Have a look at Android Time picker example. Quite a similar one.

 



Reactions:

7 comments :

  1. superb.. ThANX A LOT

    ReplyDelete
  2. Thank u...for giveing me this code which helps to my code

    ReplyDelete
  3. I can't select the date . What can be wrong?

    ReplyDelete
  4. Awesom Article...It works for me...Thanks alot!!!!!!!!!!!!!!

    ReplyDelete
  5. give this in edittext
    android:onclick="dpd";

    public void dpd(View v){
    Calendar c=Calendar.getInstance();
    DatePickerDialog dpd=new DatePickerDialog(this, new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {

    etdob.setText(dayOfMonth+"/"+monthOfYear+"/"+year);

    }
    }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));

    dpd.show();

    }

    ReplyDelete
  6. This tutorial worked very well. I have a question. I would like to ask, how to show two calenders, say one for start date, and other for end date.
    How do I do this ?

    ReplyDelete
  7. This tutorial helped me..Thanks

    ReplyDelete