Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText OnClickListener taking two clicks to work

I have 4 EditTexts in my activity. Each one has an OnClickListener that when called will check a checkbox for me. However, when I first press on the EditText, the EditText turns blue, but the checkbox does not check. It is only when I press on it again that the OnClickListener takes effect and checks the box.

Does anyone know why this is?

Example code:

public View.OnClickListener respiratoryOnClickListenerv(final View v) {
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CheckBox respiratoryCheckbox = (CheckBox) v.findViewById(R.id.vitals_2);
            respiratoryCheckbox.setChecked(true);
            respiratoryCheckbox.callOnClick();
        }
    };
    return listener;
}


respiratoryEditText.setOnClickListener(respiratoryOnClickListenerv(rootView));
like image 558
Brejuro Avatar asked Dec 07 '15 01:12

Brejuro


1 Answers

When a user interacts with a UI element the various listeners are called in a top down order. (For example: OnTouch -> OnFocusChange -> OnClick.) If a listener has been defined (with setOn...Listener) and it consumes this event: the lower priority listeners will not be called. By its nature the first time you touch an EditText it receives focus with OnFocusChangeListener so that the user can type. The action is consumed here therefor OnClick is not called. Each successive touch doesn't change the focus so the event trickles down to the OnClickListener.

From that, you have three choices:

1) Set the focusable attribute to false in your XML:

android:focusable="false"

Now the OnClickListener will fire every time it is clicked. But this makes the EditText useless since the user can no longer enter text.

2) Implement an OnFocusChangeListener along with the OnClickListener:

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
             //Do your work
        }

    }
});

Together you can catch every touch event on your EditText.

3) Implement an OnTouchListener by itself:

editText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(MotionEvent.ACTION_UP == event.getAction())
                    Toast.makeText(getApplicationContext(), "onTouch: Up", Toast.LENGTH_SHORT).show();
                return false;
            }

    });

This will execute every time the EditText is touched. Notice that this event returns a boolean. Returning false means that the event will continue trickle down and reach the built in onFocusChangeListener allowing it to receive text.

Hope this help!

like image 119
Lankaka Avatar answered Nov 20 '22 07:11

Lankaka