Alex F
Alex F

Reputation: 43321

Subscribe to button click without inline implementation

public void onCreate(Bundle savedInstanceState) {
    ...
    btn_client_connect.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            // inline implementation goes here ...
        }
}

I want to move inline implementation to separate function, keeping onCreate function short and readable:

    private void OnBtnConnectClick(View v) {
        // implementation...
    }

Now I need to subscribe to the button click using something like:

btn_client_connect.setOnClickListener(this.OnBtnConnectClick);

But this is not compiled. I only have this ugly solution:

btn_client_connect.setOnClickListener(new Button.OnClickListener() 
{
    public void onClick(View v) 
    {
        OnBtnConnectClick(v);
    }
});

Is there better way to do this?

Upvotes: 0

Views: 189

Answers (6)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

you can define the Method that the button (or the view) on click perform within your XML,

read more http://smartcloudblog.blogspot.com/2011/09/android-onclicklisteners-vs.html

Upvotes: 0

JafarKhQ
JafarKhQ

Reputation: 8734

protected void onCreate(Bundle bundle){
   btn = findview....
   btn.setOnClickListener(buttonClcikListener);
}



private View.OnClickListener buttonClcikListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // your code here 
    }
};

Upvotes: 3

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

there are other ways to set Click Listeners like one

you simply type in xml

android:onClick="OnBtnConnectClick"

and other as Keppil suggest

myButton.setOnClickListener(new myClickListener());

public class myClickListener implements OnClickListener {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }

}

Upvotes: 1

Heskja
Heskja

Reputation: 785

There are several ways of doing this:

btn_client_connect.setOnClickListener(getButtonListener());

And then that method would look like:

public OnClickListener getButtonListener(){
    return new OnClickListener() {
        public void onClick(View v) {
            // inline implementation goes here ...
            OnBtnConnectClick(v);
        }
}

Or another way of doing it:

btn_client_connect.setOnClickListener(new SomeButtonListener());

And then that class would look like:

public (or private) class SomeButtonListener implements OnClickListener {
     public void onClick(View v) {
            // inline implementation goes here ...
            OnBtnConnectClick(v);
        }
}

Upvotes: 1

Egor
Egor

Reputation: 40203

If you're creating your layout in XML, then you can use the android:onClick attribute for Button. You need to define a method's name:

android:onClick="onClick"

and implement this method inside your Activity:

public void onClick(View v) {
  switch(v.getId()) {
    ...
  }
}

No need for extra code. Hope this helps.

Upvotes: 1

Keppil
Keppil

Reputation: 46219

Methods aren't objects in Java, so you can't do this.

What you can do is create a private inner class (or a separate class, it's up to you):

private class MyListener implements OnClickListener {
    public void onClick(View v) 
    {
        OnBtnConnectClick(v);
    }
}

and then do

this.onBtnConnectClick = new MyListener();
btn_client_connect.setOnClickListener(this.onBtnConnectClick);

Upvotes: 2

Related Questions