package com.example.pickerdate;
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.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView mDateDisplay;
private int mYear;
private int year1;
private int mMonth;
private int mDay;
private int month;
private int day;
static final int DATE_DIALOG_ID = 1;
Button pickDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
pickDate = (Button) findViewById(R.id.pickDate);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("hello3");
showDialog(DATE_DIALOG_ID);
}
});
System.out.println("hello1");
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
year1 = mYear;
month = mMonth;
day = mDay;
}
@Override
protected Dialog onCreateDialog(int id) {
DatePickerDialog _date = null;
switch (id) {
case DATE_DIALOG_ID:
_date = new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay) {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
System.out.println("----------onDateChanged()-----------"
+ mYear + "--" + year);
System.out.println("----------onDateChanged()-----------"
+ mMonth + "--" + monthOfYear);
System.out.println("----------onDateChanged()-----------"
+ mDay + "--" + dayOfMonth);
/*
* These lines of commented code used for only setting the
* maximum date on Date Picker..
*
* if (year > mYear && year) view.updateDate(mYear, mMonth,
* mDay);
*
* if (monthOfYear > mMonth && year == mYear )
* view.updateDate(mYear, mMonth, mDay);
*
* if (dayOfMonth > mDay && year == mYear && monthOfYear ==
* mMonth) view.updateDate(mYear, mMonth, mDay);
*/
// these below lines of code used for setting the maximum as
// well as minimum dates on Date Picker Dialog..
if ((mYear > year)
|| ((mMonth > monthOfYear) && (mYear == year))
|| ((mDay > dayOfMonth) && (mYear == year) && (mMonth == monthOfYear))) {
view.updateDate(year1, month, day);
}
}
};
}
System.out.println("hello5");
return _date;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("hello6");
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}
private void updateDisplay() {
System.out.println("hello2");
mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" "));
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
System.out.println("hello7");
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
System.out.`enter code here`println("year=" + year);
System.out.println("month=" + monthOfYear);
System.out.println("day=" + dayOfMonth);
updateDisplay();
}
};
}