Search code examples
androidandroid-layoutsurfaceview

Widgets gets shifted around by surfaceview in a relative layout android


enter image description here I have created two joysticksview using surfaceview. I want to place a switch button shown in the yellow box in the picture. However, when I move my joystick around on my phone, the switch button would get shifted around.

My xml code for the surfaceivew are below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_v2"
tools:context="com.example.android.java.JoyStickActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:id="@+id/linearLayout">

    <com.example.android.java.JoyStickView
        android:id="@+id/joystickLeft"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentStart="true"
        android:layout_weight="0.5" />


    <com.example.android.java.JoyStickView
        android:id="@+id/joystickRight"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_weight="0.5" />

</LinearLayout>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@+id/linearLayout4"
    android:layout_marginStart="39dp"
    android:layout_marginTop="42dp"
    android:layout_toEndOf="@+id/linearLayout5"
    android:layout_weight="1">

    <Switch
        android:id="@+id/turbo_switch_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="85dp"
        android:layout_marginTop="14dp"
        android:rotation="90"
        android:scaleX="1"
        android:scaleY="0.8"
        android:thumb="@drawable/thumb"
        android:track="@drawable/track" />
</RelativeLayout>

</RelativeLayout>

Solution

  • If you have a moving widget like a joystick. Make sure other layouts or widgets does not depend on the joystick widget. For example, in my switch widget xml code, I have something like android:layout_below="@+id/linearLayout4, and linearLayout4 is the joystick layout. Therefore, when ever my joystick moves, my switch widget layout will always move as well.