Search code examples
androidmultithreadingontouch

How can I separate the screen into two equally big spaces which react to touch events independently?


At first for better imagination, this is what the gui looks like. When you touch below the right rectangle on the right side of the screen, the right rectanglemoves downwards. But when both players do this on there side, there is a ACTION_MOVE triggered which should not be the case in this scenario...

enter image description here

I am developing a relatively easy multiplayer game in android where two users can interact with the screen. I implemented onTouch differently, depending on the fact which side of the screen has been pressed. It then triggers either an action of player1 or player 2.

My problem is,that when both players press the screen simultaneously, there is a ACTION_MOVE triggered also, and not only two ACTION_DOWN events. The ACTION_MOVE has some specific implementation, but it should only be called when one player on his own causes it and not both together.

So, is there a way to separate the screen into to two areas so that the described problem will not arise?

Thanks


Solution

  • You should implemet a LinearLayout, with two Buttons with weight = 1 for each.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:text="Button1" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:text="Button2" />
    </LinearLayout>
    

    Then you can handle touches from each buttons.