I'm trying to solve the same problem described here. After the date has been chosen I wish to use it to open a browser. It should also be noted that I'm doing this work from a fragment.
I am using the second solution but am facing two problems:
- When I press the ChooseDate button the DatePicker pops up but before I can choose the date the app already opens the browser with the old date. When I press back button I see the DatePicker and I can choose a date, and if I press the ChooseDate button again then the browser opens with almost (see 2) the desired date, and then pressing back I still see the DatePicker and so on.
- The DatePicker returns the month with an offset of -1. E.G. I choose March and I get February. It happens both on the Emulator and on a Nexus 5 device. mCalender is a GregorianCalendar.
My questions:
How can I get the Calendar to stay until I choose a date and then make it go away?
Did anyone else encounter the wrong month supplied by the Calender (I rebuilt the App, closed and opened Android Studio), and if so, what did you do with this (other then the obvious +1 hack)
I am using Android Studio 1.1.0, API 21.
Thank you!
Code:
mChooseDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openBrowser(CHOOSE_DAY);
}
});
void openBrowser(int option) {
String date = "";
switch (option) {
...
case CHOOSE_DAY:
chooseDate();
date = mChosenDate;
break;
...
}
String url = getUrl(date);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
...
void chooseDate() {
int year = mCalendar.get(Calendar.YEAR);
int month = mCalendar.get(Calendar.MONTH);
int day = mCalendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(this.getActivity(),
new DateSetListener(), year, month, day);
dialog.show();
}
class DateSetListener implements DatePickerDialog.OnDateSetListener {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
int chosenMonth = monthOfYear;
int chosenDay = dayOfMonth;
Log.i("DEBUG","chosen month: " + chosenMonth);
updateChosenDate(chosenMonth,chosenDay);
}
}