Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onclicklistener doesn't work the first time when clicked

Tags:

android

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.

like image 724
user1007522 Avatar asked May 15 '14 13:05

user1007522


2 Answers

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
                }
            }
        });
like image 187
Alexander Mikhaylov Avatar answered Oct 24 '22 05:10

Alexander Mikhaylov


I had this issue with an editText where I wanted the player to enter a character name. I didn't like using a hint as the hint is also put in the popup keyboard window, I wanted that initially blank. So I initially added the name request in the editText. I wanted that erased when first clicked. The first click triggered the popup keyboard, but not 'setOnClickListener' to erase the field. I solved the issue by calling a performClick action on the 'setOnFocusChangeListener'.

This still seems a bit of a hack to me, but changing focusable or focusableInTouchMode to false as has been suggested prevents the popup keyboard from showing.

charnameEditText.setText("Enter Name Here");
....
charnameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                view.performClick();
            }
        }
});

//Normally not called by first click
charnameEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           charnameEditText.setText("");
        }
});
like image 1
Androidcoder Avatar answered Oct 24 '22 04:10

Androidcoder