bhaskarc
bhaskarc

Reputation: 9521

Setting Different onClickListener for Different Button

I have a activity where there are 10 identical imageButton all derived from the same resource myimage.png I am trying to set different onClick listener for each of the 10 buttons using setTag(). However all the imageButton get tied to the same listener.

Here's my code:

public class AllButtonsActivity extends Activity {

ImageButton imageButton;
ImageButton allImageButtons[] = new ImageButton[10];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ll);

    createButtonsAndAddListener();

}

public void createButtonsAndAddListener() {
    LinearLayout parent = (LinearLayout) findViewById(R.id.ll);
    for (int i = 0; i < 10; i++) {
        imageButton = new ImageButton(this);
        imageButton.setImageResource(R.drawable.myimage);
        imageButton.setTag(i);
        allImageButtons[i] = imageButton;
        allImageButtons[i].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = (Integer) imageButton.getTag();
                Toast.makeText(AllButtonsActivity.this,
                        "ImageButton"+i+ "is clicked!", Toast.LENGTH_SHORT)
                        .show();
            }

        });
        parent.addView(imageButton);

    }
}
}

What i want is that when the i-th Button is clicked, the toast pops out a message:

"ImageButton"+i+ "is clicked!"

Upvotes: 0

Views: 70

Answers (4)

Hamad
Hamad

Reputation: 5152

i know this question is already answered but even then i want add this different approach as answer:

public class AllButtonsActivity extends Activity implements onClickListner{
ImageButton imageButton;
ImageButton allImageButtons[] = new ImageButton[10];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ll);
    createButtonsAndAddListener();

}

public void createButtonsAndAddListener() {
    LinearLayout parent = (LinearLayout) findViewById(R.id.ll);
    for (int i = 0; i < 10; i++) {
        imageButton = new ImageButton(this);
        imageButton.setImageResource(R.drawable.myimage);
        imageButton.setTag(i);
        allImageButtons[i] = imageButton;
        allImageButtons[i].setOnClickListener(this);
        parent.addView(imageButton);
    }
}
    @Override
public void onClick(View v) {
     int i = (Integer) v.getTag();
        Toast.makeText(AllButtonsActivity.this,
                "ImageButton"+i+ "is clicked!", Toast.LENGTH_SHORT)
                .show();
}
}

Upvotes: 0

Renan Bandeira
Renan Bandeira

Reputation: 3268

Improving the code above, I suggest you to define just one onClickListener and set it to each imageButton (a variable definition outside the method):

        OnClickListener click = new OnClickListener(){@Override
            public void onClick(View v) {
                int i = (Integer) v.getTag();
                Toast.makeText(AllButtonsActivity.this,
                    "ImageButton"+i+ "is clicked!", Toast.LENGTH_SHORT)
                    .show();
            }

        });

... And here is the method defined above (Dreagen answer):

public void createButtonsAndAddListener() {
    LinearLayout parent = (LinearLayout) findViewById(R.id.ll);
    for (int i = 0; i < 10; i++) {
        ImageButton imageButton = new ImageButton(this);
        imageButton.setImageResource(R.drawable.mybutton);
        imageButton.setTag(i);
        allImageButtons[i] = imageButton;
        allImageButtons[i].setOnClickListener(click);
        parent.addView(imageButton);

    }
}

Upvotes: 3

Dmide
Dmide

Reputation: 6462

You need to use View which you recieve in onClick() (which is actually your specific ImageButton), not your local imageButton variable.

        @Override
        public void onClick(View v) {
            int i = (Integer) v.getTag();
            Toast.makeText(AllButtonsActivity.this,
                    "ImageButton"+i+ "is clicked!", Toast.LENGTH_SHORT)
                    .show();
        }

Upvotes: 1

Dreagen
Dreagen

Reputation: 1743

Your image button variable is being overwritten for each iteration of your loop. Rather than using a variable which you declare at the start of your Activity create a new one each time

public void createButtonsAndAddListener() {
    LinearLayout parent = (LinearLayout) findViewById(R.id.ll);
    for (int i = 0; i < 10; i++) {
        ImageButton imageButton = new ImageButton(this);
        imageButton.setImageResource(R.drawable.mybutton);
        imageButton.setTag(i);
        allImageButtons[i] = imageButton;
        allImageButtons[i].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = (Integer) imageButton.getTag();
                Toast.makeText(AllButtonsActivity.this,
                    "ImageButton"+i+ "is clicked!", Toast.LENGTH_SHORT)
                    .show();
            }

        });
        parent.addView(imageButton);

    }
}

Change your code to the code above and remove ImageButton imageButton; from your activity variable declarations

Upvotes: 2

Related Questions