Question

I have to use a DatePickerDialog to get selected date in a textview. When I click the textview the DatePickerDialog should appear.

I wrote this

private DatePickerDialog.OnDateSetListener dateOnDateSetListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
        datePick.setText(new StringBuilder().append(selectedDay).append("-").append(selectedMonth).append("-").append(selectedYear));
    }
};

datePick = (TextView) findViewById(R.id.date_pick);

    datePick.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
            monthOfYear = calendar.get(Calendar.MONTH);
            dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            new DatePickerDialog(getContext(), dateOnDateSetListener, year, monthOfYear, dayOfMonth);
        }
    });

But its not working. So with little googling I changed it to this and its working, but I am not able to understand why is it so? Did I mess something in above code

datePick = (TextView) findViewById(R.id.date_pick);

    datePick.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
            monthOfYear = calendar.get(Calendar.MONTH);
            dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            showDialog(DATE_DIALOG_ID);
        }
    });

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       // set date picker as current date
       return new DatePickerDialog(this, dateOnDateSetListener, 
                     year, monthOfYear,dayOfMonth);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener dateOnDateSetListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
        datePick.setText(new StringBuilder().append(selectedDay).append("-").append(selectedMonth).append("-").append(selectedYear));
    }
};
Was it helpful?

Solution

But its not working. So with little googling I changed it to this and its working, but I am not able to understand why is it so? Did I mess something in above code

You didn't really mess anything up, you just forgot to call .show() on the dialog; i.e.:

new DatePickerDialog(getContext(), dateOnDateSetListener, year, monthOfYear, dayOfMonth).show();

The reason why it works without .show() in the second code snippet is because there the Activity manages the dialog and will thus take care of calling that method on the dialog that gets returned in onCreateDialog(int id).

Generally I'd recommend the second approach because you don't really want to manage the life cycle of dialogs yourself - simply because it's quite a pain to get it right when your activities get recreated on configuration changes etc. However, for a more modern solution, using fragments, you should look into wrapping the dialog in a DialogFragment (there's also a version in the support library that works on pre-Honeycomb devices).

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