RadioButton in multirow in single RadioGroup - android

I need to create design like radiobutton in multi-row in single radiogroup like this image .
I am using android:orientation="horizontal"
layout.xml
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:singleLine="false"
android:text="option1" />
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:singleLine="false"
android:text="option2" />
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:singleLine="false"
android:text="option3" />
<RadioButton
android:id="#+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:singleLine="false"
android:text="option4" />
<RadioButton
android:id="#+id/option5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:singleLine="false"
android:text="option5" />
<RadioButton
android:id="#+id/option6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:singleLine="false"
android:text="option6" />
</RadioGroup>
Thanks..

parent can be any like linear horizontal or relative(i'd prefer Linear Horizontal), inside that 3 TableRow childs,within each tableRow your RadioButtons. something like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio3"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio4"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio5"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio6"/>
</LinearLayout>
</RadioGroup>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio3"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio4"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio5"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio6"/>
</LinearLayout>
</RadioGroup>
</TableRow>

What you are trying to achieve is possible.
You will have to subclass TableLayout and add the radio buttons programmatically
here is the link
refer this MATRIX RADIO BUTTONS
For screen width
Screen Width

Related

How to adjust space between radio button in android

In my android application using radio buttons to select screen view. I want radio button equally aligned horizontal. I tried layout xml shown below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Views"
android:paddingLeft="10dp"
android:textSize="20sp"
/>
<RadioGroup
android:id="#+id/radioView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:measureWithLargestChild="true"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioViewSingle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/single"
android:layout_weight="1"
android:checked="true" />
<RadioButton
android:id="#+id/radioView2by2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view2x2" />
<RadioButton
android:id="#+id/radioView3by3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view3x3" />
<RadioButton
android:id="#+id/radioView4by4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableRight="#drawable/view4x4" />
</RadioGroup>
</LinearLayout>
output obtained using above xml code
But i expecting below output
Thanks in advance
Instead of using android:drawableRight, use android:drawableLeft.
I just tested this and it is working as expected
Remove android:layout_weight="1" for all RadioButtons and set android:layout_width="0dp" to android:layout_width="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="Views"
android:textSize="20sp" />
<RadioGroup
android:id="#+id/radioView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioViewSingle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#android:drawable/ic_menu_search" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView2by2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:drawableRight="#android:drawable/ic_menu_camera" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView3by3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#android:drawable/ic_menu_always_landscape_portrait" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left">
<RadioButton
android:id="#+id/radioView4by4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dp"
android:drawableRight="#android:drawable/ic_menu_call" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
You can put each RadioButton to one layout and give all layout_width="0dp" and give layout_weight="1" as showen below:
<RadioGroup
android:id="#+id/radioView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:checked="true"
android:drawableRight="#drawable/single"
android:id="#+id/radioViewSingle"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view2x2"
android:id="#+id/radioView2by2"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view3x3"
android:id="#+id/radioView3by3"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
<RadioButton
android:drawableRight="#drawable/view4x4"
android:id="#+id/radioView4by4"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
</RadioGroup>

how to allow only one radiobtton to be executed in a radiogroup inside a linear layout?

I am trying to place a radio group with 3 radio buttons inside a linear layout. My aim is to choose only one RadioButton inside the RadioGroup but, it is giving me an error that there are multiple root tags. Can someone figure out how to solve this?
file.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ok"
android:id="#+id/textemail"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<RadioButton
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
/>
<RadioButton
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
</RadioGroup>
<TextView
android:id="#+id/textView1"
android:layout_below="#+id/textemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To:"
/>
<TextView
android:id="#+id/TextTo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="khushi"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment:"
/>
<TextView
android:id="#+id/TextS3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="your comment"
/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="email us "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10" />
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send"
android:textStyle="italic"/>
</LinearLayout>
You immediately close your radiogroup:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"/>
This should be:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"> // Note that there is no forward slash at the end
And you are not closing the RadioGroup:
</RadioButton> <-- This should be </RadioGroup>
Your RadioButton (views) should be children of the RadioGroup(view). By closing your RadioGroup using /> you declare the RadioButton(s) and the RadioGroup as children of the LinearLayout.
The Following should work.
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:id="#+id/textview"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<RadioButton
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
/>
<RadioButton
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
</RadioButton>
</RadioGroup>
<TextView
android:id="#+id/textViewllol"
android:layout_below="#+id/textemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi:"
/>
<TextView
android:id="#+id/Texme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="comment:"
/>
<TextView
android:id="#+id/Text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="your comments"
/>
<TextView
android:id="#+id/textView45"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
/>
<EditText
android:id="#+id/editTextMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:lines="10" />
<Button
android:id="#+id/button4785"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="send"
/>
</LinearLayout>
Change below -
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"/>
to
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<RadioButton
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
/>
<RadioButton
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
</RadioGroup>
One of your Text is not in correct format change it as below -
<TextView
android:id="#+id/textView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok:"
/>
to
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok:"
/>
One more error -
change below -
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
to
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
You have one </Radiobutton> tag above </Radiogroup> that shouldn't be here. Just remove it to have :
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<RadioButton
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
/>
<RadioButton
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
</RadioGroup>

Edittext inside radiogroup

I'm trying to get layout as below:
|--r1--|---EditText---|
|--r2--|---TextView---|
the widgets at "r1" and "r2" position is RadioButton,
I wish:
r1 and r2 should be in same RadioGroup.
I am not able to place EditText next to r1.
textview next to r2 is easy.
Can some one help?
LinearLayout 1 - Horizontal
linear layout 2 - Vertcal
RadioGroup with you radio buttons
close linear layout 2
linear layout 3 - Vertical
edit Text here
close linear layout 3
close LinearLayout 1
Hope this structure could help.
The code..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Thanks!
Try this layout
<?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="match_parent"
>
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/radioButton1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="#+id/radioButton1"
android:ems="10" >
<requestFocus />
</EditText>
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/radioButton1"
android:layout_marginTop="16dp"
android:text="" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/radioButton2"
android:layout_alignBottom="#+id/radioButton2"
android:layout_alignLeft="#+id/editText1"
android:layout_alignRight="#+id/editText1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
</RadioGroup>
</RelativeLayout>
It will be look like this
Here it is :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample text" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample text" />
</RadioGroup>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/dp_8">
<RadioGroup
android:id="#+id/rg_ends_after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="#+id/rb_until_cancelled_never"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/txt_until_cancelled_never" />
<RadioButton
android:id="#+id/rb_after_occurrence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="#string/txt_after"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RadioGroup>
<LinearLayout
android:id="#+id/layout_occurrence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/rg_ends_after"
app:layout_constraintStart_toStartOf="parent">
<RadioButton
android:id="#+id/rb_after_occurrence_dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:visibility="invisible"
android:text="#string/txt_after" />
<EditText
android:id="#+id/et_occurrence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="number"
android:maxLength="2"
android:ems="2"
android:text="#string/number" />
<TextView
android:id="#+id/txt_occurrence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="#string/txt_occurrence" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use "GridLayout" to achieve. I have done in my programm.
Use this xml layout
<RelativeLayout>
<RadioGroup>
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" />
</RelativeLayout>
and this code
radioButton is a variable for any RadioButton you want to attach EditText
radioButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
radioButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)editText.getLayoutParams();
params.leftMargin = radioButton.getLeft() + radioButton.getWidth() + littleSpacing;
params.topMargin = radioButton.getTop() + radioButton.getBaseline() - editText.getBaseline();
}
});

