androidandroid-layoutandroid-edittextandroid-textinputlayoutdatepickerdialog

OnClickListener in TextInputEditText wrapped around TextInputLayout


I have got a TextInputEditText wrapped around a TextInputLayout, as Google recommends to do: https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

However, my TextInputEditText has an OnClickListener set on it so when it gets tapped by the User, a DatePickerDialog can popup.
My issue is that in fact, on the first tap nothing happens apart from the hint label to move above the TextInputEditText. On second tap, the DatePickerDialog is shown.

How could I get that DatePickerDialog to show up on first tap?
Setting the onClickListener on the TextInutLayout instead of the TextInputEditText does not work. Anyone has an idea?


Solution

  • The calendar opening only on the second click is because you are using an edittext. On the first click, your Edit Text will get focus. then the second click only calls the onClickListener.

    If you are not looking forward to edit the date set manually (using keyboard), then why not using a TextView to display the selected Date?.

    Show Calendar on First Click :

    If you need to use EditText and load the calender in the first click, then try setting an onFocusChangeListner to the editText instead of onClickListner.

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
               // Show your calender here 
            } else {
               // Hide your calender here
            }
        }
    });