0

I want to add radio buttons such that only one of them is selected at a time among 4 buttons and I want to place them as:

        RadioButton1  RadioButton2
        RadioButton3  RadioButton4

I am trying the following code but 1&2 forms a grp and 3&4 forms a different grp and there are two value selected a time. Can anyone check it and share the correct way to do it?

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    >

    <RadioGroup 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rdogrp_main"
        android:orientation="vertical"
        >

    <RadioGroup 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rdogrp1"
        android:orientation="horizontal"            
        >

    <RadioButton 
        android:id="@+id/RadioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Orange"
        android:textColor="#000000"
        />

    <RadioButton 
        android:id="@+id/RadioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="BlueBerry"
        android:textColor="#000000"
        />
    </RadioGroup>

    <RadioGroup 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rdogrp2"
        android:orientation="horizontal"            
        >
    <RadioButton 
        android:id="@+id/RadioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apple  "
        android:textColor="#000000"
        />

    <RadioButton 
        android:id="@+id/RadioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="Santra :P"
        android:textColor="#000000"
        />
    </RadioGroup>

    </RadioGroup>
</LinearLayout>
Amrit
  • 2,295
  • 4
  • 25
  • 42

3 Answers3

0

For that you need to create only one RadioGroup and add four different RadioButton. And for adding UI refer this url : Manage Layout Inside a Horizontal Oriented Radiogroup .

I hope this will help you.

Community
  • 1
  • 1
Naresh J
  • 2,087
  • 5
  • 26
  • 39
0

Try Like this..

         <?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" >
<RadioGroup
    android:id="@+id/rdogrp_main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/RadioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.20"
        android:text="India"
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/RadioButton2"
        android:layout_width="74dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.57"
        android:text="Austraila"
        android:textColor="#000000" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/RadioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="Srilanka  "
        android:textColor="#000000" />

    <RadioButton
        android:id="@+id/RadioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.48"
        android:text="SouthAfrica"
        android:textColor="#000000" />
</LinearLayout>

 </RadioGroup>

 </LinearLayout>

Let me know if you solve the problem...

Make it Simple
  • 1,832
  • 5
  • 32
  • 57
  • Thanks for your time but it still selects all the radiobutton irrespective of its having only one grp. – Amrit May 14 '13 at 09:27
0

Ok I find a solution for you:

I know it is not the best solution but it works! :)

Just copy the xml file and activity code bellow:

Activity:

package com.example.stackoverflow;

import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    private static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RadioButton radiobutton1 = (RadioButton) findViewById(R.id.RadioButton1);
        final RadioButton radiobutton2 = (RadioButton) findViewById(R.id.RadioButton2);
        final RadioButton radiobutton3 = (RadioButton) findViewById(R.id.RadioButton3);
        final RadioButton radiobutton4 = (RadioButton) findViewById(R.id.RadioButton4);

        radiobutton1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton1.setChecked(true);
                radiobutton2.setChecked(false);
                radiobutton3.setChecked(false);
                radiobutton4.setChecked(false);
                return true;
            }
        });

        radiobutton2.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton2.setChecked(true);
                radiobutton1.setChecked(false);
                radiobutton3.setChecked(false);
                radiobutton4.setChecked(false);
                return true;
            }
        });

        radiobutton3.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton3.setChecked(true);
                radiobutton1.setChecked(false);
                radiobutton2.setChecked(false);
                radiobutton4.setChecked(false);
                return true;
            }
        });

        radiobutton4.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                radiobutton4.setChecked(true);
                radiobutton1.setChecked(false);
                radiobutton2.setChecked(false);
                radiobutton3.setChecked(false);
                return true;
            }
        });

    }

}

activity_main.xml :

<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=".MainActivity" >

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#FFDDDDDD" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/rdogrp1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/RadioButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Orange"
                    android:textColor="#000000" />

                <RadioButton
                    android:id="@+id/RadioButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:text="BlueBerry"
                    android:textColor="#000000" />
            </RadioGroup>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/rdogrp2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/RadioButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Apple  "
                    android:textColor="#000000" />

                <RadioButton
                    android:id="@+id/RadioButton4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:text="Santra :P"
                    android:textColor="#000000" />
            </RadioGroup>
        </TableRow>
    </TableLayout>


</RelativeLayout>
Jarvis
  • 1,527
  • 12
  • 17