47

I use Glide library inner custom adapter view in my apps. But I have Error :

"You must not call setTag() on a view Glide is targeting" 

This part of my code :

 @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.imageView.setAdjustViewBounds(true);
        LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        holder.imageView .setLayoutParams(vp);
        holder.imageView .setScaleType(ImageView.ScaleType.CENTER_CROP);

        String var_news_article_images = imageIdList.get(getPosition(position));

        Glide.with(context)
                .load(var_news_article_images)
                .placeholder(R.drawable.placeholder)
               .into(holder.imageView);

               return view;
    }

so how to fix it ?

2
  • Wrap your ImageView inside a ViewGroup (LinearLayout for instance). Commented Jan 19, 2016 at 11:49
  • @AmayDiam I have posted the solution! have a nice day!
    – Jorgesys
    Commented Feb 10, 2016 at 3:59

9 Answers 9

75

The key is ViewTarget.setTagId; setting it will free up the default setTag on the ImageView so you can use it as root in the item layout. It was introduced in Glide 3.6.0 in issue #370.

In your manifest add this:

<application
        android:name=".App"

Then create an application context class:

public class App extends Application {
    @Override public void onCreate() {
        super.onCreate();
        ViewTarget.setTagId(R.id.glide_tag);
    }
}

Add the following as contents for src/main/values/ids.xml:

<resources>
    <item type="id" name="glide_tag" />
</resources>

(or just add the above <item ... /> into any resources xml in values)

Update: ViewTarget was deprecated since 4.8.0. If you're using into(ImageView), you'll still have to call the deprecated class's method, because the built-in *ViewTarget classes still extend the old class.

If you use a custom ViewTarget, migrate to CustomViewTarget as the replacement. This will remove the need for setting any tag ID, because the default behaviour for CustomViewTarget is using a Glide-specific ID, so it shouldn't clash with any setTag calls. If you want to customise the ID anyway, you can use useTagId on your custom target.

5
  • This solutions DOESN´t WORK. The solution is just avoid the call of setTag() method.
    – Jorgesys
    Commented Feb 26, 2016 at 15:32
  • 2
    @Elenasys This approach configures Glide to be compatible with single-ImageView list items. I opened #370 which resulted in adding the setTagId method almost a year before this question anticipating that it may come up later. Glide has to associate Requests with Targets (Views) somehow and this is how it was designed originally and it works well in the most cases. Other times you may need a little configuration, just like with any other framework/library.
    – TWiStErRob
    Commented Feb 26, 2016 at 16:08
  • 4
    @Elenasys It actually works. Just tried it. Calling setTag() is sometimes inevitable especially if the ImageView is a custom view from a third party library. Commented Jun 23, 2016 at 11:03
  • 2
    @JasonStack it's really simple, just follow where that ID goes and who calls getRequest(). Glide stores the request object in the view's default tag by default. Notice the different overload of setTag based on presence of custom ID.
    – TWiStErRob
    Commented Dec 13, 2017 at 15:26
  • 1
    ViewTarget is deprecated! Commented Apr 9, 2019 at 17:04
19

Glide use setTag(Object) method internally. So remove all calls of setTag(Object) from your target view. ie ImageView

If you really required to use setTag method on your view, you can use setTag(int,Object) method instead.

2
  • 2
    Thanks. If using any integer constant as the first parameter, you will receive an exception: java.lang.IllegalArgumentException: The key must be an application-specific resource id.. So, see the accepted answer or stackoverflow.com/questions/2859574/….
    – CoolMind
    Commented Nov 26, 2018 at 16:13
  • 1
    Yes, You need to use Resource for First parameter. As mentioned in SetTag(int,Object) JavaDoc
    – P Vartak
    Commented Nov 28, 2018 at 5:29
3

From what I've found from Googling, seems like Glide and ViewHolder don't get along too well. I tried Picasso instead. Problem solved instantly.

You can also try the solution from TWiStErRob, but make sure you're using Glide 3.6.0 or higher. It did not recognize setTagId(int) on 3.5.2

2
  • Thanks for the version tip, I updated my answer. We're at 3.7.0 and there's no reason not to use the latest (Glide is semver'd).
    – TWiStErRob
    Commented Feb 26, 2016 at 10:39
  • 1
    Another soft fix for this that I used is, wrap the ImageView in some other view like CardView and now set the tag for the card view. (If you have to use the tag). Commented Sep 17, 2019 at 7:28
1
ImageView mImageAds = (ImageView) findViewById(R.id.iv_img);
 mImageAds.setTag(R.id.iv_img, i); // "i" means some integer value
 Glide.with(getActivity()).load(urlPath)
                          .placeholder(R.drawable.ads_banner)
                          .diskCacheStrategy(DiskCacheStrategy.ALL)
                          .into(mImageAds);

mImageAds.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(mImageAds.getTag() != null) {
                                int position = (Integer) view.getTag(R.id.iv_img);
                                //code here what would you like to do.
                            }
                        }
                    });

See also : https://androidhiker.blogspot.com/2015/10/how-to-resolve-glide-settag-issue.html

1

I had the same issue, after removing the following

android:tag="xx"

from the image on my xml layout, that issue was gone

1
  • I think everyone is mistaken, Because Glide calls a tag with a key, @Nullable private Object getTag() { return view.getTag(tagId); }
    – heqingbao
    Commented May 9 at 8:41
1

If you have freedom to use other library, use, else you can use setTag(int, Object), but if you can't do above then wrap ImageView in a basic LinearLayout > setTag() on LinearLayout > setOnClickListener on LinearLayout.

Something like :

LinearLayout imgViewLinearLayout = new LinearLayout(context);
            imgViewLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            imgViewLinearLayout.setId(R.id.dummy_image_layout);
            imgViewLinearLayout.setOnClickListener(this);

ImageView imageView = new ImageView(inflater.getContext());
                imageView.setPadding(0,15,20,0);

                imageView.setId(R.id.imageview_for_glide);

                imgViewLinearLayout.setTag("Image VIEWTAG");
imgViewLinearLayout.addView(imageView)

Override onClick and :

@Override
    public void onClick(View v) {

        if(v.getId() == R.id.dummy_image_layout){
            //Do what you wanna wanted to do on click of ImageView
        }
    }

Hope this helps.

0

As written by Thu Thu, you can simply reference an id of any existing View. For instance, suppose you want to set tag to an ImageView:

image_view.setTag(R.id.image_view, 100)
if (image_view.getTag(R.id.image_view) == 100) ...
0

I think everyone is mistaken.

Because Glide actually calls the tag with a key:

public abstract class ViewTarget<T extends View, Z> extends BaseTarget<Z> {

  ...

  private static int tagId = R.id.glide_custom_view_target_tag;
  
  @Override
  @Nullable
  public Request getRequest() {
    Object tag = getTag();
    Request request = null;
    if (tag != null) {
      if (tag instanceof Request) {
        request = (Request) tag;
      } else {
        throw new IllegalArgumentException(
            "You must not call setTag() on a view Glide is targeting");
      }
    }
    return request;
  }

  @Nullable
  private Object getTag() {
    return view.getTag(tagId);
  }
  
  ...

}

So, are there any other solutions?

-1

Use view.setTage(int, object); and view.getTag(int);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.