1

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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50
  • did you try android:layout_gravity = "left" ; android:layout_gravity = "center" ; android:layout_gravity = "right" for each control respectively – SH7 Oct 15 '16 at 21:29
  • Thanks for the reply! Yes I tried but it didn't work. I don't know if it's not working because of one of the other attributes. – Marcos Guimaraes Oct 15 '16 at 21:33

2 Answers2

0

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>
SH7
  • 732
  • 7
  • 20
0

delete android:weightSum="1" from the LinearLayout and do something like that:

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal">

    <Switch
        android:id="@+id/mySwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</FrameLayout>

same to the TextView, and with the FAB delete:

    android:layout_gravity="end"
    android:gravity="center"
    android:layout_weight="1"
ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
T.S
  • 911
  • 8
  • 24