Displaying the DatePicker View in a Dialog Window
Like the TimePicker, you can also display the DatePicker in a dialog window.
Using a Dialog to Display the DatePicker View
1 . Add the following statements in bold to the MainActivity.java file:
package com.emergingandroidtech.DatePicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
importandroid.widget.Button;
import android.widget.Toast;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.widget.TimePicker;
import android.widget.DatePicker;
import android.app.DatePickerDialog;
import java.util.Calendar;
public class MainActivity extends Activity{
TimePicker timePicker;
DatePicker datePicker;
int hour,minute;
int yr, month, day;
static final int TIME_DIALOG_ID=0;
static final int DATE_DIALOG_ID = 1;
/**Called when the activity is first created.*/
@Override
public void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//showDialog(TIME_DIALOG_ID);
//---get the current date---
Calendar today = Calendar.getInstance();
yr = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
timePicker=(TimePicker)findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
datePicker=(DatePicker)findViewById(R.id.datePicker);
//---Buttonview---
Button btnOpen=(Button)findViewById(R.id.btnSet);
btnOpen.setOnClickListener(newView.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(), “Dateselected:”+datePicker.getMonth()+ “/”+datePicker.getDayOfMonth()+ “/”+datePicker.getYear()+“\n”+ “Timeselected:”+timePicker.getCurrentHour()+ “:”+timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); }
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case TIME_DIALOG_ID:
return newTimePickerDialog( this,mTimeSetListener,hour,minute,false);
case DATE_DIALOG_ID:
return new DatePickerDialog( this, mDateSetListener, yr, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth) {
yr = year;
month = monthOfYear;
day = dayOfMonth;
Toast.makeText(getBaseContext(), “You have selected : “ + (month + 1) + “/” + day + “/” + year, Toast.LENGTH_SHORT).show();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener= newTimePickerDialog.OnTimeSetListener()
{
public void onTimeSet( TimePicker view,int hourOfDay,int minuteOfHour)
{
hour=hourOfDay;
minute=minuteOfHour;
Toast.makeText(getBaseContext(), “Youhaveselected:“+hour+“:”+minute, Toast.LENGTH_SHORT).show();
}
};
}
How It Works
The DatePicker works exactly like the TimePicker. When a date is set, it fires the onDateSet() method, where you can obtain the date set by the user:
public void onDateSet( DatePicker view,int year,int monthOfYear,int dayOfMonth)
{
yr=year;
month=monthOfYear;
day=dayOfMonth;
Toast.makeText(getBaseContext(), “Youhaveselected:“+(month+1)+ “/”+day+“/”+year, Toast.LENGTH_SHORT).show();
}
* Note that you have to initialize the three variables — yr, month, and day — before showing the dialog:
//---getthecurrentdate---
Calendar today=Calendar.getInstance();
yr=today.get(Calendar.YEAR);
month=today.get(Calendar.MONTH);
day=today.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
If you don’t, you will get an illegal argument exception error during run time (“current should be >= start and <= end”) when you create an instance of the DatePickerDialog class.
No comments:
Post a Comment