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
}
});
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
}
}
};
Alternate option for onClick() for Radio Button in Android Studio
you can add radio button change listener
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
})
or get selected radio button on the next button click. like below
next.setOnClickListener(new OnClickListener(View v){
String value =
((RadioButton)findViewById(rg.getCheckedRadioButtonId()))
.getText().toString();
// here you can compare the selected answer is correct or not
});
How to implement setOnClickListener to get selected value from dynamically created RadioButton Android
Selected position will be saved in position
variable
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
for (int i = 0; i < radioGroup.getChildCount(); i++) {
if (radioGroup.getChildAt(i).getId() == id) {
position = i + 1; //+1 is used to have a start value of 1 like your example
break;
}
}
}
});
Clicking radio button in android programmatically?
The setChecked
and setSelected
only change the state of the button. You could use performClick()
to obtain a simulated click on the button.
OnClick not working in Fragment with radiobuttons
change
final Button radioButton = (Button) view.findViewById(R.id.radioButton);
final Button button = (Button) view.findViewById(R.id.button);
change
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
if (radioButton.isChecked()) {
button.setText("asd");
}
}
Move the above code to onCreateView() method
How to check state Changing of Radio Button android?
First create RadioGroup
if you didn't do that yet, then as usual connect it by it's id
and then use setOnCheckedChangeListener
. You can check is your RadioButton
checked or not also you can have more then one RadioButton
. For example:
radioGroup.setOnCheckedChangeListener(new new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.firstRadioButton:
//Implement logic
break;
case R.id.secondRadioButton:
//Implement logic
break;
}
}
});
Related Topics
Googleservice Failed to Initialize
Callback to a Fragment from a Dialogfragment
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
How to Hide Action Bar Before Activity Is Created, and Then Show It Again
How to Put Multiple Project_Number/Sender Id in Google-Services.JSON
How to Change the Color of Alertdialog Title and the Color of the Line Under It
How to Add a Textview to a Linearlayout Dynamically in Android
How to Get String Width on Android
Finish an Activity from Another Activity
Image in Canvas with Touch Events
Calling Startactivity() from Outside of an Activity Context
Read All Contacts' Phone Numbers in Android
Recommended Way/Order to Read Data from a Webservice, Parse That Data and Insert It in a SQLite Db
Accessing Localhost of Pc from Usb Connected Android Mobile Device
Show Gif File with Glide (Image Loading and Caching Library)