Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 328
BasILyoS Avatar asked Sep 20 '13 10:09

BasILyoS


People also ask

How to add RadioButton in android?

Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.

How to change direction of radio button in android?

By Default RadioGroup Will be in Vertical Orientaion , If you want to change use android:orientation="horizontal" attritube of RadioGroup . By Default RadioButton Will be in Left to Right Direction , If you want to change use android:layoutDirection="rtl" attritube of RadioButton .

What are the methods of radio button in android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user.

How to select one radio button at a time in android?

You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.


Video Answer


2 Answers

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

like image 131
Irshu Avatar answered Oct 02 '22 08:10

Irshu


Add android:gravity="right" in each RadioButton as follow..

  <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"          android:gravity="right"/>      <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"          android:gravity="right"/>      <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"         android:gravity="right" /> </RadioGroup> 
like image 30
Mukesh Kumar Singh Avatar answered Oct 02 '22 08:10

Mukesh Kumar Singh