Android: A specific RadioButton belongs to which RadioGroup? - android

Is it possible to identify for which Radio Group a specific Radio Button belongs to in Android?
Eg:
RadioGroup_A
- RadioButton_1A
- RadioButton_2A
RadioGroup_B
- RadioButton_1B
- RadioButton_2B
When I have RadioButton_1A, to idetify that belongs to RadioGroup_A ?
I need to implement a logic to ignore triggering CheckedChange listener for a speacial case. Need to restrict that only for a one RadioGroup When I know that RadioButton belongs to that specific RadioGroup.
Thanks

What about radioGroup.indexOfChild method? The doc says that returns:
a positive integer representing the position of the view in the group,
or -1 if the view does not exist in the group
Maybe you can check if your RadioButton exits in the RadioGroup:
int idx = radioGroup.indexOfChild(radioButton);
if (idx != -1)
{
//radioButton is in the radioGroup
}
else {
//radioButton isnt in the radioGroup
}
Hope it helps!

In one of my apps, I did this by having two RadioButon ArrayLists, one for each group. In onCreate() after the layout is inflated, the RadioButtons are added to each group as described in this answer.
Then, by checking for which ArrayList contains the RadioButton, the required logic can be implemented.

i described full example of your question please apply my solution
public class MyClass extends Activity implements RadioGroup.OnCheckedChangeListener
{
RadioGroup rg1,rg2,rg3;
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
rg1=(RadioGroup)findViewById(R.id.rg1);
rg2=(RadioGroup)findViewById(R.id.rg2);
rg3=(RadioGroup)findViewById(R.id.rg3);
rg1.setOnCheckedChangeListener(this);
rg2.setOnCheckedChangeListener(this);
rg3.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(radioGroup==rg1)
{
//First RadioGroup Clicked
RadioButton radio=(RadioButton)findViewById(i);
Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
}
if(radioGroup==rg2)
{
//Second RadioGroup Clicked
RadioButton radio=(RadioButton)findViewById(i);
Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
}
if(radioGroup==rg3)
{
//Third RadioGroup Clicked
RadioButton radio=(RadioButton)findViewById(i);
Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
}

Related

Prevent form submission if radio button isn't checked

Working on an education android app. One provides answers through EditText input, radio buttons and CheckBox.
I want a check for the RadioGroup that when no RadioButton is clicked it will prompt the student and prevent submission. I succeeded in EditText but unsucessful for RadioButton.
public void checkEmptyButtons(View view){
RadioGroup rg = (RadioGroup) findViewById(R.id.myRg);
if (rg.getCheckedRadioButton()==-1{
// this is where I have a problem
//I don't even know how to search on the //dev.anderoid site
}
}
That's where am stucked. Thanks in advance
.
you need radioGroup checked Change Listener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//here you will get callback everytime radio button is checked along with id. You can have boolean checked here to know if any of your radio button is checked
}
});
Use getCheckedRadioButtonId() in stead of getCheckedRadioButton().
if (rg.getCheckedRadioButtonId() == -1) {
//no radio buttons are checked
}
else {
//any one of the radio buttons is checked
}

RadioButton Issue get checked when sending?

Having two RadioButtons and button send.
When I click on the send button the 2 RadioButtons are checked.
How to avoid this?
enter image description here
You need to group the radio buttons so inside the xml add the radio buttons inside this tag:
<RadioGroup> .....</RadioGroup>
then in your activity do this:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.myRadioGroup); //declare the radiogroup
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.radiobtn1) {
//code here
} else if(checkedId == R.id.radiobtn2) {
//code here
} else {
//code here
}
}
});
If you want only one RadioButton to be checked at once, keep them inside a RadioGroup.
Just Put your RadioButtons inside RadioGroup

RadioGroup.check() make OnCheckedChangeListener.onCheckedChanged called twice [duplicate]

