Total Pageviews

Thursday, December 12, 2013

Display date dialog to select a date in Android.

Sometimes it will be little difficult to enter date, so we require that the date input has to be taken from the dialog. Generally this will avoid the issues with proper date format, separator used in date etc.

So here we will see how to display a dialog as DatePicker.

Here I am calling a date picker function from a click of a button. The function is as follows

 private void showDatePickerDialog(){  
           final Calendar c = Calendar.getInstance();  
           int mYear = c.get(Calendar.YEAR);  
           int mMonth = c.get(Calendar.MONTH);  
           int mDay = c.get(Calendar.DAY_OF_MONTH);  
           DatePickerDialog dpd = new DatePickerDialog(this,  
                     new DatePickerDialog.OnDateSetListener() {  
                @Override  
                public void onDateSet(DatePicker view, int year,  
                          int monthOfYear, int dayOfMonth) {  
                     editSearchText.setText((monthOfYear + 1) + "/"  
                               + dayOfMonth + "/" + year);  
                }  
           }, mYear, mMonth, mDay);  
           dpd.show();  
      }  

In onDateSet() function, it sets the date to the edit view. You can write your own logic to play with the selected date.
Keep Coding :)