androidandroid-layoutright-aligncenter-align

Android: center and right alignment inside LinearLayout


I have two Linear Layouts one inside the other. In the second one I have a Switch button, a TextView and a FloatingActionButton. I want to put these 3 in the same line and I want to centralize the Switch and the TextView and I want to align the FloatingActionButton on the right side of the screen. I tried to use marginLeft to force them to go to the position I want but I believe this is not the correct way.

Here's my code (right now the 3 components are already in the same line):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:weightSum="1" >

    <Switch
        android:id="@+id/mySwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="140dp"
        android:layout_marginTop="20dp"
        android:theme="@style/SCBSwitch"
        />

        <TextView
            android:id="@+id/OpenClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:text="Open"
            android:textSize="20dp"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/action_find_location"
        android:layout_width="277dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="75dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/calendar_today"
        fab:backgroundTint="@color/theme_color"
        fab:borderWidth="0dp"
        fab:elevation="9dp"
        fab:rippleColor="@color/toolbar_title_color"
        fab:fabSize="mini"/>

    </LinearLayout>
    </LinearLayout>

Solution

  • Just took your code and made some changes. Please check if it works fine

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:weightSum="3">
    
        <Switch
            android:id="@+id/mySwitch"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:theme="@style/SCBSwitch"
            android:layout_gravity="start"
            android:gravity="center"
            android:layout_weight="1"/>
    
        <TextView
            android:id="@+id/OpenClose"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="Open"
            android:textSize="20dp"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:layout_weight="1"/>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/action_find_location"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/calendar_today"
            fab:backgroundTint="@color/theme_color"
            fab:borderWidth="0dp"
            fab:elevation="9dp"
            fab:rippleColor="@color/toolbar_title_color"
            fab:fabSize="mini"
            android:layout_gravity="end"
            android:gravity="center"
            android:layout_weight="1"/>
    
    </LinearLayout>
    </LinearLayout>