smriti3
smriti3

Reputation: 871

Radiobutton functionality in a android

XML

<LinearLayout
            android:id="@+id/linearLayout_individualdays"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_below="@+id/linearLayout_type_of_days"
            android:gravity="center|top"
            android:orientation="horizontal"
            android:paddingLeft="5dp"
            android:paddingTop="10dp" >

            <RadioGroup
                android:id="@+id/radioGroup2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:visibility="gone"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/BreakfastRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:checked="true"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Mon"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/LunchRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Tue"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Wed"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/BreakfastRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:checked="true"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Thu"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/LunchRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Fri"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Sat"
                    android:textSize="15dp" />

                <RadioButton
                    android:id="@+id/DinnerRG_ID"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="2dp"
                    android:layout_weight=".5"
                    android:background="@drawable/yourbuttonbackground"
                    android:button="@android:color/transparent"
                    android:gravity="center_horizontal"
                    android:padding="5dp"
                    android:text="Sun"
                    android:textSize="15dp" />
            </RadioGroup>
        </LinearLayout>

enter image description here

enter image description here


enter image description here

enter image description here


How to resolve this, Hope i am clear

Upvotes: 0

Views: 116

Answers (3)

Gunaseelan
Gunaseelan

Reputation: 15525

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="checkBox1" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="checkBox2"
    android:checked="true" /> 

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Check" />

</LinearLayout>

Code:

public class MainActivity extends Activity {

private checkBox1,checkBox2;
private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
    btn = (Button) findViewById(R.id.button1);
    checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

    btn.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        StringBuffer result = new StringBuffer();
        result.append("checkBox1 : ").append(checkBox1.isChecked());
        result.append("checkBox2 : ").append(checkBox2.isChecked());

        Toast.makeText(MainActivity.this, result.toString(),
            Toast.LENGTH_LONG).show();

  }
});

 }
}

I hope this will help you.

Upvotes: 1

SathMK
SathMK

Reputation: 1171

Radiobutton in wikipedia says

A RadioButton or option button is a type of graphical user interface element that allows the user to choose only one of a predefined set of options.

You would have to just use checkboxes instead of radio button for allowing multiple options to be selected,else you are doing something which is inherently wrong in a UI be it mobile/web.

If still want to use RadioButtons, don't put them all in a RadioGroup. Either put them in several groups or manage them yourself completely.

Upvotes: 2

Dhaval Pandya
Dhaval Pandya

Reputation: 1617

You can try the layout as follows :

None of the Radio buttons will be selected at launch, but you can select them individually.

You just had to put them outside RadioGroup.

But You won't be able to de-select those Radio buttons, if selected mistakenly.

Checkboxes are a Proper way to go for such a thing.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/orange"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

</LinearLayout>

Upvotes: 2

Related Questions