Great distribution of horizontal radio buttons

I have 3 RadioButton in one line (linearLayout). I want to distribute them properly in the line, and not align all of them to the left, just as in this screenshot.
Currently it is displayed like this:
XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/linearLayout2"
android:layout_alignLeft="#+id/linearLayout2" android:id="#+id/linearLayout3">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:id="#+id/radioButton3" android:layout_gravity="center_horizontal|top" android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maybe"
android:id="#+id/radioButton" android:layout_gravity="center_horizontal|top" android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:id="#+id/radioButton2" android:checked="false"/>
</RadioGroup>
</LinearLayout>
It is possible for classic buttons with 0dp (see here), but RadioButtons are invisible with this setting.
Is there an easy way to do that ?
Try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/linearLayout2" >
<RadioGroup
android:layout_width="fill_parent"
android:weightSum="3"
android:layout_height="fill_parent" android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Yes"
android:id="#+id/radioButton3" android:checked="false"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Maybe"
android:layout_weight="1"
android:id="#+id/radioButton" android:checked="true"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="No"
android:id="#+id/radioButton2" android:checked="false"/>
</RadioGroup>
</LinearLayout>
Then you must have to use layout weight on you radio button:-
android:layout_weight beginner's question
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#+id/linearLayout2"
android:layout_alignLeft="#+id/linearLayout2" android:id="#+id/linearLayout3">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yes"
android:id="#+id/radioButton3" android:layout_gravity="center_horizontal|top" android:checked="false"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Maybe"
android:id="#+id/radioButton" android:layout_gravity="center_horizontal|top" android:checked="true"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No"
android:id="#+id/radioButton2" android:checked="false"/>
</RadioGroup>
</LinearLayout>

Radio Button with image as an option instead of text

How can i add radio buttons in android app with option as an image , not the text.. we can set the text to radio button by android:setText property. But i want an image to be displayed there and not the text.. Please help me?
How about this one using the property android:drawableRight?
<RadioGroup
android:id="#+id/maptype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/line1"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:drawableRight="#drawable/map" />
<RadioButton
android:id="#+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/sat" />
</RadioGroup>
use this:
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</RadioGroup>

Categories

Resources