Can i use RadioButtons without a RadioGroup? If not then please explain why? I am programming a layout for an android app in xml.
The Answer is Yes. you can used RadioButton without RadioGroup.But if you are using RadioButton without RadioGroup then you have doing Programming for that to make it perfect way.
I have posted both the Example so you can better understand.
Let's say this is your layout with RadioGroup.
layout.xml:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnMM"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnInch"
/>
To handle it selection the java code is below.
public class MainActivity extends Activity implements OnCheckedChangeListener {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main);
rb1 = (RadioButton) findViewById(R.id.radBtnInch);
rb1.setOnCheckedChangeListener(this);
rb2 = (RadioButton) findViewById(R.id.radBtnMM);
rb2.setOnCheckedChangeListener(this);
return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.radBtnInch) {
rb2.setChecked(false);
}
if (buttonView.getId() == R.id.radBtnMM) {
rb1.setChecked(false);
}
}
}
Let's say you used RadioGroup like below.
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="@+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true" />
then you can simple handle selection and display selection in Toast like this,
public class MainActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(Button)findViewById(R.id.button);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
}
});
}
RadioButton can be used without RadioGroup. RadioGroup is just a helper wrapper around RadioButtons to give you some capabilities like if you select a RadioButton, all other RadioButtons in that group will automatically be deselected. If you do without RadioGroup then you have to implement those functionalities yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With