26

I have the following radio buttons inside a radio group of similar buttons. By default a button is on the left of the associated text. How do I get the button itself to be on the right of the associated text?

<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: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:onClick="doTask2"
    android:paddingLeft="40dip"
    android:text="@string/task_name_2"
    android:textColor="#000000" />

</RadioGroup>
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
learner
  • 11,490
  • 26
  • 97
  • 169
  • I've used a trick to achieve this in all SDKs with default UI and default select behavior, in below answer: http://stackoverflow.com/a/42734740/4832356 – SiSa Mar 11 '17 at 12:26

6 Answers6

54

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>
andreikashin
  • 1,528
  • 3
  • 14
  • 21
stinepike
  • 54,068
  • 14
  • 92
  • 112
19

by using

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

you change radio button format. It is better practice just to align right,

android:layoutDirection="rtl"

on each radioButton, and text will be on left side.

Andracchi
  • 191
  • 1
  • 4
4

You can do in xml

<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center" //or "center_vertical" for center text
android:layoutDirection="rtl"
android:text="hello" />

Following line is enough

android:layoutDirection="rtl"
Mahesh Lad
  • 1,997
  • 1
  • 9
  • 8
3

easy way add this to your radiobutton in xml

android:layoutDirection="rtl"
Nissa
  • 4,636
  • 8
  • 29
  • 37
1

If you plan to add elements dynamically you can try to create a layout with a textview and a radio button with no text on the right. Something on these lines:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RadioButton
    android:id="@+id/radiobutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"/>

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/radiobutton"
    android:layout_alignBottom="@+id/radiobutton"
    android:layout_alignParentLeft="true">

Carlos
  • 147
  • 10
0

A much simpler solution, as I posted here is to use the built in Android layout - android.R.layout.simple_list_item_single_choice.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />

If you want to modify it, you can copy-paste the above into a new XML file, and then use the <include> tag to include it in your other layouts (under the Apache 2.0 License). You can also use it as a list item, and leave it unchanged.

Community
  • 1
  • 1
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125