Search code examples
androidandroid-radiogroupandroid-radiobutton

How to get onClick event on radioButton in android


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)


Solution

  • 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
        }
     }
    };