Android DatePicker Dialog

Android DatePicker:

In this session will learn to use DatePicker.

Create new project in eclipse (Android-First-Programm).

In res -> layout open “activity_main.xml” and use button.

activity_main.xml
<LinearLayout 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" >
      <Button
        android:id="@+id/btnDatePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  </LinearLayout>

In MainActivity.java get button reference and using Calendar class assign current date to Button.

MainActivity.java

[sociallocker]

package com.example.androiddatepicker;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends Activity {
 Button mBtnDatePicker;
 int mYear;
 int mMonth;
 int mDay;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mBtnDatePicker=(Button) findViewById(R.id.btnDatePicker);
  Calendar calendar=Calendar.getInstance();//Constructs a new instance of the Calendar

  mDay=calendar.get(Calendar.DAY_OF_MONTH);
  mMonth=calendar.get(Calendar.MONTH)+1; //Month starts from 0
  mYear=calendar.get(Calendar.YEAR);
    String currentDate=mDay+"/"
    +mMonth+"/"
    +mYear;  
   mBtnDatePicker.setText(currentDate);
 }       
}

Now Button click open DatePicker dialog and set that date to Button, So complete MainActivity.java will looks like this.

MainActivity.java

package com.example.androiddatepicker;
 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.widget.Button;
 public class MainActivity extends Activity {
 static final int DATE_PICKER_ID=1;
 Button mBtnDatePicker;
 int mYear;
 int mMonth;
 int mDay;
   @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mBtnDatePicker=(Button) findViewById(R.id.btnDatePicker);
  Calendar calendar=Calendar.getInstance(); //Constructs a new instance of the Calendar
  mDay=calendar.get(Calendar.DAY_OF_MONTH);
  mMonth=calendar.get(Calendar.MONTH+1);//Month starts from 0
  mYear=calendar.get(Calendar.YEAR);
    String currentDate=mDay+"/"
    +mMonth+"/"
    +mYear;
   mBtnDatePicker.setText(currentDate);
   mBtnDatePicker.setOnClickListener(new View.OnClickListener() {
  @Override
   public void onClick(View v) {
    showDialog(DATE_PICKER_ID);
   }
  });
 }
   @Override
 protected Dialog onCreateDialog(int dialogId) {
  switch(dialogId){ 
  case DATE_PICKER_ID:  
   return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
  default:   return null; 
  }
 }
 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener(){
  @Override

   public void onDateSet(android.widget.DatePicker arg0, int arg1,    int arg2, int arg3) {  
   String date=String.valueOf(arg3) + "/"  + String.valueOf(arg2+1) + "/"  + String.valueOf(arg1);
   mBtnDatePicker.setText(date);   
  }  };
}

[/sociallocker]

Leave a comment

Your email address will not be published.