codareee
codareee

Reputation: 570

How to align a Switch to the right? [Android]

I have a layout with switches. I want the text to be on the left and the actual switch to be on the right. How can i do it? I know it's possible because they are like this in the phone settings.

Current situation:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="15dp"
android:dividerPadding="5dp"
android:orientation="vertical"
android:padding="15dp"
android:scrollbars="vertical"
android:showDividers="middle" >

<Switch
     android:paddingBottom="15dp"
    android:id="@+id/switch_gprs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false"
    android:gravity="center_vertical|fill|end"
    android:text="@string/gprs" />

<View
android:paddingTop="15dp"
 android:paddingBottom="15dp"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="@color/holo_black" />

<Switch
    android:paddingTop="15dp"
     android:paddingBottom="15dp"
    android:id="@+id/switch_voicemail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/voicemail" />

<RelativeLayout
    android:paddingTop="15dp"
     android:paddingBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/number" />

    <TextView
        android:id="@+id/voicemail_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

</RelativeLayout>

<RelativeLayout
    android:paddingTop="15dp"
     android:paddingBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/email" />

    <TextView
        android:id="@+id/voicemail_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

</RelativeLayout>

<TextView
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/voicemail_download" />

 <View
android:paddingTop="15dp"
 android:paddingBottom="15dp"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="@color/holo_black" />

<Switch
    android:paddingTop="15dp"
     android:paddingBottom="15dp"
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/redirect" />

<RelativeLayout
    android:paddingTop="15dp"
     android:paddingBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/sim_number" />

    <TextView
        android:id="@+id/redirect_sim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

</RelativeLayout>

<RelativeLayout
    android:paddingTop="15dp"
     android:paddingBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/redirect_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/redirect_number" />

</RelativeLayout>

Upvotes: 8

Views: 19206

Answers (5)

trevren11
trevren11

Reputation: 312

Version 27 works like this:

<Switch
    android:id="@+id/enable_disable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="On/Off" />

Upvotes: 1

apurva kochar
apurva kochar

Reputation: 49

Use linear layout and put spacing between TextView and Switch. Something like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/mexican"
        android:id="@+id/textView" />
    <Space
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </Space>
    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/switch1"
        android:gravity="center"
       />

</LinearLayout>

Upvotes: 0

cegprakash
cegprakash

Reputation: 3125

Surround by a LinearLayout which is within a RelativeLayout with alignparentRight and centerVertical. This works regardless of parent of the switch.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">        

        <!--Your stuff in left side..-->


    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        >
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:id="@+id/selectAppSwitch"
        />
    </LinearLayout>
</RelativeLayout>

Upvotes: 0

eluleci
eluleci

Reputation: 3529

You can set add these to switch in xml

android:layout_width="match_parent"
android:gravity="right|center"

If it doesn't work either you can put your switch into a relative layout(relative layout's width is match_parent) and add this line to the switch.

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

    <Switch 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

Upvotes: 2

Nachi
Nachi

Reputation: 4248

Just set the width to match_parent:

<Switch
    android:paddingBottom="15dp"
    android:id="@+id/switch_gprs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked=false
    android:text="@string/gprs" />

Upvotes: 36

Related Questions