Reputation: 98
I have dynamically generated ImageButtons with different ImageResource for each ImageButton. Now I want to know which ImageButton was clicked, how can I determine this ? Need your help. Thanks.
Upvotes: 0
Views: 2612
Reputation: 763
i had to do same thing and this is what i have done
for(int i = 0 ;i<mediaList.size();i++){
view_media_gallery_item = LayoutInflater.from(view.getContext()).inflate(R.layout.e_media_gallery_item, null);
TextView title = (TextView) view_media_gallery_item.findViewById(R.id.media_gallery_item_title);
TextView subtitle = (TextView) view_media_gallery_item.findViewById(R.id.media_gallery_item_subtitle);
ImageView flux_Title_Image =(ImageView) view_media_gallery_item.findViewById(R.id.media_gallery_item_img);
title.setId(i+100);
subtitle.setId(i+1000);
flux_Title_Image.setId(2000+i);
title.setText("" +mediaList.get(i).getTitle());
subtitle.setText(""+mediaList.get(i).getArtist());
System.out.println("view added::::");
view_media_gallery_item.setTag(mediaList.get(i));
view_media_gallery_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("view media clicked");
Media m = (Media )v.getTag();
medialistner.setOnItemclick(m);
}
});
Upvotes: 0
Reputation: 109237
If your code generates the imageButtons then, in this code you can write something like,
imageButton.setId(1);
and when your imageButton is clicked then you can get it with,
int id = imageButton.getId();
Upvotes: 0
Reputation: 6728
Any resource is uniquely identified by its id which is generated in R.java file. So you can use something like :
if(image.getId() == R.id.image) {
// do awesome stuff
}
Upvotes: 0
Reputation: 5183
In order to do this you could do two things:
Firstly, when dynamically generated the ImageButton you could call setId() in order to set a specific id to this View and store it in List, etc.
Then when you have a click event (or anything else), you can call the getId() method of the View to get the id.
Then you can compare and do anything you want.
Hope this helps!
Upvotes: 0
Reputation: 4064
you can set an id for each created ImageButton
and getId()
for check witch button clicked
ImageButton im=new ImageButton(Yourcontext);
im.setId(giveAnID);
//where you check
int theID=im.getId();
Upvotes: 4