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:
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);
}
}
You need to move this:
String url = getUrl(date);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
to the method the DateSetListener calls onDateSet, so it doesn't fire before you've actually set a date. Your current code would have it run through the switch case and start the intent without waiting on the Dialog in openBrowser().
The issue with the month could be because the calendar begins at 0 and the value being supplied is not offsetting this. So just increment by 1.