Change The Side The Text Appears On A Radio Button
Solution 1:
I wanted to do the same thing, without having to extend yet another class (or two, as you would have to at least extend CompoundButton and RadioButton) for something that should be part of the implementation from the beginning. Since I was using a RadioGroup, which will not work if you put say, a RadioButton and a TextView in a layout container. My solution is admittedly more than a bit hackish, but - it works.
1) Set Padding left to 40 2) Set Layout margin left to -36dp
At this point, the original radio button will be outside the view, and your text view will be sitting on the far left with a 4dp margin.
3) Set Drawable right to @android:drawable/btn_radio
You'll now have a native RadioButton with the text on the left and a button on the right, that will work with a RadioGroup.
@CommonsWare
It's worth mentioning that it's incredibly ironic to bring up Human Interface Guidelines in response to this particular question. Especially considering that adjusting the RadioButton layout to place the button on the far right, would achieve consistency with the layout of the Spinner menu. I completely agree with your opinion on the matter - but it's quite possible that NickTFried was trying to compensate for Android "hanging" itself in that regard.
Solution 2:
As Ravi Vyas indicates, you can do this yourself with a TextView
and a RadioButton
. There is nothing intrinsic to RadioButton
to reposition the button relative to the text, from my reading of the source code.
Also, please bear in mind that just because this is possible does not mean that it is a good idea. For example, on iPhone, you might not be allowed to ship your app if you mess around with this too much, because they have human interface guidelines that apps must adhere to. Android gives you a lot more rope -- don't hang your users with it.
Solution 3:
The CheckedTextView
works fine if you're working with a single "radio button".
But I needed to retain the toggling functionality among radio buttons in a group so this worked better for me:
<RadioGroupandroid:id="@+id/radioGroup1"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/radioButton1"android:layout_width="match_parent"android:layout_height="wrap_content"android:button="@null"android:drawableRight="@android:drawable/btn_radio"android:gravity="left|center_vertical"android:layout_marginLeft="-32dp"android:text="Radio Button 1" /><RadioButtonandroid:id="@+id/radioButton2"android:layout_width="match_parent"android:layout_height="wrap_content"android:button="@null"android:drawableRight="@android:drawable/btn_radio"android:gravity="left|center_vertical"android:layout_marginLeft="-32dp"android:text="Radio Button2" /></RadioGroup>
Adjust the android:layout_marginLeft
as necessary.
Solution 4:
For RTL languages i.e Arabic
use this:
<RadioGroupandroid:id="@+id/rgNewsFilter"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/rbAllNews"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="-32dp"android:button="@null"android:drawableRight="@android:drawable/btn_radio"android:gravity="right|center_vertical"android:text="ذنيسبمنشخصث"android:textColor="#ffffff" /><RadioButtonandroid:id="@+id/rbMyTeam"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="-32dp"android:button="@null"android:drawableRight="@android:drawable/btn_radio"android:gravity="right|center_vertical"android:text="تشسيبتسيتبتسيب"android:textColor="#ffffff" /></RadioGroup>
Solution 5:
This can be done using CheckedTextView
; see android RadioButton option on the right side of the text
Post a Comment for "Change The Side The Text Appears On A Radio Button"