Uniquely identify ImageButtons placed programmatically

I have placed multiple ImageButtons programmatically in the TableLayout, each ImageButton has its own Drawable resource as the background. I am using the XML description for the layout of the ImageButton itself, and then using LayoutInflater to retrieve such an ImageButton (genre_cell.xml):

<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/genreCellItemId" android:layout_weight="1"
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_gravity="center_horizontal"
android:paddingLeft="5dip" android:paddingRight="5dip">
</ImageButton>

      

And in my class I do:

myButton = (ImageButton) inflater.inflate(R.layout.genre_cell, row, false);

      

I did bind an onClickListener on each ImageButton, but now I would like to uniquely identify which ImageButton was clicked ... I thought maybe I could somehow get the Drawable ID used for the background and check it with available values ​​Drawable int? Is this an option, and if so, how to implement it? Also are there any other options?

+2


a source to share


2 answers


By chance I came across a blog that mentioned the setTag () and getTag () methods for ImageButton, the ones that I can use, and thus my question was answered ... Blog link: http: //jongladwin.blogspot. com / 2010/03 / androidsettag-and-gettag-usage-for.html



After some inspection I even saw the setId () method and the getId () method, so there seem to be several methods to identify such an ImageButton ... good to know: P

+1


a source


Yes, you can!

in your folder res/values

, create an xml file button_ids.xml

:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <item type="id" name="image_button_one" />
  <item type="id" name="image_button_two" />
  ...
</resources>

      



then after your bloat:

myButton = (ImageButton) inflater.inflate(R.layout.genre_cell, row, false);
myButton.setId(R.id.image_button_one);
...

      

I haven't actually tried it, but I think this is how you should do it.

+1


a source







All Articles