Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 338
Andy Strife Avatar asked Nov 18 '16 06:11

Andy Strife


2 Answers

Set the attribute android:focusableInTouchMode to false, like this:

android:focusableInTouchMode="false"

in your edittext xml code. For more information, visit here

like image 84
Umasankar Avatar answered Oct 15 '22 10:10

Umasankar


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
        }
    }
});
like image 29
Harshad Pansuriya Avatar answered Oct 15 '22 10:10

Harshad Pansuriya