user565
user565

Reputation: 1001

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)

Upvotes: 1

Views: 8296

Answers (4)

user8641723
user8641723

Reputation:

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);

Upvotes: 1

Tobias Uhmann
Tobias Uhmann

Reputation: 3037

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

Upvotes: 0

Nikhil Jadhav
Nikhil Jadhav

Reputation: 1621

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.

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69709

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

Upvotes: 0

Related Questions