Android Button edit

Defining external Listener

When should I use it


To achieve this you need to create a class implementing one of the listeners in the View API.

For example, give help when long click on any element:

public class HelpLongClickListener implements View.OnLongClickListener 
{
    public HelpLongClickListener() {
    }

    @Override 
    public void onLongClick(View v) {
        // show help toast or popup
    }
}

Then you just need to have an attribute or variable in your Activity to use it:

HelpLongClickListener helpListener = new HelpLongClickListener(...);

button1.setOnClickListener(helpListener);
button2.setOnClickListener(helpListener);
label.setOnClickListener(helpListener);
button1.setOnClickListener(helpListener);

NOTE: defining listeners in separated class has one disadvantage, it cannot access class fields directly, so you need to pass data (context, view) through constructor unless you make attributes public or define geters.


Table Of Contents
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close