meGaMind
meGaMind

Reputation: 105

setCompoundDrawables working only when drawable set to another imageview?

I am changing the color of a drawable and then setting it as drawable left of a textview, but i am observing a weird thing. The drawable left is only working if i am setting the drawable to some other image view before setting it into the textview.

  Drawable mDrawable = this.getResources().getDrawable(R.drawable.legendc);
                    mDrawable.setColorFilter(colorsActive[0], PorterDuff.Mode.SRC_IN);
                    mImageview.setImageDrawable(mDrawable);

                    mtextview.setCompoundDrawables(mDrawable, null, null, null);

If i remove mImageview.setImageDrawable(mDrawable); then the setCompoundDrawables is not working and no drawable left is being applied. Why is this happening??

Upvotes: 0

Views: 1164

Answers (1)

Geryson
Geryson

Reputation: 719

The reason why setCompoundDrawables() alone do not work might be something related to image rendering and creating references in Android. There is a parameter in every Drawable variable called mCallback. When you want to skip setting ImageView it's value is null, else it has a WeakReference variable - this means something like app would say "Look, reference is bound to somewhere in the memory, now I can use it!" Looks like setImageDrawable() method creates this binding, while setCompoundDrawables() doesn't.

I'm not an expert in this topic and what I've found is just a workaround (maybe you will need an ImageLoader-like object to handle this), but looks like using mtextview.setCompoundDrawablesWithIntrinsicBounds() works well.

//mImageview.setImageDrawable(mDrawable); You can delete this line

//Using this will not require to load your Drawable somewhere else
mtextview.setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);

Upvotes: 3

Related Questions