2

I have an AlertDialog which includes a button to initiate a DatePicker. Is it possible to accomplish this within a dialog and if so how would I go about it?

I have searched Google and several posts on SO but have been unable to find information on accomplishing a DatePicker within an Alert Dialog.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ljamison
  • 115
  • 1
  • 10
  • Have you tried this: http://stackoverflow.com/questions/26834831/alertdialog-with-datepicker – Joe Maher Mar 07 '15 at 05:15
  • I attempted that solution myself. The problem that I face is that the solution there is for JUST a DatePicker. Here is a screenshot of the AlertDialog I am referring to. http://imgur.com/jPlmKw1 where I want to open up the DatePicker clicking on "Set Date" button and then, once selected, go back to the AlertDialog shown. – ljamison Mar 07 '15 at 05:35
  • So the set date button, you want that to open up another dialog? or display a datepicker in that current dialog? – Joe Maher Mar 07 '15 at 05:52
  • Ideally, I would like the DatePicker to open up within the same dialog then once a date is selected return to the edit dialog (this is because when "Update" is clicked, the date will be updated in the parent activity). – ljamison Mar 07 '15 at 06:01

2 Answers2

1

Without seeing your code, it is difficult to Guess where is an Error, but you may try below code:

   public class MainActivity extends ActionBarActivity implements DatePickerDialog.OnDateSetListener {


    Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Set your Alert Dialog box code
        mContext=this;

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("This is Alert dialog messgae")
        .setTitle("Launch the date Picker")
        .setPositiveButton("Date Pick", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Launch a date picker from Here
                showDatePickerDialog();

            }
        })
        .setNegativeButton("cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            //Cancel the Dialog itself  

            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();

    }


    public void showDatePickerDialog() {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    private static class DatePickerFragment extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), (OnDateSetListener) getActivity(), year, month, day);
        }

    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub

    }


}

As you can see, I am creating a Date Picker using Fragment on click of Alert Dialog Button.

Your Activity needs to implement

onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)

Otherwise App crashes with an java.lang.ClassCastException with below error message :

MainActivity$DatePickerFragment cannot be cast to android.app.DatePickerDialog$OnDateSetListener
AADProgramming
  • 6,077
  • 11
  • 38
  • 58
0

AlertDialog is just for alert messages. Use Dialog and you can set your own content view.

kris larson
  • 30,387
  • 5
  • 62
  • 74