howto group radio buttons in a custom adapter? - android

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?

Related

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.

Android RadioGroup - will it work with additional components within?

My idea is to have pairs ImageView - RadioButton, each pair enclosed in LinearLayout (horizontal orientation) and ImageButtons have their background color changed when an option is selected and confirmed. So, it would look like:
[LinearLayout1]: [ImageView1] [RadioButton1]
[LinearLayout2]: [ImageView2] [RadioButton2]
[LinearLayout3]: [ImageView3] [RadioButton3]
Is it possible to store this hierarchy within RadioGroup? I tried to apply this but see no ImageViews in my app. Or is RadioGroup built only for RadioButtons and all other views are ignored?
Yes, it's possible. If it's not working, there's something else wrong with your code.
A proof of concept layout based on the ApiDemos sample Android app included in the SDK:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="#+id/lunch"
android:id="#+id/menu">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_breakfast"
android:id="#+id/breakfast"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_star"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_lunch"
android:id="#id/lunch"/>
</LinearLayout>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_dinner"
android:id="#+id/dinner"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_all"
android:id="#+id/all"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_group_1_selection"
android:id="#+id/choice"/>
</RadioGroup>
and the layout it produces:

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.

Preference item-like layout

In main activity of my application I need a control which should look like ListPreference in PreferencesActivity. I mean, 2 lines: title and current value and down arrow icon on the right.
I've tried to use TwoLineListItem for this:
<TwoLineListItem android:id="#+id/twoLineListItem1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="onButtonClick" android:focusable="true"
android:background="#android:drawable/list_selector_background">
<TextView android:layout_width="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:id="#android:id/text1"
android:text="Upload into:" />
<TextView android:layout_below="#android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignLeft="#android:id/text1" android:layout_height="wrap_content"
android:id="#android:id/text2" android:text="<not set>"
android:layout_width="wrap_content" />
<!-- <ImageView android:layout_width="wrap_content" android:layout_alignParentRight="true"
android:id="#android:id/selectedIcon"
android:src="???"> </ImageView> -->
</TwoLineListItem>
It all works except of arrow icon: I can't find proper resource which system uses for it.
How this could be solved?
Or maybe I should use another control instead of TwoLineListItem?
Or maybe even try to implement my activity as PreferencesActivity (thought I need some EditText controls in my activity)?
For the reference, what I need is on the left picture:
http://androinica-serve.s3.amazonaws.com/wp-content/uploads/2010/11/evernote_comp2.png
wow, this question is so old and i can't believe no one answered it correctly lol. the layout you were looking for is in the sdk tree under:
[android-sdk-root]/platforms/[your-platform]/data/res/layout/preference.xml
you'll find this and any other sdk defined layouts in this folder. fish around in the res folder and see what's available. sorry it's a year late, but there ya go.
Use this layout and modify according to your need...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true">
<TextView android:layout_width="fill_parent"
android:text="ggggggggg"
android:layout_height="wrap_content" android:id="#android:id/text1"
/>
<TextView android:layout_below="#android:id/text1"
android:text="ssssssssssss"
android:layout_height="wrap_content"
android:id="#android:id/text2"
android:layout_width="wrap_content" />
</LinearLayout>
<ImageView android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#android:id/selectedIcon"
android:layout_height="wrap_content"
android:src="#drawable/icon"
/>
</RelativeLayout>
You might try using #android:drawable/btn_dropdown to get the system resource and setting that as the background.

Android RadioButton like Behaviour

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

Categories

Resources