How to Put the Text on the Left of a Radio Button in Android

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

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>

How to align radiobutton with its text?

When you align right with gravity="right" which implicitly aligned button right/top, You can override that with add flag center besides right.

<RadioButton
...
android:gravity="right|center"
... />

RadioButton align text to left side and button to right

try by adding layout gravity and gravity attribute inside both radio button group and radio button

 android:layout_gravity="start"
android:gravity="left"

android set radio button right from text

Solved but next Problem

This was the solution:

<RadioGroup
android:id="@+id/rgRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@+id/HeaderSectionSort"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">

<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:button="@null"
android:drawableRight="@drawable/ic_selected_sort"
android:text="@string/Radio1"
android:textColor="@color/DefaultGrey"
android:textSize="14sp"
android:height="40dp"
android:paddingLeft="16dp"
android:paddingRight="10dp" />

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#f4f4f4"
android:id="@+id/Seperator3"
android:layout_marginTop="5dp" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:paddingLeft="16dp"
android:button="@null"
android:drawableRight="@drawable/ic_selected_sort"
android:buttonTint="@color/DefaultGreen"
android:text="@string/Radio2"
android:textColor="@color/DefaultGrey"
android:textSize="14sp"
android:height="40dp"
android:paddingRight="10dp" />

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#f4f4f4"
android:id="@+id/Seperator4"
android:layout_marginTop="5dp" />

<RadioButton
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:paddingLeft="16dp"
android:button="@null"
android:drawableRight="@drawable/ic_selected_sort"
android:text="Radio3"
android:textColor="@color/DefaultGrey"
android:textSize="14sp"
android:height="40dp"
android:paddingRight="10dp" />

</RadioGroup>

Result:

Sample Image

Problem:
i would like to hide the drawable (ic_selected_sort) at the beginning programmatically. and on click set it to visible.

i try something like this:

        rb1.setButtonDrawable(android.R.color.white);

but i doesn't take effect.
any ideas? :)

Android: radiobutton text not well aligned with number

To fix this you should add the android:textDirection="ltr" which defines the direction of the text from Left to Right. The android:layoutDirection="rtl"
defines the direction of layout drawing so it will draw the radio button to the right side of text.

Your layout should be like the below:

<RadioGroup
android:id="@+id/rgPasswordSendMode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="32dp"
android:divider="?android:attr/dividerHorizontal"
android:orientation="vertical"
android:showDividers="middle|end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<RadioButton
android:id="@+id/phoneRadioButton"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layoutDirection="rtl"
android:textDirection="ltr"
android:text="******6043" />

<RadioButton
android:id="@+id/emailRadioButton"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layoutDirection="rtl"
android:textDirection="ltr"
android:text="test@test.com" />
</RadioGroup>

Result:

radio_button

Aligning Radio Buttons to left in ConstraintLayout?

Try using horizontal orientation in your RadioGroup like below:

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#C8E585"
app:layout_constraintTop_toTopOf="parent" >

<RadioButton
android:id="@+id/btn1"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ColdFusion" />

<RadioButton
android:id="@+id/btn2"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flex" />

<RadioButton
android:id="@+id/btn3"
android:textSize="18sp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flash" />
</RadioGroup>

Output:
Sample Image

Radio buttons aligned with button on right & text on left

Your code is almost fine, You need to set each RadioButtons width to android:layout_width="match_parent" so your each radio button will occupy full width of its parent layout.

It would be much better if you use recycler view as it would be easier to maintain in case of data changes and you could also implement features like search and sort with it.

How can I align radio buttons to the right of associated text?

Use

    android:button="@null"
android:drawableRight="@android:drawable/btn_radio"

So your code will be like:

<RadioGroup
android:id="@+id/points_radio_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<RadioButton
android:id="@+id/do_tastk_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:onClick="doTask1"
android:paddingLeft="40dip"
android:text="@string/task_name_1"
android:textColor="#000000" />

<RadioButton
android:id="@+id/do_tastk_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:onClick="doTask2"
android:paddingLeft="40dip"
android:text="@string/task_name_2"
android:textColor="#000000" />

</RadioGroup>


Related Topics



Leave a reply



Submit