Sunday, July 20, 2014

How to Use DatePicker Control

Main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/changeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Change Date" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current/Selected Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />
  
  

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>


Activity_main.java


package com.chirag.datepicker;


import com.android.datepicker.R;

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.Button;
import android.widget.DatePicker;
import android.widget.EditText;


public class DatePickerExample extends Activity {

private Button changeDate;
public EditText ed;
private int year;
private int month;
private int day;

static final int DATE_PICKER_ID = 709; 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

changeDate = (Button) findViewById(R.id.changeDate);
ed=(EditText)findViewById(R.id.editText1);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year  = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day   = c.get(Calendar.DAY_OF_MONTH);

// Show current date
ed.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
 
// Button listener to show date picker dialog
changeDate.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
                
// On button click show datepicker dialog 
showDialog(DATE_PICKER_ID);

}

});
ed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_PICKER_ID);

}
});
   }

@Override
protected Dialog onCreateDialog(int id) 
{
switch (id) 
{
case DATE_PICKER_ID:
// open datepicker dialog. 
// set date picker for current date 
// add pickerListener listner to date picker
return new DatePickerDialog(this, pickerListener, year, month,day);
}
return null;
}

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() 
{

// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year  = selectedYear;
month = selectedMonth;
day   = selectedDay;

// Show selected date 
ed.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
  }
   };

}



Screen Short:-