Question

I am using a DatePickerDialog to prompt the user for a date. It works fine in the simulator and on my Samsung Galaxy Nexus, but onDateSet does not get called on my Samsung Galaxy Tab 2.

I noticed, that the dialog is bigger and shows a calendar besides the normal spin view. Can that be the problem?

Here is some code:

import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import com.cbit.gtbetapp.R;
import com.cbit.gtbetapp.gui.racedata.MeetingDataActivity;
import com.cbit.gtbetapp.gui.racedata.MeetingListFragment;
import com.cbit.gtbetapp.logic.Utility;

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    protected Date date = null;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current date as the default date in the picker
        final Calendar c = Utility.getToday();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        date = c.getTime();

        // Create a new instance of DatePickerDialog and return it
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day) {
            @Override
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                super.onDateChanged(view, year, month, day);
                setTitle(getString(R.string.date_picker_title));
            }
        };
        dialog.setTitle(getString(R.string.date_picker_title));

        dialog.setButton(DatePickerDialog.BUTTON_POSITIVE,
                getString(android.R.string.ok), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                        Intent intent = new Intent(getActivity(), MeetingDataActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra(MeetingListFragment.EXTRA_DATE, date.getTime());
                        startActivity(intent);
                    }
                });
        dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE,
                getString(R.string.button_cancel),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        return dialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        date = c.getTime();
    }
}

What could cause this? A bug in the tablet? Am I missing something? Can anyone think of a workaround?

Thanks a lot!

Was it helpful?

Solution

I'm surprised that it's working at all. The onDateSet() gets called when the default onClick behavior is executed, but since you're completely setting new buttons you're getting rid of the original behavior. You can check the code out. Why are you not starting your activity in the onDateSet()? If what you want is to set the text, then you can get each button instead of setting them (getButton(BUTTON_POSITIVE)).

Also, beware that there's a new bug in Jelly Bean where the onDateSet will get called twice, will get called even if you press back AND they also removed the default Cancel button. Go figure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top