Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add a general listener for all buttons (no intrusive)?

Tags:

android

I try to play a sound when you push any button of the app.

How is the minor intrusive way to add the listener to all buttons in the app?

Can I add overwrite (and extend) the generic listener of Android?

Thanks.

like image 462
Zeus Monolitics Avatar asked Feb 17 '13 16:02

Zeus Monolitics


1 Answers

You can set one listener for all buttons and can be identified with tag also.

View.OnClickListener myClickLIstener= new View.OnClickListener() {
    public void onClick(View v) {
        String tag = (String) v.getTag();
        Log.e("","tag : "+tag)
       // your stuff
    }
};

setting up listeners...

btn1.setOnClickListener(myClickLIstener);
btn1.setTag("btn1");
btn2.setOnClickListener(myClickLIstener);
btn2.setTag("btn2");

EDIT :

Are you looking for like this...

class superTop implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Log.e("", "onClick superTop");
        }

    }

    class NewClick extends superTop implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Log.e("", "onClick NewClick");

            super.onClick(v);

        }

    }

    findViewById(R.id.button1).setOnClickListener(new NewClick());

You can call both the listener...

like image 69
Bhavesh Hirpara Avatar answered Sep 22 '22 15:09

Bhavesh Hirpara