Apperside
Apperside

Reputation: 3602

Android custom button generic click handler

i'm trying to implement a custom button which allows me to execute a standard set of actions when the button is clicked (such as writing a log) plus executing the assigned click listener specific for every instance of the button. IS that possibile?

Many thanks

Upvotes: 3

Views: 3182

Answers (2)

Apperside
Apperside

Reputation: 3602

Solution found, in this way for every instance of the button i can do a standard set of actions (in this case it's just writing a log) before executing the specified click listener

@Override
    public void setOnClickListener(final OnClickListener l) {

        super.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LogHelper.WriteLogInfo("click");
                l.onClick(v);

            }
        });
    }

Upvotes: 6

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try this:

Implement OnClickListener for your Activity :

public class MainActivity extends Activity implements OnClickListener

Add click listener for all the views like this :

boldButton = (Button) findViewById(R.id.bold);
boldButton.setOnClickListener(this);

Then Override OnClick event common for all the views :

@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bold:
            // Do Something                  
            break;

        case R.id.italic:
            // Do Something
            break;

        case R.id.underline:
            // Do Something
            break;

        case R.id.reset:
            // Do Something
            break;

        default:
            break;
        }
    }

Hope it helps you.

Thanks.

Upvotes: 2

Related Questions