I have been trying to keep only one of the radio buttons checked but every time the user is able to select multiple radio buttons.
How can I keep just a single or the current selected radio button checked?
I basically have a TableLayout
which has a TableRow
as an XML layout being inflatd from code. Also, an onClickListener
is applied on the RadioButton which fetches the tag
for the current selected Radio Button.
Can someone please help me out with this?
For ref. my Table Row's XML Layout:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tblDynStoreRow"
android:layout_margin="0.0dp"
android:layout_width="match_parent" >
<TextView
android:id="@+id/tvStoreCount"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:singleLine="true"
android:scrollHorizontally="true" />
<RadioButton
android:id="@+id/rb1StoreName"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="31dp"
android:singleLine="true"
android:scrollHorizontally="true" />
</TableRow>
onCreate() method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_selection);
TableLayout tl = (TableLayout) findViewById(R.id.mainstoretable);
//RadioButton rb1;
TextView TVdate = (TextView) findViewById(R.id.textView1_date2);
Intent getStorei = getIntent();
// UsrGPSLock = i.getStringExtra("currUsrLat") + "," +
// i.getStringExtra("currUsrLon");
uuid = getStorei.getStringExtra("imei").trim();
userDate = getStorei.getStringExtra("userDate");
TVdate.setText(userDate);
/*TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
uuid = tManager.getDeviceId();*/
Button btnStart = (Button)findViewById(R.id.startQues);
dbengine.open();
storeList = dbengine.FetchStoreList();
dbengine.close();
/*final String[] storeCode = new String[storeList.length];
final String[] storeName = new String[storeList.length];*/
storeCode = new String[storeList.length];
storeName = new String[storeList.length];
for (int splitval = 0; splitval <= (storeList.length - 1); splitval++) {
StringTokenizer tokens = new StringTokenizer(String.valueOf(storeList[splitval]), "_");
storeCode[splitval] = tokens.nextToken().trim();
storeName[splitval] = tokens.nextToken().trim();
}
System.out.println(storeList);
// Get the TableLayout
/*TableLayout tl = (TableLayout) findViewById(R.id.maintable);*/
LayoutInflater inflater = getLayoutInflater();
// Go through each item in the array
for (int current = 0; current <= (storeList.length - 1); current++) {
TableRow storeRow = (TableRow)inflater.inflate(R.layout.store_table_row, tl, false);
TextView tv1 = (TextView)storeRow.findViewById(R.id.tvStoreCount);
final RadioButton rb1 = (RadioButton)storeRow.findViewById(R.id.rb1StoreName);
//tv1.setTag(current);
tv1.setText((""+(current+1)).trim());
//tv1.setId();
rb1.setTag(storeCode[current]);
rb1.setText(storeName[current]);
tl.addView(storeRow);
/*// Create a TableRow and give it an ID
tr = new TableRow(this);
tr.setId(200 + current);
tr.setTag(storeCode[current]);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.setClickable(true);
rb1 = new RadioButton(this);
rb1.setId(current);
rb1.setTag(storeCode[current]);
rb1.setText(storeName[current]);
rb1.setTextColor(Color.BLACK);
rb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.addView(rb1);
CheckBox cb1 = new CheckBox(this);
cb1.setId(500 + current);
// cb1.setText(provinces[current]);
cb1.setTextColor(Color.BLACK);
cb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.addView(cb1);
* CheckBox cb2 = new CheckBox(this); cb2.setId(300+current);
* //cb1.setText(provinces[current]); cb2.setTextColor(Color.BLACK);
* cb2.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,
* LayoutParams.WRAP_CONTENT)); tr.addView(cb2);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));*/
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("inside-onClick");
//System.out.println(arg0.getId());
System.out.println(arg0.getTag());
selStoreID = arg0.getTag().toString();
if(prevSel == 0) {
prevSelStoreID = selStoreID;
prevID = arg0.getId();
prevSel = 1;
}
else{
}
/*rb1.refreshDrawableState();*/
if(prevSelStoreID.equals(arg0.getTag().toString())){
/*int rbID;
rbID = rb1.getId();
System.out.println("RB ID: " + rbID);*/
}
else{
int rbID;
rbID = arg0.getId();
//rb1.refreshDrawableState();
//rb1.clearFocus();
//rb1.findViewById(rbID).isSelected();
rb1.findViewById(prevID).clearFocus();
rb1.findViewById(prevID).invalidate();
rb1.findViewById(prevID).refreshDrawableState();
System.out.println("prevID: " + prevID);
System.out.println("rbID: " + rbID);
rb1.setChecked(false);
//rb1.findViewWithTag(prevSelStoreID).invalidate();
prevSel = 0;
prevSelStoreID = selStoreID;
prevID = rbID;
rb1.findViewById(rbID).isSelected();
}
}
});
}
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent nxtP3 = new Intent(StoreSelection.this, LastVisitDetails.class);
nxtP3.putExtra("storeID", selStoreID);
nxtP3.putExtra("imei", uuid);
nxtP3.putExtra("date", userDate);
startActivity(nxtP3);
}
});
}
To prevent multiple checking of RadioButton they must be all within the same RadioGroup.
Either you define a Radiogroup in your XML (but from what I see you are trying to do it will be difficult)
or you create it in Java: RadioGroup rg = new RadioGroup(this);
EDIT EDIT EDIT
Try this...hope it works
RadioGroup rg = new RadioGroup(this);
RadioButton[] rb = new RadioButton[storeList.length];
for (int current = 0; current <= (storeList.length - 1); current++) {
TableRow storeRow = (TableRow)inflater.inflate(R.layout.store_table_row, tl, false);
TextView tv1 = (TextView)storeRow.findViewById(R.id.tvStoreCount);
rb[current] = (RadioButton)storeRow.findViewById(R.id.rb1StoreName);
rg.addView(rb[current]);
/*other suff...