Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect a cancel click of the datepicker dialog?

Tags:

i am using following example of date picker

http://developer.android.com/guide/tutorials/views/hello-datepicker.html

now i want to perform some functionality on clicking date picker cancel button but dont see cancel event inside datepickerdialog

can any one guide me how to achieve this.

any help would be appreciated.

like image 626
UMAR-MOBITSOLUTIONS Avatar asked May 28 '10 12:05

UMAR-MOBITSOLUTIONS


People also ask

What is DatePicker dialog?

Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components.

What is correct method of DatePicker?

In Android, DatePicker is a widget used to select a date. It allows to select date by day, month and year in your custom UI (user interface). If we need to show this view as a dialog then we have to use a DatePickerDialog class. For selecting time Android also provides timepicker to select time.

How can I change DatePicker dialog color?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Kindly fine the highlighted code, this is the simplest way to change the colour of your datePicker.


2 Answers

Here's how I did it:

  DatePickerDialog dialog = new DatePickerDialog(this,               mDateSetListener,               year, month, day);    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {     public void onClick(DialogInterface dialog, int which) {        if (which == DialogInterface.BUTTON_NEGATIVE) {           // Do Stuff        }     }   }); 
like image 118
esilver Avatar answered Oct 19 '22 11:10

esilver


If you want to perform a different action depending on whether the user selected a date or not you can use an onDismiss handler. Be sure to set a Boolean (e.g., "isDataSet") to indicate whether the user selected a date or not. Here's an example:

// Date Picker Dialog    public void showDatePickerDialog(View view) {    int year, month, day;    isDataSet = false;  // this is used by the onDismiss handler  // Set initial time period in DatePicker to current month    calendarCurrent = Calendar.getInstance();     month = calendarCurrent.get(Calendar.MONTH);    day =   calendarCurrent.get(Calendar.DAY_OF_MONTH);    year =  calendarCurrent.get(Calendar.YEAR);     DatePickerDialog datePickerDialog = new DatePickerDialog(YourActivity.this, dateSetListener, year, month, day );    datePickerDialog.setOnDismissListener(mOnDismissListener);    datePickerDialog.show();    datePickerDialog_visible=true;  //indicate dialog is up  } // [END showDatePickerDialog]   //onDismiss handler private DialogInterface.OnDismissListener mOnDismissListener =         new DialogInterface.OnDismissListener() {             public void onDismiss(DialogInterface dialog) {                 datePickerDialog_visible=false;  //indicate dialog is cancelled/gone                 if (isDataSet) {  // [IF date was picked                     //do something, date now selected                 } else {                     //do someething else, dialog cancelled or exited                 }                }         }; 
like image 40
PeteH Avatar answered Oct 19 '22 10:10

PeteH