I've been struggling with an issue of programmatic checking of a RadioButton, which is situated in a RadioGroup. It seems that a single check() call raises three events - once for the first RadioButton and twice for the second, which is my target. At the same time clicking on the second RadioButton in the interface causes only one event to appear, which is correct.
So, is there a way to avoid multiple event raising ?
public class RadiogroupActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup r = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton b = (RadioButton) findViewById(R.id.radio1);
r.setOnCheckedChangeListener(onPromobuttonsClick);
r.check(R.id.radio1);
}
private OnCheckedChangeListener onPromobuttonsClick = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("x", "x");
}
};
}
So, is there a way to avoid multiple event raising ?
Call b.setChecked(true); instead.
A quick look through the source code confirms that RadioGroup#check() really does call the OnCheckChangedListener three times...
When the current selection is unchecked,
When the new RadioButton is checked, and
When the RadioGroup saves which RadioButton is now checked.
Odd quirk.
Simply post your r.setOnCheckedChangeListener(onPromobuttonsClick); line in onResume() method. It is works for me.
So this is an old one but I also got a similar behavior. A workaround that I found was to call the toggle() method of RadioButton instead of the check() method of RadioGroup.
So in this example, you woulod just need to swap
r.check(R.id.radio1);
with
b.toggle();
since b is already the view with id R.id.radio1.
I had this problem when I wanted to log analytics for radio button presses, but the way the radio groups calls were being handled with .check() caused onCheckedChangeListener to get called multiple times (which sent too many events).
I handled it the following way to only get one event per click:
// create listeners so we don't duplicate the creation in for loop
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// whatever you want to do here
}
};
// add on click listener to all radio buttons
int buttonCount = buttonGroup.getChildCount();
for (int i = 0; i < buttonCount; i++) {
if (buttonGroup.getChildAt(i) instanceof RadioButton) {
buttonGroup.getChildAt(i).setOnClickListener(onClickListener);
}
}
This is a old question but I didn't found a complete answer yet that cover my issue. But thanks to #Hellojeffy suggestion I figured a way to handle the situation when RadioButton trigger multipletimes when using setOnCheckedChangeListener().
The idea is to add a click on each view inside the RadioGroup and trigger a event on click, instead a event on check changes.
for (child in radioButtonGroup.children) {
if (child is RadioButton) {
when (child.id) {
R.id.rb1 -> child.setOnClickListener { onRb1Click(...) }
R.id.rb2 -> child.setOnClickListener { onRb1Click(...) }
R.id.rb3 -> child.setOnClickListener { onRb1Click(...) }
...
}
}
}

How to get radio Button values number

I am making an application that has list and Radio Buttons. My Layout contains of the following : a list , A button .
The List contains of Radio buttons. Two radio Buttons. After checking the buttons. I want to make a loop that reads the values of how many are in Button one and how many in Button Two.
This is how I have done so far.
ListViewStudentEditAbsence country ;
for(int i=0;i<listViewStudentNames.size();i++) {
country = listViewStudentNames.get(i);
if (country.getAbsence()==1) {
Absence[i] = "1";
} else {
Absence[i] ="0";
}
}
But It doesn't seem to work.
In your CustomAdapter getView() method
int value=0; //globally
RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
if (checkedId == R.id.rbutton1) {
value=10; //a's value
} else if (checkedId == R.id.rbutton2) {
value=20; //b's value
}
}
});
https://stackoverflow.com/questions/11194515/android-get-value-of-the-selected-radio-button
Android getting value from selected radiobutton
Hope it works. This two links give you proper knowledge about fetching value.

How to add a listener to dynamically created RadioButtons?

I have a ListView of questions. In each row there is a question and a RadioGroup with possibile answers. Not all the questions have the same number of answers, so I add the RadioButtons dynamically in my custom ListAdapter. My question is how to add Listener to these dynamically created RadioButtons. My code:
RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.question);
rg.removeAllViews();
for (String qA : question.answers) {
RadioButton rb = new RadioButton(getContext());
rb.setText(qA.toString());
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
//WHAT SHOULD I PUT HERE?
}
}
});
Maby something like this:
#Override public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
}
and then something with the btn? :)
To find the checked button
View checkedButton = rg.findViewById(checkedId);
To find the index of checked button
int checkedIndex = rg.indexOfChild(checkedButton);
Before doing the above you have to set id to the buttons like mentioned in the comments rb.setId(count). Count has to be unique for each button within a group.
1 first create global variable of CompoundButton.OnCheckedChangeListener in your adapter for example private CompoundButton.OnCheckedChangeListener listener
2 second you have to attach listener variable with your radio button at the time of creating the radio button.
3 third when you bind the data or value with your ui you have to call this method
listener = new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked )
{
}
};

Categories

Resources