1

I am trying to finding a way to get onClick event on radio button. I know that there is way to get a selected radio button value like this:

radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

But I have a requirement, that when user click/select radio button by touch on a screen then do some logic. So while using this way when i choose a radio button which user previous selected then radioButton.setOnCheckedChangeListener is calling. It is little bit difficult to distinguish that who is calling onCheckedChanged event from code(when show previous selection) or user himself click on screen.

Can someone tell me how i can find onClick on touch event on radioButton(not radioGroup)

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
user565
  • 871
  • 1
  • 22
  • 47
  • [dublicate](https://stackoverflow.com/a/8323909/3166697) – Dima Kozhevin Sep 20 '17 at 13:30
  • 2
    Possible duplicate of [How to set OnClickListener on a RadioButton in Android?](https://stackoverflow.com/questions/8323778/how-to-set-onclicklistener-on-a-radiobutton-in-android) – akhilesh0707 Sep 20 '17 at 13:32

4 Answers4

1

Just set onClick in XML file like this :

 android:onClick="Button1"

After this, in your java file :

OnClickListener yourRadiolistener = new OnClickListener (){
   public void onClick(View v) {
       //Do whatever you want to do
   }
};

RadioButton Button1 = (RadioButton) findViewById(R.id.yourFirstRadioButton);
Button1.setOnClickListener(yourRadiolistener);
mrid
  • 5,782
  • 5
  • 28
  • 71
  • 1
    Actually, you set the click handler programmatically and totally ignore the XML attribute in your example. Also the "Button1" variable should be lowercase. And "yourRadioListener" must be declared before it is used. – Tobias Uhmann Sep 20 '17 at 13:40
0

Alternatively, you can set the click handler directly in XML:

<RadioGroup 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickHandler" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickHandler" />
</RadioGroup>

Your activity must then implement the onClick handler:

public class MainActivity extends AppCompatActivity {
    ...

    public void onClickHandler(View view) {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.radioButton1:
                    ...
                    break;
                case R.id.radioButton2:
                    ...
                    break;
            }
        }
    }
}

Here are the official docs: https://developer.android.com/guide/topics/ui/controls/radiobutton.html

Tobias Uhmann
  • 2,757
  • 2
  • 25
  • 35
0

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
    }
 }
};
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

you can get the id of each view that has trigered the checkchange event so get the id inside the

 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.getId()==firstButton)

compare the id and apply further logic.

Nikhil Jadhav
  • 1,621
  • 2
  • 12
  • 19