0

Who to capture the click of any button?

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
            Toast.makeText(WHKConversorActivity.this, "Hello World", Toast.LENGTH_SHORT).show();                    
       }
 });

this is a click on button1 but i need an function for all buttons :-/ . Example in javascript:

$('button').click(function(){
    alert($(this).val());
});

Thanks :)

4
  • I don't understand. Do you want your buttons to call the same method or a different ? Commented Aug 2, 2012 at 13:58
  • 1
    Run through a loop with all the buttons and add listener inside the loop Commented Aug 2, 2012 at 14:00
  • the application contain 18 buttons and for each button on click show two same pictures, but for each button show different text. The idea is capture all buttons for show two images and show text for each button Commented Aug 2, 2012 at 14:07
  • example: all.click = show tho images and action(thisbutton.id); ... action(idbutton){ each action } Commented Aug 2, 2012 at 14:10

5 Answers 5

3

If you want all your buttons to do exactly the same thing (which is unlikely) you could bind the same click listener to each button. Eg:

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(mGlobalClick);

final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(mGlobalClick);

... ect

OnClickListener mGlobalListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
            //Stuff
            }
};

Or if you want a each button to do something different, but still need some sort of reapeated function for each, you could put that part of the code in a seperate method and reference it in every click listener

public void GlobalStuff(){
     //Stuff
}

OnClickListener mSpecificListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
            GlobalStuff();
            //More Stuff
            }
};
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming all views in your activity are in some kind of ViewGroup, you can use this:

private void applyToAllButtons(ViewGroup viewGroup, OnClickListener listener) {
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        if (viewGroup.getChildAt(i) instanceof Button) {
            viewGroup.getChildAt(i).setOnClickListener(listener);
        }
    }
}

Then, in your onCreate(), do the following:

@Override
public void onCreate(Bundle savedInstanceState) {
    ... // old code here
    View view = findViewById(R.id.layoutRoot);
    setContentView(view);
    OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            // Do stuff here.
        }
    };
    applyToAllButtons((ViewGroup) view, listener);
}

1 Comment

hardcore ram useful with loop
0

There is no generic way like in javascript. You need to assign the click handler to all buttons individually.

Comments

0

You can try overriding the Button class, and call the addListener in its constructor.

Comments

0

Thanks Jack... this is more useful :) less variables, less memory. The OnClickListener put before tiggers:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        OnClickListener globalClick = new View.OnClickListener() {
            public void onClick(View v) {
                // some functions...
            }
        };

        /* Tiggers */
        findViewById(R.id.button1).setOnClickListener(globalClick);
        findViewById(R.id.button2).setOnClickListener(globalClick);
        findViewById(R.id.button3).setOnClickListener(globalClick);
        findViewById(R.id.button4).setOnClickListener(globalClick);
        findViewById(R.id.button5).setOnClickListener(globalClick);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.