Question

enter image description here

Well I have two imageviews and they look like this. I want to add a TouchListener to just imageview1. but Listener not working properly. I am adding imageview2 to

 android:focusable="false"

but not working again

Was it helpful?

Solution

Try this. It works for me correctly even when you click on imageview2. MainActivity onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imageView1 = (ImageView) findViewById(R.id.imageview1);
    imageView1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Log.i("imageviewandontouchlistener", "imageView1 onTouch");
            return false;
        }
    });
}

And in your Layout XML:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"/>

    <ImageView
        android:id="@+id/imageview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2" />
</FrameLayout>

OTHER TIPS

Set a custom OnTouchListener to imageview2 that always returns false in the onTouch method. This way the touch event will be passed to the views bellow until one of them returns true in the onTouch method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top