Set current date to Button and Datepicker dialog
This tutorial is going to explain how to set current date on Button and when you click on that Button, DatePicker Dialog open to ask your new Date. When you set Date from datepicker dialog, selected Date will be set to that Button.
A simple Layout with just only Button.
<Button
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="setDate"/>
For Date related task, you have to use Calendar class. And to get current Date, use method getInstance() of Calendar class.
Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
mYear, mMonth, mDay is of int type.
Now set date on Button:
Button date = (Button) findViewById(R.id.date);
date.setText(new StringBuilder().append(mDay).append("/").append(mMonth+1).append("/").append(mYear));
Now Create one method with same name that you define in layout file for Button click event. Here I wrote setDate()
public void setDate(View view) {
showDialog(0);
}
then to access showDialog(int id), method onCreateDialog(int id) will be used, that return type is Dialog.
protected Dialog onCreateDialog(int id) {
if (id == 0) {
return new DatePickerDialog(this, myDateListener, mYear, mMonth, mDay);
}
return null;
}
Now define DatePickerDialogListener event and get selected month, year and day then set on Button
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
date.setText(new StringBuilder().append(dayOfMonth).append("/").append(monthOfYear+1).append("/").append(year));
}
};
That's it.
Here you noticed that I add +1 in month field. Because When you set the month from calender than it should be between 0 to 11. So you have to +1 to month to get correct month.
showDialog(int id) method is now deprecated. So people not prefer to choose this one. Now DialogFragment is available for this type of dialog. Here I explain you.
Create DatePickerFragment class that extends to DialogFragment. Pass parameters of day, month and year to the fragment, and call change date method.
Define date listener: OnDateSetListener ondateSet;
change date method in DialogFragment
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
Override method of SetArguments(Bundle args) to get parameters passed from activity
@Override
public void setArguments(Bundle args) {
// TODO Auto-generated method stub
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}
Override onCreateDialog() method
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
Now from activity, pass parameteres on click of button:
DatePickerFragment date = new DatePickerFragment();
Bundle args = new Bundle();
args.putInt("year", mYear);
args.putInt("month", mMonth);
args.putInt("day", mDay);
date.setArguments(args);
date.setCallBack(ondate);
date.show(getSupportFragmentManager(), "Date Picker");
Create OnDateSetListener to change date
OnDateSetListener ondate = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
date.setText(new StringBuilder().append(dayOfMonth).append("/")
.append(monthOfYear + 1).append("/").append(year));
}
};
That's It.
Source code: DatePicker
A simple Layout with just only Button.
<Button
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="setDate"/>
For Date related task, you have to use Calendar class. And to get current Date, use method getInstance() of Calendar class.
Calendar cal = Calendar.getInstance();
mYear = cal.get(Calendar.YEAR);
mMonth = cal.get(Calendar.MONTH);
mDay = cal.get(Calendar.DAY_OF_MONTH);
mYear, mMonth, mDay is of int type.
Now set date on Button:
Button date = (Button) findViewById(R.id.date);
date.setText(new StringBuilder().append(mDay).append("/").append(mMonth+1).append("/").append(mYear));
Now Create one method with same name that you define in layout file for Button click event. Here I wrote setDate()
public void setDate(View view) {
showDialog(0);
}
then to access showDialog(int id), method onCreateDialog(int id) will be used, that return type is Dialog.
protected Dialog onCreateDialog(int id) {
if (id == 0) {
return new DatePickerDialog(this, myDateListener, mYear, mMonth, mDay);
}
return null;
}
Now define DatePickerDialogListener event and get selected month, year and day then set on Button
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
date.setText(new StringBuilder().append(dayOfMonth).append("/").append(monthOfYear+1).append("/").append(year));
}
};
That's it.
Here you noticed that I add +1 in month field. Because When you set the month from calender than it should be between 0 to 11. So you have to +1 to month to get correct month.
showDialog(int id) method is now deprecated. So people not prefer to choose this one. Now DialogFragment is available for this type of dialog. Here I explain you.
Create DatePickerFragment class that extends to DialogFragment. Pass parameters of day, month and year to the fragment, and call change date method.
Define date listener: OnDateSetListener ondateSet;
change date method in DialogFragment
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
Override method of SetArguments(Bundle args) to get parameters passed from activity
@Override
public void setArguments(Bundle args) {
// TODO Auto-generated method stub
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}
Override onCreateDialog() method
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
Now from activity, pass parameteres on click of button:
DatePickerFragment date = new DatePickerFragment();
Bundle args = new Bundle();
args.putInt("year", mYear);
args.putInt("month", mMonth);
args.putInt("day", mDay);
date.setArguments(args);
date.setCallBack(ondate);
date.show(getSupportFragmentManager(), "Date Picker");
Create OnDateSetListener to change date
OnDateSetListener ondate = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
date.setText(new StringBuilder().append(dayOfMonth).append("/")
.append(monthOfYear + 1).append("/").append(year));
}
};
That's It.
Source code: DatePicker
Comments
Post a Comment