0

I created a button which changes its Image onClick. As I tried to add another Button which does exactly the same the app crashed.

I don´t know how to add a second or more Buttons which have an access to the onClick method. I found similar questions to this but nothing solved the Problem.

Here´s the code with the example. It works for 1 button.

code:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
    Button b;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.button3);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            b.setBackgroundResource(R.drawable.ic_launcher);

        }
    });
}
}
keyser
  • 18,829
  • 16
  • 59
  • 101

3 Answers3

1

You can use the same OnClicklistener for several buttons. For example you can do like this:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
    Button b1, b2, b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
               Button myButton = (Button) v;
               myButton.setBackgroundResource(R.drawable.ic_launcher);
            }
        };

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);  

        b1.setOnClickListener(listener);
        b2.setOnClickListener(listener);
        b3.setOnClickListener(listener);     
    }
}

EDIT

To get the button which is clicked you just have to cast the View given as parameter in the onClick method:

@Override
public void onClick(View v) {
    Button myButton = (Button) v;
    myButton.setBackgroundResource(R.drawable.ic_launcher);
}
G.T.
  • 1,557
  • 1
  • 12
  • 24
  • Hi, firstable thank´s for your answer, I tested it and it only works for 1 button. When I click the first button it´s Image changes.When I click for example the second Button the Image of the first button changes again but not the second. Same at Button 3. –  Jul 22 '14 at 19:02
  • @ANDROID_DEV Indeed, I have edited my answer and now it should work. The problem was that in my example I always changed the background of `b` (the first Button). To change the background of the clicked button you have to use the `View` passed in parameter. – G.T. Jul 22 '14 at 19:09
  • Thank you very much. It works pefectly fine. I really appreciate your help :) Awesome ! –  Jul 22 '14 at 19:12
0

Instead of setting setOnClickListener, add the onClick in your xml file and create the method for clicking:

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click" />

the click method:

public void click(View bu) {
    int id = v.getId();
    b =(Button)bu.findViewById(id);
    b.setBackgroundResource(R.drawable.ic_launcher);
}  //this can be used for more than one buttons.
betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
  • That's useless. It's more difficult to maintain such code and easier to make mistakes. http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – Marius Jul 22 '14 at 18:43
  • @Marius how come, can you explain why? In this case. – betteroutthanin Jul 22 '14 at 18:50
  • I dont exactly understand how I can use it for more than 1 button. Do I just have to put android:onClick="click" in the other Buttons XML file or do I Need to write something in the code ? For example instead of this b.setBackgroundResource(R.drawable.ic_launcher); b2.setBackgroundResource(R.drawable.ic_launcher); ? –  Jul 22 '14 at 19:10
  • And just put `android:onClick="click"` in another button. – betteroutthanin Jul 22 '14 at 19:12
  • One non-subjective drawback is extra steps required to make them work with fragments. Other drawbacks are more subjective, just like such if statements: `if(cond) doSomething(); doSomethingWithoutCondition()`. http://stackoverflow.com/questions/18267288/best-practice-androidonclick-xml-attribute-or-setonclicklistener – Marius Jul 22 '14 at 19:14
  • Hi, G.T.´s answer already works! But thank´s for your help. I appreciate that ! :) But of course I´ll test your suggestion too. –  Jul 22 '14 at 19:15
  • @Marius Alright, in this simple case it works fine, but didn't dive in that much deep, thanks for correcting. – betteroutthanin Jul 22 '14 at 19:18
0

Declare another button and do the exact same thing you did for ur button b. So ur new code would be

                 Button b, b2;
                  b2=  (Button) findViewById(R...);
                  b2.setOnClickListener(new onClickListener(){
  }

This code goes below ur onclicklistener for button b and b4 your end of onCreate I am sorry if the code doesnt look perfect.. Im answering from my phone so the code is not seperate from the comment.

I hope this helps.

android.rookie
  • 171
  • 3
  • 14