-1

It maybe a very simple question but i am beginner in Android please help with it.

i have created two RadioButtons in XML without RadioGroup and in MainActivity.java created a RadioGroup, how can i add both RadioButtons in that RadioGroup? Code is below...

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.avisingh.radiobuttontest.MainActivity">

<RadioButton
    android:id="@+id/male_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Male"/>

<RadioButton
    android:id="@+id/female_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="Female"/>

</RelativeLayout>

MainActivity.java

package com.avisingh.radiobuttontest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    final RadioButton maleBtn = (RadioButton)findViewById(R.id.male_button);
    final RadioButton femaleBtn = (RadioButton)findViewById(R.id.female_button);
    RadioGroup radioGroup = new RadioGroup(this);

    //radioGroup.add(maleBtn); // its getting error
    //radioGroup.add(femaleBtn); // its getting error
    //radioGroup.addView(maleBtn); // it is also getting error
    //radioGroup.addView(femaleBtn); // it is also getting error

    }
}

is there any method too add both RadioButtons like java?

Avinash Kumar
  • 288
  • 6
  • 21

2 Answers2

1

you can add 'RadioGroup' inside the 'activity_main.xml'

<?xml version="1.0" encoding="utf-8"?>
<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="com.avisingh.radiobuttontest.MainActivity">
<RadioGroup
    android:id="@+id/radioSex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
<RadioButton
    android:id="@+id/male_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Male"/>

<RadioButton
    android:id="@+id/female_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="Female"/>
</RadioGroup>

Supun Kavinda
  • 308
  • 1
  • 6
  • 20
1

Here is how you can do it

RadioButton radioButton = new RadioButton(this);
    radioButton.setText("Test Radio");
    radioButton.setId(1);
    radioGrp.addView(radioButton);

if its too many try putting it into loop

 for (int i = 0; i < radioItems.length; i++) {
        RadioButton radioButton = new RadioButton(this);
        radioButton.setText(radioItems[i]);
        radioButton.setId(i);
        radioGrp.addView(radioButton);
    }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • 1
    but is it not a new `RadioButton` ? i only want to add these `RadioButton`s that i have created in XML without `RadioGroup` – Avinash Kumar Aug 15 '17 at 18:18
  • Then instead of radio group use linear layout or relative layout and call addView() on it. Because in anyway you have to add your radio button under some parent view. – vikas kumar Aug 15 '17 at 18:25