How to Set Onclicklistener on a Radiobutton in Android

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



Leave a reply



Submit