Android - How to set a named method in button.setOnClickListener()

Most samples that I see appear to use an anonymous method in a call like button.setOnClickListener(). Instead, I'd like to pass in a method defined on the Activity class that I'm working in. What's the Java/Android equivalent of the following event handler wiring in C#?

Button myButton = new Button();
myButton.Click += this.OnMyButtonClick;

Where:

private void OnMyButtonClick(object sender, EventArgs ea)
{
}

Essentially, I'd like to reuse a non-anonymous method to handle the click event of multiple buttons.


Asked by: Emma723 | Posted: 25-01-2022






Answer 1

Roman Nurik's answer is almost correct. View.OnClickListener() is actually an interface. So if your Activity implements OnClickListener, you can set it as the button click handler.

public class Main extends Activity implements OnClickListener {

      public void onCreate() {
           button.setOnClickListener(this);
           button2.setOnClickListener(this);
      }

      public void onClick(View v) {
           //Handle based on which view was clicked.
      }
}

There aren't delegates as in .Net, so you're stuck using the function based on the interface. In .Net you can specify a different function through the use of delegates.

Answered by: Kelvin671 | Posted: 26-02-2022



Answer 2

The argument to View.setOnClickListener must be an instance of the class View.OnClickListener (an inner class of the View class).. For your use case, you can keep an instance of this inner class in a variable and then pass that in, like so:

View.OnClickListener clickListener = new OnClickListener() {
    public void onClick(View v) {
        // do something here
    }
};

myButton.setOnClickListener(clickListener);
myButton2.setOnClickListener(clickListener);

If you need this listener across multiple subroutines/methods, you can store it as a member variable in your activity class.

Answered by: Aston898 | Posted: 26-02-2022



Answer 3

The signature of the method will need to be this...

public void onMyButtonClick(View view){

}

If you are not using dynamic buttons, you can set the "onClick" event from the designer to "onMyButtonClick". That's how I do it for static buttons on a screen. It was easier for me to relate it to C#.

Answered by: Cherry954 | Posted: 26-02-2022



Similar questions

android - How can i implement button.setOnClickListener and button.setOnTouchListener simultaneously

i am working on android. i am creating a login button. i want that whenever i press login button then text color of that button should be changed. and when this button pressed then login functionality should be performed. for this i am coding like this:- button_login.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { ...


android - Add Toast in button.setOnClickListener?

How to Toast is add when changing picture ? button = (ImageButton)findViewById(R.id.imageButton1); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (bIcon) button.setImageResource(R.drawable.add1); else button.setImageResource(R.drawable.add2); bIcon = !...


Multiple Clicks on button leads to calling button.setOnClickListener multiple times in Android

I have a button.setOnClickListener() which inserts data into database, but I have to call button.setOnClickListener() only once even it was clicked multiple times, I have tried the below code but it didn't work: int flag=1; @Override public void onClick(View v) { if(flag) { button.setEnabled(false); Log.d("ins", "called"); } flag=0; }


android - button.setOnClicklistener cause unfortunately app_name has stopped in kepler

I have an app that contain 4 buttons , 2 in MainActivity and 2 in custom Dialog. when I call setOnClickListener() for 2 buttons in custom Dialog , it cause Unfortunately (app_name) has stopped in virtual machine public class MainActivity extends Activity implements OnClickListener{ Button btnAdd; Button btnSet; Button btnDlt; Button btnCancel; Dialog getDialog = new Dialog(this); Li...


java - How to pass value from Button.setOnClickListener to onDateSet Android

I have a class called InboxActivity in which I use datepicker, and I need to pass some data(it is one string var, like this: String test = "aa";) from protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_inbox); btn_outbox = (Button)findViewById(R.id.btn_outbox); btn_outbox.setOnClickListene...


java - Android app closes due because of button.setOnClickListener(this)

I'm going through a tutorial that's teaching me about Android App development. I've been going through it all just fine but for some reason when I run this activity the app closes and says that it has stopped working. As far as I can tell I have copied the person's code correctly, but his works. I found that the problem is with the line inside onCreate that says tryCmd.setOnClickListener(this); If I co...


java - Android Dialog Button.setonClickListener

When I create a dialog and set an onClickListener on a button, the app crashes. The same code works in another Activity, so what's the matter? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCR...


android - button.setOnClickListener. error

This is my code: final Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); This is the android developers guide link: http://developer.android....


java - DialogFragment: button.setOnClickListener throws NullPointerException

I have an Activity with a button like this: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:...


java - Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

This question already has answers here:






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top