0

I want to center two switches as I did with the textviews but I've tried all the possible options without success.

This is a brief version of the code:

<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:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="NOTIFICATIONS" />

        <TextView [...] />
    </LinearLayout>

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

        <Switch
            android:id="@+id/swnotif"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1" />

        <Switch [...] />

    </LinearLayout>

</LinearLayout>

Screenshot

David
  • 503
  • 3
  • 7
  • 18

4 Answers4

2

The problem is that the gravity attribute doesn't seem to affect Switch views like it does text and other views. A workaround is to wrap each of the switches in a RelativeLayout like the following:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />
    </RelativeLayout>
Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
1

try this i tested

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

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="NOTIFICATIONS" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="MUTE" />
    </LinearLayout>

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <Switch
                android:id="@+id/swnotif1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />
        </FrameLayout>
    </LinearLayout>
</LinearLayout>
uday
  • 1,348
  • 12
  • 27
  • Thanks! It works. But now I've tried using Horizontal LinearLayouts instead of FrameLayouts and also works, what is the best solution? – David Dec 26 '14 at 12:57
  • nice to hear that.. please refer [this](http://stackoverflow.com/a/4905403/3442067) – uday Dec 26 '14 at 13:01
0

Here bro,

I adjust margin left and right.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:weightSum="2">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:textSize="30sp"
            android:gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="30sp"/>
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="New"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_weight="1"
            />
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="New"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

Hope this can help

Ye Min Htut
  • 2,904
  • 15
  • 28
0

Set center_vertical gravity to TextView and Switch parent LinearLayout :

android:gravity="center_vertical"
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67