i want to get image name on click of image which is created dynamically......
for(int i=0;i<list.size();i++)
{
final ImageButton b=new ImageButton(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70);
layoutParams.setMargins(5, 5, 0, 0); // left, top, right, bottom
b.setLayoutParams(layoutParams);
Resources res = getResources();
int resourceId = res.getIdentifier("_"+list.get(i).toString(), "drawable", getPackageName() );
b.setImageResource( resourceId );
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//at here i want to get the selected image name
}
});
rowoptions.addView(b);
}
Set the image's name as the ImageButton
tag. You can then retrieve it in onClick()
. Use setTag()
and getTag()
for this:
String imageName = "_" + list.get(i).toString();
int resourceId = res.getIdentifier(imageName, "drawable", getPackageName() );
b.setTag(imageName);
b.setImageResource(resourceId);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//at here i want to get the selected image name
String clickedImageName = (String)v.getTag();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With