Have horizontal RadioButtons wrap if too long for screen - android

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.

Related

how to rearrange my textview in my relativelayout?

Here's my layout screenshot:
How can I arrange all these TextView for them to have a good format (heading being Taking responsibility (means being aware)..) alignment etc.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Menu"
android:background="#android:color/holo_blue_bright"
>
<TextView
android:text="*Follow safety guide for moving around the town and between towns."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView25"
android:gravity="left"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="51dp" />
<TextView
android:text="*Avoid crowds and do not participate actively in demonstrations even when it is related to programme work."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView25"
android:gravity="left"
android:layout_below="#+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp" />
<TextView
android:text="*Avoid crowds and do not participate actively in demonstrations even when it is related to programme work."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView25"
android:gravity="left"
android:layout_marginBottom="23dp"
android:layout_above="#+id/textView25"
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25" />
<TextView
android:text="*keep this briefing pack in an accessible place, and the emergency numbers in your mobile phone memory"
android:layout_width="wrap_content"
android:gravity="bottom"
android:layout_height="wrap_content"
android:id="#+id/textView28"
android:layout_above="#+id/textView25"
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25"
android:layout_marginBottom="46dp" />
<TextView
android:gravity="center"
android:textColor="#000"
android:textSize="15dp"
android:text="Taking resposibility(means being aware)"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView22"
android:layout_marginTop="18dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="*Avoid moving around town by yourself and always make others aware of your location."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:id="#+id/textView25"
android:layout_marginTop="76dp"
android:layout_below="#+id/textView22"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="*Share any security information that might have implications for Ballloon Ventures to the Country Manager or pc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView24"
android:gravity="left"
android:layout_below="#+id/textView26"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp" />
<TextView
android:text="*If using host home accommondation,check that it is secure.if it is not,tell the Programme Coordinator"
android:layout_width="wrap_content"
android:gravity="bottom"
android:layout_height="wrap_content"
android:id="#+id/textView28"
android:layout_marginTop="14dp"
android:layout_below="#+id/textView24"
android:layout_alignRight="#+id/textView25"
android:layout_alignEnd="#+id/textView25" />
<TextView
android:text="*Report all incidents to your Programmer Cordinator."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView26"
android:gravity="left"
android:layout_below="#+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />
</RelativeLayout>
Because if I drag one TextView it affecting all other TextView.
As #GabeSechan said, you need to learn what android:layout_xxx properties in RelativeLayout means.
When you moving a View in RelativeLayout via Layout Editor, you need to consider some of the following properties in your layout:
android:layout_above="#+id/textView25"
android:layout_below="#+id/textView24"
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/textView25" means that you want the View above textView25 view.
android:layout_below="#+id/textView24" means that you want the View below textView25 View.
The following properties means that you want the view to the right side of TextView25 View:
android:layout_toRightOf="#+id/textView25"
android:layout_toEndOf="#+id/textView25"
The following properties means that you want the view to align with the left side of the parent layout:
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
If you want to edit views position in a RelativeLayout, please don't depends with drag and drop via Layout Editor. You better do it manually except you want to have a headache later. Use Layout Editor to view the design result.
Design with a paper or imagination first, then code it after that.
Try to learn it from:
Relative Layout
Constructing View Layouts
The best practice for doing that would be to open a splitview between your code and design views and then editing the code to place the text in the correct location, using attributes such as padding, margin, aligning, below, above, etc. refer to this guide for positioning stuff in relative layout and combine it with other formatting attributes such as margin and padding.

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:

How to reduce space between radiobuttons in a radiogroup in android?

In my app, I have a dialog that contains a radiogroup with four radio buttons. My issue is that it looks good in emulator but when I install and run my app in phone, the radiobuttons get more space between each one of them. This makes the radiogroup stretch outside of my Dialog.
Please help me with this.
Thank you.
<RadioGroup
android:id="#+id/radio_RemindAtDlg"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_marginTop="110dp"
android:layout_marginLeft="20dp" >
<RadioButton
android:id="#+id/radio_OnceDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Once"
android:button="#drawable/radio_button"
android:textSize="12dp" />
<RadioButton
android:id="#+id/radio_WeekDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Every Week"
android:button="#drawable/radio_button"
android:textSize="12dp" />
<RadioButton
android:id="#+id/radio_MonthDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Every Month"
android:button="#drawable/radio_button"
android:textSize="12dp" />
<RadioButton
android:id="#+id/radio_YearDlg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Every Year"
android:button="#drawable/radio_button"
android:textSize="12dp" />
</RadioGroup>
Define your own radio buttons with your own sizes, shapes and spaces between components:
http://blog.devminded.com/posts/custom-android-radiobutton
another example
https://stackoverflow.com/a/17693303/1276374
Without code hard to tell but I would guess that you have the xml height attribute set to fill_parent instead of wrap_content

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