It maybe a very simple question but i am beginner in Android please help with it.
i have created two RadioButton
s in XML without RadioGroup
and in MainActivity.java
created a RadioGroup
, how can i add both RadioButton
s 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 RadioButton
s like java?