Android: RadioGroup - How to configure the event listener
This is how you get the checked radiobutton:
// This will get the radiogroup
RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1);
// This will get the radiobutton in the radiogroup that is checked
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
To use the listener, you do this:
// This overrides the radiogroup onCheckListener
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
// This puts the value (true/false) into the variable
boolean isChecked = checkedRadioButton.isChecked();
// If the radiobutton that has changed in check state is now checked...
if (isChecked)
{
// Changes the textview's text to "Checked: example radiobutton text"
tv.setText("Checked:" + checkedRadioButton.getText());
}
}
});
How to set OnClickListener on a RadioButton in Android?
I'd think a better way is to use RadioGroup
and set the listener on this to change and update the View
accordingly (saves you having 2 or 3 or 4 etc listeners).
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
Listener for RadioButton for change Fragments
For single RadioButton
you can use OnClickListener
as follows:
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
Toast.makeText(your_Activity.this, rb.getText(),
Toast.LENGTH_SHORT).show();
}
};
RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener);
In case of RadioGroup
you need to use OnCheckedChangeListener
as follows :
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
switch(checkedId) {
case R.id.radioButton1:
// switch to fragment 1
break;
case R.id.radioButton2:
// Fragment 2
break;
case R.id.radioButton3:
// Fragment 3
break;
}
}
});
The onCheckedChanged callback receives the ID of the newly checked button in the checkedId parameter.
Radio group onclick event not firing, how do I tell which is selected?
This problem can be solved using the following
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.radio0:
// TODO Something
break;
case R.id.radio1:
// TODO Something
break;
case R.id.radio2:
// TODO Something
break;
}
}
});
Alternatively
You can use custom ids rather than default one
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.male:
// TODO Something
break;
case R.id.female:
// TODO Something
break;
case R.id.other:
// TODO Something
break;
}
}
});
RadioGroup fire OnCheckedChangeListener event on checked RadioButton
From your comments, you can set an OnClickListener
on each RadioButton
. However, without knowing exactly what you are trying to accomplish, I don't see this as a viable solution. Users expect a RadioButton
to act in a certain way. You may want to consider changing the checked state of the RadioButton
each time the user returns to this Activity
.
How to add an action event for RadioButton inside a recyler view?
Replace your holder with this :
public class ViewHolder extends RecyclerView.ViewHolder{
private DatabaseReference demo;
private TextView name;
private RadioButton rb1,rb2;
private RadioGroup rg;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
rg = itemView.findViewById(R.id.rg);
rb1 = itemView.findViewById(R.id.rb1);
rb2 = itemView.findViewById(R.id.rb2);
rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");
demo.setValue("Present");
Toast.makeText(context, "updated by Present", Toast.LENGTH_SHORT).show();
}
}
});
rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");
demo.setValue("Absent");
Toast.makeText(context, "Updated by Absent", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Add event listener for all radio buttons
In the getView of your listviews adapter create the RadioButton and set the listener.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
res = inflater.inflate(R.layout.rowview, null);
RadioButton radioButton = (TextView)res.findViewById(R.id.radio_view);
radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
});
}
R.layout.rowview is the view for a single Row in you list view. R.id.radio_view is the id for your radio button within
RadioGroup calls onCheckChanged() three times
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.
How to get onClick event on radioButton in android
try this to get click listener to your radio button
RadioButton radioButton = (RadioButton) findViewById(R.id.yourRadioButton);
radioButton.setOnClickListener(radio_listener);// set listner to your radio button
than create a new radio_listener using below code
OnClickListener radio_listener = new OnClickListener (){
public void onClick(View v) {
//perform your action here
if(v==radioButton){
//perform your action here
}
}
};
Related Topics
How to Implement Drag and Drop in Android 2.2
Wrap_Content View Inside a Constraintlayout Stretches Outside the Screen
Android: Remove All the Previous Activities from the Back Stack
Android - Configure Spinner to Use Array
Send Post Request Along with Httpheaders on Android
Low-Latency Audio Playback on Android
Android, Make an Image at a Url Equal to Imageview's Image
Getapplicationcontext(), Getbasecontext(), Getapplication(), Getparent()
How to Determine Which Dependency Causes Google Play Openssl Warning
Android Viewmodel Additional Arguments
Difference Between APK (.Apk) and App Bundle (.Aab)
Is 'Shouldoverrideurlloading' Really Deprecated? What How to Use Instead
Will Google Android Ever Support .Net
Swiperefreshlayout Setrefreshing() Not Showing Indicator Initially
Expandablelistview - Hide Indicator for Groups with No Children
Toolbar Overlapping Below Status Bar
How to Show an Empty View with a Recyclerview
How to Measure Height, Width and Distance of Object Using Camera