Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 741
codareee Avatar asked Sep 05 '13 07:09

codareee


2 Answers

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" />
like image 69
Nachi Avatar answered Oct 02 '22 01:10

Nachi


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>
like image 35
eluleci Avatar answered Oct 02 '22 01:10

eluleci