EGHDK EGHDK - 1 month ago 18
Android Question

onClick event is not triggering | Android

I made a very simple test application with one activity and one layout. The

onClick
doesn't trigger the first time it is pressed, as it should.

The activity:

package com.example.mytest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed1 = (EditText) findViewById(R.id.editText1);

ed1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG)
.show();

}

});
}

}


The layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:ems="10" />

</RelativeLayout>


If you run this application, and click on the second
editText
and then back on the first one, it will not trigger the
onClick
. You can keep selecting back and forth and it will not trigger the
onClick
at all. I need this basic functionality, but haven't been able to think of a way to get it to work. Ideas?

Notice



I have tried all of the methods recommended on my API level 16 physical device and my API level 8 emulator, but I get the same problem.

Clarification



When
editText1
is focused and is clicked on, then the
onClick
method fires. If
editText2
is focussed, and then
editText1
is clicked, it doesn't fire. Big problem.

Sam Sam
Answer

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.

Basically, 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 any text...

  2. Implement an OnFocusChangeListener along with the OnClickListener:

    ed1.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                Toast.makeText(getApplicationContext(), "onFocusChange", Toast.LENGTH_LONG).show();
        }
    });
    

    Together you can catch every touch event on your EditText.

  3. Implement an OnTouchListener by itself:

    ed1.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 that helps!