Search code examples
androidandroid-layoutontouchlistenertouch-eventandroid-imagebutton

Touch Down on Layout Blocking Other Touch Events of Buttons


I'm implementing tap events on several ImageButtons and code looks like this

  MyTouchListener touchListener = new MyTouchListener();
        l1.setOnTouchListener(touchListener);
        r1.setOnTouchListener(touchListener);
        l2.setOnTouchListener(touchListener);
        r2.setOnTouchListener(touchListener);

 public class MyTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            switch (v.getId()) {
                case R.id.l1:
                    clickL1(v);
                    break;
                case R.id.r1:
                    clickR1(v);
                    break;
                case R.id.l2:
                    clickL2(v);
                    break;
                case R.id.r2:
                    clickR2(v);
                    break;
            }
            return true;
        }

        return false;
   }

Problem I have encountered is when I hold one finger on layout (anywhere EXCEPT on imagebuttons), the buttons do not register any events while I hold it.

I tried giving my layout its own touch listener that returns false but that didnt work.

What should I do to "disregard" touch down event on layout and just listen normally to imagebutton events?

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:id="@+id/relativeLayout"
    android:splitMotionEvents="true"
    tools:context="com.example.josip.something.GameOffline">


    <ImageButton
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/l1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:focusableInTouchMode="true"/>

    <ImageButton
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/r1"
        android:layout_above="@+id/l1"
        android:layout_toRightOf="@+id/l1"
        android:layout_toEndOf="@+id/l1"
        android:focusableInTouchMode="true"/>

    <ImageButton
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/r2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:focusableInTouchMode="true"/>

    <ImageButton
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/l2"
        android:layout_above="@+id/r2"
        android:layout_toLeftOf="@+id/r2"
        android:layout_toStartOf="@+id/r2"
        android:focusableInTouchMode="true"/>


<RelativeLayout>

Solution

  • So I found a solution doing this. I put another RelativeLayout as a child of main Layout and as a sibiling of buttons. I made it clickable and this way it works when I hold finger on layout anywhere except the buttons and I try clicking buttons, the event triggers. Why is that, I dont know and if someone does I would like an explanation.

    <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"
        android:id="@+id/relativeLayout"
        android:splitMotionEvents="true"
        tools:context="com.example.josip.something.GameOffline">
    
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="true"></RelativeLayout>
    
        <ImageButton
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/l1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:focusableInTouchMode="true"/>
    
        <ImageButton
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/r1"
            android:layout_above="@+id/l1"
            android:layout_toRightOf="@+id/l1"
            android:layout_toEndOf="@+id/l1"
            android:focusableInTouchMode="true"/>
    
        <ImageButton
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/r2"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:focusableInTouchMode="true"/>
    
        <ImageButton
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/l2"
            android:layout_above="@+id/r2"
            android:layout_toLeftOf="@+id/r2"
            android:layout_toStartOf="@+id/r2"
            android:focusableInTouchMode="true"/>
    
    
    <RelativeLayout>