2

I am designing an Android application and I would like to find a way to handle multitouch between multiple nested views.

My layout is as following:

Relative layout
|
|---Linear layout
|    \---Linear layout
|           \--Button A
|
|---Linear layout
     \---Button B

The two buttons take approximately 50% of the screen size. I would like to be able to press the two buttons at the same time one after the other. I can press button B and then A but when I press A, I'm not able to press B.

It seems that it's a normal behaviour if we consider the way that Android handles event. I googled my problem and I only found complex solutions that involve rewriting a custom view and the "DispatchTouchEvent" method.

Do you know if there's a easy way to avoid that behaviour (being able to press A then B and B and then A)?

I wrote a minimal example: run an activity with the following layout

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:splitMotionEvents="true"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="epl.groupe16.testbutton.MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:text="New Button"
        android:id="@+id/button" />

</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:weightSum="2">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="New Button"
            android:id="@+id/button21"
            />
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="New Button"
        android:id="@+id/button20"
        android:layout_weight="1"
        android:visibility="invisible" />
</LinearLayout>

I can press the lowest button, hold my touch and then click on the upper button but if I try the highest first, the lowest isn't clickable.

Thank you in advance

Alexis Clarembeau
  • 2,776
  • 14
  • 27

1 Answers1

0

Finally we found that there's no easy solution for this. But there was some workarounds that I would like to share if somebody had the same problem.

  • First, there's a manual way to do that: reconding a proper way to propagate event by creating custom views and overwriting the method "DispatchTouchEvent". It's quite long but it can solve the problem.

  • Also, Google added a PercentRelativeLayout in API 23, so you can place all of your components in a single view. But, we were targetting API 19 and Android Marshmallow doesn't represent a huge percentage of the current devices.

  • Finally, we decided to code our custom SurfaceView and touchEvent listener. We drawed our components in a SurfaceView and handled clics manually.

I hope it will help you :)

Alexis Clarembeau
  • 2,776
  • 14
  • 27