In my app,i am creating radio buttons dynamically and want to uncheck all other radio buttons when one of them is checked.For this purpose,i am using RadioGroup.clearCheck() but it is not working at all.This is the code:
for (int i=0; i<files.length; i++)
{
rbi = new RadioButton(context);
rb1 = new RadioGroup(context);
rb1.addView(rbi);
nameOfFile = files[i].getName();
rbi.setText(nameOfFile);
ll.addView(rb1);
rbi.setOnClickListener(
new RadioButton.OnClickListener()
{
#Override
public void onClick(View v)
{
rb1.clearCheck();
rbi.setChecked(true);
}
Please help me.Even the alternate solutions for achieving the goal will be welcomed.Thanks in advance.
You create a lot of RadioGroups with just 1 RadioButton inside. That is probably not what you want. A RadioGroup needs to contain several RadioButtons so you can select an active button inside the list. See code below
// create 1 RadioGroup, add it to the layout
RadioGroup rg = new RadioGroup(context);
ll.addView(rg);
// add several RadioButtons to the RadioGroup
for (int i=0; i < files.length; i++) {
String nameOfFile = files[i].getName();
RadioButton rb = new RadioButton(context);
rb.setId(i); // assign an id
rb.setText(nameOfFile);
rg.addView(rb); // add to group
}
// do something when user checks a button
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// user selected files[checkedId].getName();
}
});
Did you try (with System.out.println("test");) or something, if it even gets into the clearing part? Would test that first.
This is what zapl means
rb1.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rb1,
int checkedId) {
Log.v("Selected", "New radio item selected: " + checkedId);
}
});
if that works, then you can try this:
if (rbi.isChecked()){
rb1.clearCheck();
rbi.setChecked(true);
}
Related
I had added radio buttons dynamically like below.
for (int i = 0; i< typeArrayList.size(); i++) {
radioButtons[i] = new RadioButton(MainActivity.this);
radioButtons[i].setId(i);
radioButtons[i].setText(typeArrayList.get(i).toString());
if(i==0) {
radioButtons[i].setChecked(true);
}
typeLayout.addView( radioButtons[i]);
}
I have a button when clicked calls a method to which the selected item (text) of the dynamically added radio buttons should be passed . How can I get the selected radio button text for the dynamically added radio buttons?
Its very important to set your radio button id when adding buttons to a radio group.
RadioButton rb = new RadioButton(context);
rb.setText(option);
rb.setId(rb.hashCode());
radioGroup.addView(rb);
radioGroup.setOnCheckedChangeListener(mCheckedListner);
Now in your click listener check for the unique id.
private RadioGroup.OnCheckedChangeListener mCheckedListner = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
JSONArray actionArray = new JSONArray();
if(group.findViewById(checkedId)!=null) {
RadioButton rb = ((RadioButton) group.findViewById(checkedId)).getText();
//Your Code here
}
}
}
};
You have stored all RadioButtons that you create dynamically in a radioButtons array.
So if you want to know which radiobutton is selected you can loop all this array and check each RadioButton
for (int i = 0; i< radioButtons.size(); i++) {
if(radioButtons[i].isChecked()){
// selected radio button is here
String text = radioButtons[i].getText();
}
}
First you have to get all childs of your view. Then check if this view is RadioButton. And at last check which button is checked.
int childcount = typeLayout.getChildCount();
for (int i=0; i < childcount; i++){
View view = typeLayout.getChildAt(i);
if (view instanceof RadioButton) {
if(((RadioButton)view).isChecked()) {
RadioButton yourCheckedRadioButton = (RadioButton) typeLayout.getChildAt(i); // this is your checked RadioButton
}
}
}
#Komali Shirumavilla
Add those dynamic radio buttons to radio group
so add following lines in your code
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for (int i = 0; i< typeArrayList.size(); i++) {
radioButtons[i] = new RadioButton(MainActivity.this);
radioButtons[i].setId(i);
radioButtons[i].setText(typeArrayList.get(i).toString());
if(i==0) {
radioButtons[i].setChecked(true);
}
rg.addView(radioButtons[i]); // add dynamic radio buttons to radio group
}
typeLayout.addView(rg); // add radio group to view
Now set setOnCheckedChangeListener on the RadioGroup
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
Log.d("Your selected radio button id",btn.getText());
}
}
});
This can be simply achieved using tags.
While adding the RadioButton set a tag that particular object. In your case, set the tag to be the text of the radio button,
radioButtons[i].setTag(typeArrayList.get(i).toString());
This way all your radio buttons will have the text associated to them as a tag.
Now whenever a particular radio button is selected, just get the tag which will give you the text associated with it.
String text = (String) selectedRadioButton.getTag();
Hope it helps.
For layout purposes I have three radiogroups with multiple selections in each group. How can I code so that only one choice is allowed, across all three groups?
In other words, right now I can make a selection in each group for a total of 3 selections. I want these to "see" each other and only allow one selection total.
Here's the method:
private RadioButton lastCheckedRB = null;
//whatever code
//onClicklistener for radiogroup does not work
RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
//OnCheckedChangeListener checks for reclick of a clicked rb, so the rest of the code is not executed
if (lastCheckedRB != null) {
lastCheckedRB.setChecked(false);
}
//store the clicked radiobutton
lastCheckedRB = checked_rb;
}
};
radioGroup1.setOnCheckedChangeListener(listener);
radioGroup2.setOnCheckedChangeListener(listener);
I am designing customize form programmatically where user can put a question and can add multiple options using radio buttons. I have taken RadioGroup and i am adding radio buttons in it. But while selecting i want only one radio button get selected at a time. How to implement it programmatically. Please help me..
Here is my code
final RadioGroup radioGroup = new RadioGroup(getApplicationContext());
radioGroup.setId(1);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
RadioButton radioButtonView = new RadioButton(getApplicationContext());
radioButtonView.setId(i++);
radioButtonView.setText(addnew);
radioGroup.addView(radioButtonView, p);
loption.addView(radioGroup, p);
Thanks in advance,
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged " + buttonView.getId());
mRadioGroup.clearCheck();
if (isChecked) {
mRadioGroup.check(buttonView.getId());
}
}
It seems that you are making a new RadioGroup for every RadioButton.
You should add every new RadioButton to the same RadioGroup. The RadioGroup will then make sure only one RadioButton can be selected at the time.
I have tried the same problem when I insert LinearLayout to put the radiobuttons in a grid.
I solved this way.
Considering that we have more RadioGroups, we know the RadioButton that was pressed. For this reason, just remove the listener from all of your RadioGroups and then deselect all the RadioButtons, resetting onCheckedChangeListener to your RadioGroup.
This is my code:
onCheckedChangeListener= new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, #IdRes int i) {
clearAllRadioButton();
((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
resetListenerRadioGroup();
}
};
onCheckedChangeListener2 = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, #IdRes int i) {
clearAllRadioButton();
((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
resetListenerRadioGroup();
}
};
binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
private void clearAllRadioButton() {
binding.rg2.setOnCheckedChangeListener(null);
binding.rg.setOnCheckedChangeListener(null);
binding.failed.setChecked(false);
binding.draft.setChecked(false);
binding.sent.setChecked(false);
binding.inbox.setChecked(false);
}
private void resetListenerRadioGroup(){
binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
}
*do not use the radioButton's clearCheck () because it does not respond well when resetting listeners.
Check this out it will work for you:
First Get RadioButton:
RadioButton radioBalls = (RadioButton) findViewById(R.id.radioGameInPlayBalls);
RadioButton radioTime = (RadioButton) findViewById(R.id.radioGameInPlayTime);
radioBalls.setOnCheckedChangeListener(this);
radioTime.setOnCheckedChangeListener(this);
Now in #overide method:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
switch (buttonView.getId()) {
case R.id.radioGameInPlayBalls:
if (radioTime.isChecked() && c) {
radioBalls.setChecked(false);
return;
}
radioBalls.setEnabled(c);
break;
case R.id.radioGameInPlayTime:
if (radioBalls.isChecked() && c) {
radioTime.setChecked(false);
return;
}
radioTime.setEnabled(c);
break;
default:
break;
}
}
You should check a radio button using RadioGroup, not radio button directly. Below, I populate radio group from list of options and get the radio button id of option to be checked and check the corresponding button after populating radio group. Same goes if you determine the checked option by index.
int checkedId = -1;
for(JsonElement option : options){
RadioButton radioButton = new RadioButton(getContext());
radioGroup.addView(radioButton);
if(checkedOptionId.equals(id)){
checkedId = radioButton.getId();
}
}
radioGroup.check(checkedId);
In xml layout I have RadioGroup, Button1, Button2. When user clicks on button1, several radio buttons are programmatically created in RadioGroup (total amount of radio buttons may differ (pocet = count of radio buttons to be created).
final RadioButton[] rb = new RadioButton[pocet];
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
radiobuttonCount++;
for(int i=0; i<pocet; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Radio Button " + radiobuttonCount);
rb[i].setId(radiobuttonCount+i);
rb[i].setBackgroundResource(R.drawable.button_green);
rg.addView(rb[i]);
}
What I try to do is this: When user selects xy item from RadioGroup, I'll pass selected value to textview and remove all radioButtons.
For deleting purpose I use:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked)
{
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
}
Problem is that this sometimes works well, but sometimes first radio button remains undeleted.
P.S.
Later I want to add button2 that will feed radiogroup with different items and different radio buttons amount. That's why I need to remove all radio buttons after user does selection.
Its really easy, you just do this:
rg.removeAllViews();
because i worked with the for loop but it didn't remove all the RadioButtons.
have fun :)
My first guess is that this portion of code is bad:
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
you can't run over one view's children while removing child at the same time
(rg.getChildCount() will change during the run)
I have a TableLayout and in the third column of every row I want to place a radio group.
I build the RadioButtons like this:
rg = (RadioGroup) findViewById(R.id.radioGroup1);
for (int k = 0; k < size; k++) {
rb[k] = new RadioButton(context);
rg.addView(rb[k]);
}
However this cause my app to crash, any ideas?
You are building a primitive array with the length of megethos, but your loop uses the length size. If megethos and size are different values this can cause many different types of errors... But all of this redundant since a RadioGroup keeps this array up to date for you.
I would try something like this:
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton button;
for(int i = 0; i < 3; i++) {
button = new RadioButton(this);
button.setText("Button " + i);
group.addView(button);
}
And when you want to reference a button at index:
group.getChildAt(index);
Also please always post your logcat errors, it tells us exactly what went wrong and where to look. Otherwise we have to guess like this.
Update
The error is because you are trying to add the same button to two different layouts:
tr[k].addView(rb[k]);
rg.addView(rb[k]);
a view can only have one parent. As far as I know you cannot break a RadioGroup apart into multiple views without a lot of customization first. However a ListView already has the built-in feature setChoiceMode() that behaves like a RadioGroup:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, list);
ListView listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
You can easily adapt simple_list_item_checked to display the SSID and signal strength. Hope that helps. (If you wait long enough imran khan might cut & paste my answer with graphical change, then claim it as his own again.)
I have created one demo app in which I have added Radio buttons dynamically and also handled click events.
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup2;
ArrayList<String> buttonNames;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
buttonNames = new ArrayList<>();
buttonNames.add("No Tip");
buttonNames.add("10%");
buttonNames.add("20%");
buttonNames.add("30%");
radioGroup2.setWeightSum(Float.parseFloat(buttonNames.size() + ""));
radioGroup2.setBackgroundColor(Color.BLUE);
for (int i = 0; i < buttonNames.size(); ++i) {
RadioButton radioButton = new RadioButton(this);
radioButton.setId(i);
RadioGroup.LayoutParams childParam1 = new RadioGroup.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
childParam1.setMarginEnd(2);
radioButton.setGravity(Gravity.CENTER);
radioButton.setLayoutParams(childParam1);
radioButton.setBackground(null);
radioButton.setText(buttonNames.get(i));
radioButton.setTextColor(Color.BLUE);
radioButton.setBackgroundColor(Color.WHITE);
radioButton.setButtonDrawable(null);
if (buttonNames.get(i).equals("20%")) {
radioButton.setChecked(true);
radioButton.setBackgroundColor(Color.BLUE);
radioButton.setTextColor(Color.WHITE);
}
radioGroup2.addView(radioButton, childParam1);
}
radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i1) {
RadioButton button = null;
for (int i = 0; i < buttonNames.size(); ++i) {
if (i1 == i) {
button = (RadioButton) findViewById(i1);
button.setChecked(true);
button.setBackgroundColor(Color.BLUE);
button.setTextColor(Color.WHITE);
Toast.makeText(getApplicationContext(), button.getText() + " checked", Toast.LENGTH_SHORT).show();
} else {
button = (RadioButton) findViewById(i);
button.setChecked(false);
button.setBackgroundColor(Color.WHITE);
button.setTextColor(Color.BLUE);
}
}
}
});
}
}