onclicklistener – EditText inside TextInputLayout onclick requires 2 click ?! Android-ThrowExceptions

Exception or error:

I am simply trying to have an onlick listen on an Edit text inside a TextInputLayout. It works but I need to click on the EditText twice for it to trigger I dont understand why. Here is my code:



            android:hint="Starting Date*: "
            android:inputType="textPersonName" />


   private void setListenners() {
        EditText startDate = (EditText) mView.findViewById(R.id.start_date);
        startDate.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            public void onClick(View v) {
                Calendar mcurrentDate=Calendar.getInstance();
                int mYear = mcurrentDate.get(Calendar.YEAR);
                int mMonth = mcurrentDate.get(Calendar.MONTH);
                int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        Log.d("DEBUG", "year: " + year + " month: " + month + " day: " + day);
                },mYear, mMonth, mDay);
How to solve:

Set the attribute android:focusableInTouchMode to false


in your edittext xml code.

Explanation, as from docs, android:focusableInTouchMode is:

Boolean that controls whether a view can take focus while in touch
mode. If this is true for a view, that view can gain focus when
clicked on, and can keep focus if another view is clicked on that
doesn’t have this attribute set to true.

and the EditText is true by default.

In other words: the first click will make the edittext to gain focus and second click is the one that triggers the ClickListener. So you should disable gaining focus on touch.

Leave a Reply

Your email address will not be published. Required fields are marked *