Changing onclicklistener for a button from radio buttons

radio button onclick android
multiple radio button in android
how to check and uncheck single radio button in android
how to set radio button checked in android programmatically
radio button android example
how to pass radio button value in android
radio button validation in android
radio button methods

Hi im trying to make 3 radio buttons change onclick listener for a button each radiobutton sets its own on click listener for that button. but i get (MainActivity is not an enclosing class)

Note: the radiobuttons are in settings activity and the button is in mainactivity.

Settings

public class Settings extends MainActivity {

private RadioGroup radioGroup;
private RadioButton radioButtonpc;
private RadioButton radioButtonps;
private RadioButton radioButtonxb;
public Button settings;
public Button cheatsactivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    cheatsactivity = (Button) findViewById(R.id.cheats_activity);
    settings = (Button) findViewById(R.id.setting_btn);
    radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
    radioButtonpc = (RadioButton) findViewById(R.id.radioButton1);
    radioButtonxb = (RadioButton) findViewById(R.id.radioButton2);
    radioButtonps = (RadioButton) findViewById(R.id.radioButton3);

    radioButtonpc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PC.class);
                    startActivity(intent);
                }
            });
        }
    });

    radioButtonxb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, XBOX.class);
                    startActivity(intent);
                }
            });
        }
    });
    radioButtonps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cheatsactivity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, PS.class);
                    startActivity(intent);
                }
            });
        }
    });
}




}

Update It now gives me java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference whenever i clicked on the radio button Im trying to change to make the user change the onclick listener for a button in main activty from the settings activity any solution?

Write Settings.this instead of MainActivity.this like @howdoidothis said. And I think you have mistakenly used onClickListener instead of onCheckChangedListener. See the following example:

rd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        Intent intent = new Intent(Settings.this, PC.class);
        startActivity(intent);
    }
});

Radio Buttons, To create each radio button option, create a RadioButton in your layout. To define the click event handler for a button, add the android:onClick attribute to Tip: If you need to change the radio button state yourself, use the  You don’t need to change onclicklistener every time. You can do like this : [code]private RadioGroup radioGroup; private RadioButton radioButtonpc; private RadioButton radioButtonps; private RadioButton radioButtonxb; public Button

You need to use a living activitys context as the first parameter of new Intent.

Try changing MainActivity.this to Settings.this in your intents, e.g.

Intent intent = new Intent(Settings.this, PC.class);

Radio button onclick change url and checked - JavaScript, To create each radio button option, create a RadioButton in your layout. To define the click event handler for a button, add the android:onClick attribute to Tip: If you need to change the radio button state yourself (such as when loading a​  setOnCheckedChangeListener is a method in android basically used with checkboxes, radio button etc. While working with setOnCheckedChangeListener method in Android we and implement a listener with each checkboxes which gives us a callback function for each of them.

First, always follow re-usability because you have used Radio group and you applying intent from Radio button with another button click.

Once go with below code snippet,if still not work then update here,

radioGroup 
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.radioButton1) {
                    //some code
                     Intent intent = new Intent(Settings .this, PC.class);
                    startActivity(intent);
                } else if (checkedId == R.id.radioButton2) {
                    //some code
                     Intent intent = new Intent(Settings .this, XBOX.class);
                    startActivity(intent);

                }else if (checkedId == R.id.radioButton3) {
                    //some code
                      Intent intent = new Intent(Settings .this, PS.class);
                    startActivity(intent);
                }

            }

        }); 

Android RadioButton with Examples, In my checkout page i have a set of 2 radiobuttons with onClick. How to have them checked based on selected/current url? <div class="b2bc">  In this video we will learn, how to set OnClickListeners for multiple buttons and handle them all in one onClick method. Instead of passing an anonymous inner class to the setOnClickListener method, we will pass the activity itself and implement the OnClickListener interface into our MainActivity.

Try This Code

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup radioGroup, int i) {
                        if (i == radioButtonpc.getId()) {
                            //your code
                        }
                        if (i == radioButtonxx.getId()) {
                            //your code
                        }
                        if (i == radioButton.getId()) {
                            //your code
                        }
                        if (i == radioButtonab.getId()) {
                            //your code
                        }
                    }
                });

RadioButton in Kotlin, In android, CheckBox control allow users to change the state of control either We can define click event handler for button by adding the android:onClick  Demo to create radio buttons via Java code. It reads the values for the buttons out of a resource file, creates the buttons, and sets on-click handlers for each button. Steps: - Create new

How to use Radio button click event in android, Also, it's working is same as Checkbox except that radio button can not allow By default, the RadioButton in OFF(Unchecked) state but we can change the android:onClick, It's a name of the method to invoke when the radio button clicked. Radio Groups will make sure, that we can select only one Radio Button at a time. RadioButtons are helpful to implement multiple choice features into your app. Check out the source code:

How to let user change the 'on Click Listener' for a button from a , Radio buttons allow the app users to select one option (RadioButton) a Radio button in xml layout by adding the android:onClick attribute to  Android Beginner Tutorial #6 - Switching Activities Using a Button Click By the end of this video you will know how to: 1) Set an OnClickListener to a button 2) Create a second layout 3) Navigate

Android Radio Button Example, You don't need to change onclicklistener every time. How can I let user change the "on Click Listener" for a button from a group of radio buttons in settings  Android RadioButton with Examples In android, Radio Button is a two-states button that can be either checked or unchecked and it’s the same as CheckBox control, except that it will allow only one option to select from the group of options.

Comments
  • All these radio buttons are declared in R.layout.settings xml? If possible, please share xml code.
  • yes i did declare them inside a radiogroup
  • Change the MainActivity.this to Settings.this In all Intent
  • its gives me java.lang.IllegalStateException: Could not execute method for android:onClick
  • Can you please clear where you will get java.lang.IllegalStateException: Could not execute method for android:onClick error ?
  • i fixed this it was caused by me not declaring settings activity now i get java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
  • Have you declared button widget? I don't think here you need button click event because all manages from radio button.