Tuesday, October 26, 2010

How to use Date Picker...

A DatePicker is a widget that allows the user to select a month, day and year.



Create a new Android Application, AndroidDatePicker.

Modify the layout, main.xml, to add a button to start the DatePicker.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
    android:id="@+id/datepickerbutton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="DatePicker"
 />
</LinearLayout>


Modify the main program, AndroidDatePicker.java.

package com.exercise.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;
import android.widget.DatePicker;
import android.widget.Toast;

public class AndroidDatePicker extends Activity {
 
 private int myYear, myMonth, myDay;
 static final int ID_DATEPICKER = 0;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button datePickerButton = (Button)findViewById(R.id.datepickerbutton);
        datePickerButton.setOnClickListener(datePickerButtonOnClickListener);
    }
    
    private Button.OnClickListener datePickerButtonOnClickListener
     = new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    final Calendar c = Calendar.getInstance();
    myYear = c.get(Calendar.YEAR);
    myMonth = c.get(Calendar.MONTH);
    myDay = c.get(Calendar.DAY_OF_MONTH);
    showDialog(ID_DATEPICKER);
   }
    };

 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub
  switch(id){
   case ID_DATEPICKER:
    Toast.makeText(AndroidDatePicker.this, 
      "- onCreateDialog -", 
      Toast.LENGTH_LONG).show();
    return new DatePickerDialog(this,
      myDateSetListener,
      myYear, myMonth, myDay);
   default:
    return null;
  }
 }
    
 private DatePickerDialog.OnDateSetListener myDateSetListener
  = new DatePickerDialog.OnDateSetListener(){
   @Override
   public void onDateSet(DatePicker view, int year, 
     int monthOfYear, int dayOfMonth) {
    // TODO Auto-generated method stub
    String date = "Year: " + String.valueOf(year) + "\n"
     + "Month: " + String.valueOf(monthOfYear+1) + "\n"
     + "Day: " + String.valueOf(dayOfMonth);
    Toast.makeText(AndroidDatePicker.this, date, 
      Toast.LENGTH_LONG).show();
   } 
 };
}


A Toast will be displayed in onCreateDialog(), to show that DatePickerDialog() will be created once only (in the first time display the dialog).