onClick event is not triggering | Android

Overview, when a user interacts with any UI component the various listeners are called in a top-down order. If one of the higher priority listeners "consumes the event" then the lower listeners will not be called.

In your case these three listeners are called in order:

  1. OnTouchListener
  2. OnFocusChangeListener
  3. OnClickListener

The first time the user touches an EditText it receives focus so that the user can type. The action is consumed here. Therefor the lower priority OnClickListener is not called. Each successive touch doesn't change the focus so these events trickle down to the OnClickListener.

Buttons (and other such components) don't receive focus from a touch event, that's why the OnClickListener is called every time.

Basically, you have three choices:

  1. Implement an OnTouchListener by itself:

    ed1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(MotionEvent.ACTION_UP == event.getAction())
                editTextClicked(); // Instead of your Toast
            return false;
        }
    });
    

    This will execute every time the EditText is touched. Notice that the listener returns false, this allows the event to trickle down to the built-in OnFocusChangeListener which changes the focus so the user can type in the EditText.

  2. Implement an OnFocusChangeListener along with the OnClickListener:

    ed1.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                editTextClicked(); // Instead of your Toast
        }
    });
    

    This listener catches the first touch event when the focus is changed while your OnClickListener catches every other event.

  3. (This isn't a valid answer here, but it is a good trick to know.) 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 any text...

Note:

getApplicationContext() can create memory leaks. A good habit is to avoid using it unless absolutely necessary. You can safely use v.getContext() instead.

Hope that helps!


I'm probably too late to the party, but here is a code snipped which fixes the issue with onClick() not being called:

ed1.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP && !v.hasFocus()) {
            // onClick() is not called when the EditText doesn't have focus,
            // onFocusChange() is called instead, which might have a different 
            // meaning. This condition calls onClick() when click was performed
            // but wasn't reported. Condition can be extended for v.isClickable()
            // or v.isEnabled() if needed. Returning false means that everything
            // else behaves as before.
            v.performClick();
        }
        return false;
    }
});

make edit text clickable.. In XML android:clickable="true" or in code

ed1.setClickable(true);

then do

ed1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      Toast.makeText(getBaseContext(), "1",Toast.LENGTH_SHORT).show();
    }
  });