android - Open a DatePickerDialog on Click of EditText takes two clicks -


i want open calendar on click of edit text. after want set date user selects calendar in edit text. problem that, when click on edittext second time calendar open. please me resolve issue(why calendar don't open first time).

edittext xml code

<edittext             android:id="@+id/dateofbirth"             android:layout_width="290dp"             android:layout_height="wrap_content"             android:layout_marginleft="5dp"             android:layout_margintop="10dp"             android:maxlines="1"             android:hint="dd/mm/yyyy" /> 

activity code

public void informationpopup() {         final dialog dialog= new dialog(mainactivity.this,r.style.dialog_fullscreen);         dialog.setcontentview(r.layout.details_dialog);          dateofbirth = (edittext)dialog.findviewbyid(r.id.dateofbirth);          dialog.show();          mycalendar = calendar.getinstance();         final datepickerdialog.ondatesetlistener date = new datepickerdialog.ondatesetlistener() {              @override             public void ondateset(datepicker view, int year, int monthofyear,                     int dayofmonth) {                 // todo auto-generated method stub                 mycalendar.set(calendar.year, year);                 mycalendar.set(calendar.month, monthofyear);                 mycalendar.set(calendar.day_of_month, dayofmonth);                 updatelabel();             }           };          dateofbirth.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 showdialog(date_dialog_id);               }         });      }      private void updatelabel() {          string myformat = "mm/dd/yy"; //in need put here          simpledateformat sdf = new simpledateformat(myformat, locale.us);          dateofbirth.settext(sdf.format(mycalendar.gettime()));      }       protected dialog oncreatedialog(int id) {             final calendar = calendar.getinstance();              switch (id) {             case date_dialog_id:                 // set date picker current date                 datepickerdialog _date =   new datepickerdialog(this, date,mycalendar                         .get(calendar.year), mycalendar.get(calendar.month),                         mycalendar.get(calendar.day_of_month)){                     @override                      public void ondatechanged(datepicker view, int year, int monthofyear, int dayofmonth){                             if (year > now.get(calendar.year))                              view.updatedate(mycalendar                                     .get(calendar.year), mycalendar                                     .get(calendar.month), mycalendar.get(calendar.day_of_month));                          if (monthofyear > now.get(calendar.month) && year == now.get(calendar.year))                             view.updatedate(mycalendar                                     .get(calendar.year), mycalendar                                     .get(calendar.month), mycalendar.get(calendar.day_of_month));                          if (dayofmonth > now.get(calendar.day_of_month) && year == now.get(calendar.year) &&                                  monthofyear == now.get(calendar.month))                             view.updatedate(mycalendar                                     .get(calendar.year), mycalendar                                     .get(calendar.month), mycalendar.get(calendar.day_of_month));                             }                 };                 return _date;             }             return null;         }  

i not understanding have done wrong. please suggest solution.

i'll try address problem, not sure first reason.

  1. the calendar opening on second click because using edittext. on first click, edit text focus. second click calls onclicklistener.

    if not looking forward edit date set manually (using keyboard), why not using textview display selected date?

  2. the problem date not updating in edittext occurring because not setting datesetlistener in code. need set notify system date set. datechange listener returns date while changing date, , doesn't appear setting date in edittext.

try code:

            calendar cal = calendar.getinstance(timezone.getdefault());             datepickerdialog datepicker = new datepickerdialog(this,                 r.style.appblacktheme,                 datepickerlistener,                 cal.get(calendar.year),                  cal.get(calendar.month),                 cal.get(calendar.day_of_month));              datepicker.setcancelable(false);             datepicker.settitle("select date");              return datepicker;         }     } catch (exception e) {         showmsgdialog("exception",             "an error occured while showing date picker\n\n"             + " error details:\n" + e.tostring(), "ok");     }     return null; }   private datepickerdialog.ondatesetlistener datepickerlistener = new datepickerdialog.ondatesetlistener() {      // when dialog box closed, below method called.     public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) {         string year1 = string.valueof(selectedyear);         string month1 = string.valueof(selectedmonth + 1);         string day1 = string.valueof(selectedday);         textview tvdt = (textview) findviewbyid(r.id.tvdate);         tvdt.settext(day1 + "/" + month1 + "/" + year1);     } }; 

in this, updating date textview id "tvdate". advise using textview instead of edittext , try code.

update

if need use edittext , load calender in first click, try setting onfocuslistner edittext instead of onclicklistner.

edittext.setonfocuschangelistener(new onfocuschangelistener() {     @override     public void onfocuschange(view v, boolean hasfocus) {         if(hasfocus) {            // show calender here          } else {            // hide calender here         }     } }); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -