Android onclicklistener doesn't work the first time when clicked

android

Solution

Set focusable on EditText to false. First click gains focus to EditText.

<EditText
                    android:id="@+id/step"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginTop="5dp"
                    android:hint="@string/hnt_message"
                    android:text="Default text"
                    android:maxLength="@integer/max_free_msg_length"
                    android:maxLines="2"
android:focusable="false"
                    />

UPDATE: Ok, in your case you can do it with `onFocusChangeListener` instead of `onClickListener`.

edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //do something
                }
            }
        });

Problem

I've got a EditText with default text on it. Now when a user clicks on that EditText the default text should be changed to something. What I've got is: - I click on the EditText the cursor comes after the default text nothing happens - When I click again then the onClickListener works How can I fix this that it is done from the first time? This is my xml: ``` <EditText android:id="@+id/step" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="5dp" android:hint="@string/hnt_message" android:text="Default text" android:maxLength="@integer/max_free_msg_length" android:maxLines="2" /> ``` This is my fragment code: ``` private EditText editMessage; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mContentView = inflater.inflate(R.layout.fragment_step, container, false); ButterKnife.inject(this, mContentView); editMessage = (EditText) mContentView.findViewById(step); editMessage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { clearEditMessageText(); } }); } ``` I know I could use butterknife to for this button but I did and that didn't worked.

Original source

Related problems