Dan Methad
Dan Methad

Reputation: 217

Android ImageView set non int ID

Is it possible to set ImageViews id with a String?

I'm adding ImageViews programmatically and i want to set the ID the something like "IMG12a"

    ImageView image = new ImageView(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
    params.leftMargin = X;
    params.topMargin = Y;
    image.setLayoutParams(params);
    image.setScaleType(ImageView.ScaleType.CENTER_CROP);
    image.setImageResource(IMG);
    image.setId(?????);
    theLayout.addView(image);

Upvotes: 3

Views: 2630

Answers (2)

Erez
Erez

Reputation: 2655

It's not possible with setId,

you should use setTag(Object tag) (you can use a string and you don't need to convert it to an Object).

and use View.findViewWithTag(Object tag) in order to get the View and modify it.

if you need the get a tag - use getTag(). and getTag().toString() in order to get it as a string.

Upvotes: 1

Ryan
Ryan

Reputation: 672

No, but you could use setTag (int key, Object tag) or setTag (Object tag) instead.

http://developer.android.com/reference/android/view/View.html#setTag(int, java.lang.Object) http://developer.android.com/reference/android/view/View.html#setTag(java.lang.Object)

and get the tag with getTag (int key) or getTag (), respectively.

http://developer.android.com/reference/android/view/View.html#getTag(int) http://developer.android.com/reference/android/view/View.html#getTag()

You'll then need to cast the returned Object to a String.

Upvotes: 3

Related Questions