Reputation: 113
i have a simple Activity, what i wanted to do is display 6 radio button in a single radio group
xml
<?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/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/adbms_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="10dp"
android:text="@string/adbms" />
<RadioButton
android:id="@+id/we_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:text="@string/we" />
<RadioButton
android:id="@+id/mp_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:text="@string/mp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/tcs_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tcs"
/>
<RadioButton
android:id="@+id/evs_radioBtn"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="56dp"
android:layout_height="wrap_content"
android:text="@string/evs" />
<RadioButton
android:id="@+id/cn_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="22dp"
android:text="@string/cn" ></RadioButton>
</TableRow>
</RadioGroup>
</LinearLayout>
but it is not working, means i can select all the radio button, but what i want is only one radio button should be selected, selecting another radio button should deselect the first one can any one help please?
i have coded this to implement radiogroup like functionality but it is not working as expected, whenever i am select multiple radiobuttons it is getting selected
package com.example.istudy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class AddMenuScreen extends Activity implements OnClickListener, OnCheckedChangeListener{
private RadioButton adbmsRadioBtn;
private RadioButton cnRadioBtn;
private RadioButton tcsRadioBtn;
private RadioButton evsRadioBtn;
private RadioButton mpRadioBtn;
private RadioButton weRadioBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_activity);
Button doneBtn = (Button) findViewById(R.id.doneBtn);
doneBtn.setOnClickListener(this);
adbmsRadioBtn = (RadioButton) findViewById(R.id.adbms_radioBtn);
weRadioBtn = (RadioButton) findViewById(R.id.we_radioBtn);
mpRadioBtn = (RadioButton) findViewById(R.id.mp_radioBtn);
evsRadioBtn = (RadioButton) findViewById(R.id.evs_radioBtn);
tcsRadioBtn = (RadioButton) findViewById(R.id.tcs_radioBtn);
cnRadioBtn = (RadioButton) findViewById(R.id.cn_radioBtn);
adbmsRadioBtn.setOnCheckedChangeListener(this);
cnRadioBtn.setOnCheckedChangeListener(this);
mpRadioBtn.setOnCheckedChangeListener(this);
weRadioBtn.setOnCheckedChangeListener(this);
evsRadioBtn.setOnCheckedChangeListener(this);
tcsRadioBtn.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.doneBtn:
saveData( );
// Toast.makeText(AddMenuScreen.this, "done btn clicked..", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
private void saveData() {
// TODO Auto-generated method stub
EditText chaptersCompletedTxtBox = (EditText) findViewById(R.id.chaptersCompletedTxtBox);
int chaptersCompleted = Integer.parseInt( chaptersCompletedTxtBox.getText().toString() );
if( adbmsRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, adbmsRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For ADBMS valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( weRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, weRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For WE valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( mpRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 8 ){
insertIntoDB(chaptersCompleted, mpRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For MP valid range is 1-7", Toast.LENGTH_SHORT).show();
}
}else if( evsRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, evsRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For EVS valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( tcsRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, tcsRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For TCS valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( cnRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, cnRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For CN valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}
}
private void insertIntoDB(int chaptersCompleted, String subName) {
// TODO Auto-generated method stub
DBHandler db = new DBHandler(this);
db.updateSem5Table(chaptersCompleted, subName);
Toast.makeText(AddMenuScreen.this, "Data saved successfully", Toast.LENGTH_SHORT).show();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
deselectAllRadioBtn();
buttonView.setChecked(true);
Toast.makeText(this, buttonView.getText().toString() + " is checked", Toast.LENGTH_SHORT).show();
}
private void deselectAllRadioBtn() {
// TODO Auto-generated method stub
adbmsRadioBtn.setChecked(false);
cnRadioBtn.setChecked(false);
evsRadioBtn.setChecked(false);
tcsRadioBtn.setChecked(false);
mpRadioBtn.setChecked(false);
weRadioBtn.setChecked(false);
}
}
Upvotes: 0
Views: 2883
Reputation: 274
It appears to me as though your RadioGroup contains the TableRows and not the RadioButtons. If you put the RadioButtons one level under RadioGroup that should work.
Upvotes: 1