Android RadioButton like Behaviour - android

Greetings,
I'm trying to create a single-choice android control, in a horizontal layout, by making use of the RadioGroup behaviour. I can assign the drawable just fine, but i would like to position the label of each RadioButton inside the drawable, is this possible using the standard APIs?
<RadioGroup
android:id="#+id/switchcontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:checkedButton="#+id/RadioButton02"
android:padding="3dip">
<RadioButton
android:text="id RadioButton02"
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radio_button"
android:paddingRight="2dip" />
<RadioButton
android:text="#+id/RadioButton03"
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radio_button"
android:paddingRight="2dip" />>
</RadioGroup>

Okay, i found a way.
Just use #null in the attribute button, and move the drawable reference to the attribute background like this:
<RadioGroup
android:id="#+id/switchcontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:checkedButton="#+id/RadioButton02">
<RadioButton
android:text="id RadioButton02"
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/radio_button"
android:button="#null"/>
<RadioButton
android:text="#+id/RadioButton03"
android:id="#+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/radio_button"
android:button="#null"/>
</RadioGroup>

I'm not sure about with the RadioGroup; however, it should be possible to accomplish this with a ListView.
Set the ListView choiceMode to singleChoice.
Provide an ArrayAdapter or SimpleAdapter with a custom layout file for the row. This allows you to configure the position of the label.
For a quick intro to ListView's, see Hello ListView

Related

How to disable radiobutton's text from clickable feature?

I need to select radio button while touching only the icon of a radio button. By default when i touch on the radio button's text, the radio button also get checked. How can i disable the feature?
<RadioGroup
android:id="#+id/optionGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
</RadioGroup>
To answer your question, you can't do it using the default radiobutton class.
The android-radiobutton is an object of the class which actually makes the radiobutton itself. This class sets the properties of the RadioButton, and, to a certain extent, you cannot change these properties. You can, however, make it unclickable, change the color, set the text, however, you cannot change what portion of the button is clickable. To do that, you must make your own class of a RadioButton, and extend it to the class that you are using.
Only way is by making your own class. Let me know if this answered your question.
:)
Try this simple hack
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"/>
</LinearLayout>

Radio Group android

I started to develop for Android few days ago and I got stuck trying to use Radio Group. In my code for some reason all of the radio buttons can be selected together.
Any suggestion?
xml:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rG">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="89dp"
android:layout_below="#id/title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="95"
android:layout_marginLeft="75sp"
android:layout_marginTop="20sp"
android:id="#+id/rb95"
android:layout_gravity="left|top"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="98"
android:id="#+id/rb98"
android:layout_gravity="right|top"
android:layout_alignTop="#+id/rb95"
android:layout_alignLeft="#+id/rbs"
android:layout_alignStart="#+id/rbs"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="96"
android:id="#+id/rb96"
android:layout_gravity="left"
android:layout_below="#id/rb95"
android:layout_alignLeft="#id/rb95"
android:layout_alignStart="#id/rb95"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="soler"
android:id="#+id/rbs"
android:layout_gravity="right"
android:layout_marginRight="75dp"
android:layout_alignTop="#id/rb96"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</RadioGroup>
Pull the RadioButtons out of the RelativeLayout (get completely rid of it: it's not only misplaced, but also useless) and they will work as expected.
If you really need the RelativeLayout as a container for the RadioGroup, then swap the RelativeLayout and RadioGroup (RelativeLayout on the outside)
I think your problem is that you have a relativeLayout inside your RadioGroup.
Here is the Google Developer page on radio buttons.

Have horizontal RadioButtons wrap if too long for screen

So I have the following radiobuttons. I want to have them display like this:
However this occurs:
How I can get it to display like above? I can move in the GUI editor in Eclipse it but it removes the RadioButton from the RadioGroup!Within the group, it ignores all other layout parameters.
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeBar"
android:layout_marginTop="43dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/privRadio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Everyone" />
<RadioButton
android:id="#+id/privRadio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="FriendOfFriends" />
<RadioButton
android:id="#+id/privRadio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friends" />
</RadioGroup>
You can simply copy this class:
https://github.com/jevonbeck/AbstractMachine/blob/jevon_dev/app/src/main/java/org/ricts/abstractmachine/ui/utils/MultiLineRadioGroup.java
into an appropriate package in your project and instantiate in XML like so:
<view
class="mypackage.packagepath.MultiLineRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
What you are asking for is a FlowLayout. Such a layout has the benefit of only wrapping when it's needed, as opposed to 0gravity's more "static" solution.

RadioGroup Right Aligned in Android

I have a textview on the left side and RadioGroup on the right side. Before I put my buttons in a radio group I had the buttons on the right side of the screen. What I am doing wrong after putting it in a RadioGroup?
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.25" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/radioButton1"
android:layout_alignParentLeft="true"
android:text="#string/hair"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/Yes" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioButton1"
android:layout_below="#+id/radioButton1"
android:text="#string/No" />
</RadioGroup>
</RelativeLayout>
All of the "align" parameters that you have set on the buttons individually (like layout_alignParentRight) are no longer valid because the buttons are inside of a RadioGroup, which is a subclass of LinearLayout. In order to right-align the group as a whole, you need to add the proper parameters to the RadioGroup itself.
Also, you may want the RadioGroup width to wrap_content instead of fill_parent. Otherwise any horizontal layout alignments you do will likely not be visible with the container trying to fill up all available space.
HTH
you can also add this to your RadioGroup and it will work fine aswell!
android:gravity="center|left"

howto group radio buttons in a custom adapter?

My question is similar to this question and since I was unable to find a satifying answer I would like to put up my question explicitly.
I have a custom adapter which takes an array of strings and has to populate the the list (defined in my ListActivity class) with that array. Now, the layout of each row has a text view and and a radiobutton as shown
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="#+id/word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/word"
android:textSize="30px">
</TextView>
<RadioButton
android:id="#+id/selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_alignParentRight="true"
android:layout_x="27px"
android:layout_y="8px">
</RadioButton>
Everything is working fine till this point. I am able to render each row with a radiobutton.
What I am looking for is a way in which I can group those radiobuttons together to perform a single selection.
This is required for an an application I am making in which has a question with four options as answers.
Also please tell me whether this is the right approach for this sort of a problem or not.
Thanks in advance
I could have been more clear as well. Anyhow, your final assembled XML needs to look like this. You could be doing this in code and that is fine as long as what you're building is returned as such. Each RadioButton must fall within the RadioGroup container.
<RadioGroup
android:id="#+id/rg_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<RadioButton
android:id="#+id/rb_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sales"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regional"
android:layout_above="#+id/rb_configup1"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Global"
android:layout_above="#+id/rb_configup2"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad-hoc"
android:layout_above="#+id/rb_configup3"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
</RadioGroup>
Does this make sense, or are you trying to do this in something other than XML?

Categories

Resources