how to put the text on the left of a radio button in android

BasILyoS picture BasILyoS · Sep 20, 2013 · Viewed 85.2k times · Source

I want to put the text of a radio button on the left not on the right

I found this solution

        <RadioGroup
        android:id="@+id/radios"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_gravity="right"
        android:inputType="text"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/first"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@color/white"
            android:button="@null"
            android:drawablePadding="30dp"
            android:drawableRight="@android:drawable/btn_radio"
            android:text="first"
            android:textColor="@color/Black"
            android:textSize="20dip" />

        <RadioButton
            android:id="@+id/second"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/Black"
            android:button="@null"
            android:drawablePadding="30dp"
            android:drawableRight="@android:drawable/btn_radio"
            android:text="second"
            android:textColor="@color/White"
            android:textSize="20dp" />

        <RadioButton
            android:id="@+id/third"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/Maroon"
            android:button="@null"
            android:drawablePadding="30dp"
            android:drawableRight="@android:drawable/btn_radio"
            android:text="third"
            android:textColor="@color/Vanilla"
            android:textSize="20dp" />
    </RadioGroup>

but the problem is that the text gravity will be at the left what I want is to put it to right because i'm writing Arabic words

enter image description here

Answer

Irshu picture Irshu · Jul 31, 2016

Try adding the following attributes into the RadioButton, it should work, this way you still get to keep the ripple effect on the radio button:

android:layoutDirection="rtl"
android:textAlignment="textStart"
android:layout_gravity="start"

Remember to set supportsRtl property to true in your application manifest.

for eg:

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

        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layoutDirection="rtl"
            android:textAlignment="textStart"
            android:layout_gravity="start"
            android:text="The test item a"
            android:textSize="14sp" />

         ....                       

    </RadioGroup>

would give out:

enter image description here