Reputation:
I'm trying to figure out how to get the current selected date to be showned on the CalendarDatePicker. E.g.
If i have September 13, 2016 as the date being selected it should be shown/highlighted on the CalendarDatePicker that i selected that date. So i set the date and display it on a textview, and if I update the date it should get the previous selected date being highligted on the CalendarDatePicker. I've added their onDayOfMonthSelected() method but it still not working. This is what i have so far. I'm using this library Android-BetterPicker from github.
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CalendarDatePickerDialogFragment cdp2 = new CalendarDatePickerDialogFragment()
.setOnDateSetListener(CalendarPicker.this);
cdp2.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
cdp2.setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
mResultTextView.setText(getString(R.string.calendar_date_picker_result_values, year, monthOfYear+1, dayOfMonth));
}
});
}
});
Here's my onResume() function
public void onResume() {
// Example of reattaching to the fragment
super.onResume();
CalendarDatePickerDialogFragment calendarDatePickerDialogFragment = (CalendarDatePickerDialogFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_TAG_DATE_PICKER);
if (calendarDatePickerDialogFragment != null) {
calendarDatePickerDialogFragment.onDayOfMonthSelected(CHOSEN_YEAR,CHOSEN_MONTH,CHOSEN_DAY);
calendarDatePickerDialogFragment.setOnDateSetListener(this);
}
}
Upvotes: 0
Views: 353
Reputation: 5236
They have a function for what you want :
public void onResume() {
// Example of reattaching to the fragment
super.onResume();
CalendarDatePickerDialogFragment calendarDatePickerDialogFragment = (CalendarDatePickerDialogFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_TAG_DATE_PICKER);
if (calendarDatePickerDialogFragment != null) {
calendarDatePickerDialogFragment.onDayOfMonthSelected(CHOSEN_YEAR,CHOSEN_MONTH,CHOSEN_DAY);
calendarDatePickerDialogFragment.setOnDateSetListener(this);
}
}
also change here :
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CalendarDatePickerDialogFragment cdp2 = new CalendarDatePickerDialogFragment()
.setOnDateSetListener(CalendarPicker.this);
cdp2.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
//here
cdp2.onDayOfMonthSelected(CHOSEN_YEAR,CHOSEN_MONTH,CHOSEN_DAY);
cdp2.setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
mResultTextView.setText(getString(R.string.calendar_date_picker_result_values, year, monthOfYear+1, dayOfMonth));
}
});
}
});
Upvotes: 1
Reputation: 880
You can this :
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
If you want to show the current date on Button itself, then use this one :
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
MyDate mydate = new MyDate();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments()!=null && getArguments().get("DATE") != null) {
mydate = (MyDate) getArguments().get("DATE");
}
DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, mydate.year, mydate.month, mydate.day);
dpd.getDatePicker().setCalendarViewShown(false);
dpd.setTitle("Set Date");
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mydate = new MyDate(day,month,year);
if (getTag().equals("birthdatePicker")){
birthday.setText(mydate.toStringDate());
}
else if (getTag().equals("annidatePicker")){
anniversary.setText(mydate.toStringDate());
}
else if (getTag().equals("spousedatePicker")){
spousebday.setText(mydate.toStringDate());
}
}
public String getDate(){
return mydate.toString();
}
}
static class MyDate implements Parcelable {
int day;
int month;
int year;
public MyDate() {
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR)- 15;
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
}
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public String toString() {
return year + "-" + (month+1) + "-" + day;
}
public String toStringDate(){
return day + "-" + (month+1) + "-" + year;
}
public static MyDate toDate(String date) {
String [] strArray = date.split("/");
int d = Integer.parseInt(strArray[0]);
int m = Integer.parseInt(strArray[1])-1;
int y = Integer.parseInt(strArray[2]);
return new MyDate(d, m, y);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.day);
dest.writeInt(this.month);
dest.writeInt(this.year);
}
protected MyDate(Parcel in) {
this.day = in.readInt();
this.month = in.readInt();
this.year = in.readInt();
}
public static final Parcelable.Creator<MyDate> CREATOR = new Parcelable.Creator<MyDate>() {
@Override
public MyDate createFromParcel(Parcel source) {
return new MyDate(source);
}
@Override
public MyDate[] newArray(int size) {
return new MyDate[size];
}
};
}
public void showBirthDateDialog() {
birthFragment.show(getFragmentManager(), "birthdatePicker");
}
birthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showBirthDateDialog();
}
});
I think this will help your cause !
Upvotes: 0