Alignment vertical the switch on Android

186 views Asked by At

I am trying to create a layout (Android Studio) in which I need to vertically align various controls like Switch in below screenshot.

enter image description here

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:textAlignment="center"
        android:text="Entrée Chaude" />

    <Switch
        android:id="@+id/switchRespectMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50px"
        android:switchMinWidth="50dp"
        android:textSize="25sp"
        android:text="RespectMenu : "
        android:checked="false"/>

    <EditText
        android:id="@+id/remarqueRespectMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:ems="10"
        android:hint="Remarque RespectMenu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.184"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Switch
        android:id="@+id/switchGrammage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="50px"
        android:switchMinWidth="50dp"
        android:textSize="25sp"
        android:text="Grammage  : "
         />
1

There are 1 answers

0
BittorH On

If you nest all the horizontal LinearLayouts that you want inside a vertical LinearLayout it should work


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

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

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            <Switch
                android:id="@+id/switch2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Switch" />
        </LinearLayout>

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

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />

            
            <Switch
                android:id="@+id/switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Switch" />
        </LinearLayout>
</LinearLayout>