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??
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);
Related
The CircularImageView, https://github.com/Pkmmte/CircularImageView, works great when setting an image bitmap like so...
circularImageView.setImageBitmap(bitmap);
However, there are times where I just want to set a solid color instead of a bitmap. If I do something like this,
circularImageView.setBackgroundResource(R.color.blue);
The color of the view is set but the image is never made circular, so it fill the entire rectangular view. I'm assuming the getDrawable() is returning null so it can't actually manipulate the view. Anyone ran into this problem or any suggestions on what to do?
Edit:
I can do this but it seems a bit flimsy:
Bitmap image = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
circularImageView.setImageBitmap(image);
You should call circularImageView.setImageResource(R.color.blue) instead.
The code you wrote sets the background of the view, not the content image. Looking at the code on github, this view will only clip the content image to the circle--it has no effect on the background at all.
Give a try for ColorDrawable;
int decode = Integer.decode("FF6666");
ColorDrawable colorDrawable = new ColorDrawable(decode);
I have a textView in which I put some text (obviously) and a Drawable left. Unfortunately, when I use phones with small screens like a Samsung Galaxy Y, instead of just a single line, the text runs over to a second line. (which is fine). However, the drawable that I set now aligns itself to the center of the textview, and not on the left of the first line.
I want my drawable to stay aligned to the first line of text in my textview, no matter how many lines of text, my textview holds. Is there a way to do this? Can anyone point me in the right direction?
my implementation is pretty straightforward
...
tv.setCompoundDrawables(drawableLeft, null, null, null);
where my drawableLeft is the image from my drawable folder.
Thanks a lot!
Try to use a drawable like a separate view (ie ImageView) and not to set it as a tag in a TextView. Using that will grant you more control over the drawable/ImageView it self.
In order to align a Drawable to the top of your TextView you can use a custom Drawable that wraps the Drawable you want to show. Then, override the method onDraw(Canvas) in your custom Drawable and translate the Canvas to manipulate the position of your Drawable.
See my answer here for an example.
I have problem with padding in StateListDrawable.
If for some in my styles i define reference on some <selector> with image resources, it set some wrong padding for my 9path images. By the way i set particular image - all is ok. But otherwise, android create StateListDrawable for my <selector> and (as i saw by using debugger on sources) it get padding by use method:
Rect getConstantPadding();
and return wrong values (in my case it not null or 0).
This method use mVariablePadding variable:
if (mVariablePadding) {
return null;
}
But i can't set it false in resources (maybe i did something wrong).
Does someone know solution for this problem? Thanks!
The problem was in 9-path images. For selector it calculate padding from right-bottom border (content) of nine-path drawable.
Beside there is variables in selector android:variablePadding, that calculate that padding (choose the biggest, etc). So it can be still non 0, even if in mostly cases images doesn't have padding.
I have an ImageView. In its onClick I get its Drawable:
Drawable dr = ((ImageView) v).getDrawable();
And set it to a dialog's ImageView:
zoomedImage.setImageDrawable(dr);
But when I close the dialog or the activity is resumed. The image at the original position gets stretched and is shown larger than its size, leading to only a portion of the image is visible in the ImageView.
Is this a case of deep copy or there is another problem?
If it is, how can do I deep copy the original Drawable so that I could set the copy to zoomed image?
Thanks in advance.
Finally I succeed!
I had similar problem, when I used color filter on my drawable it changed the drawable, its very close to the solution of the other people here, but only this worked for me:
Drawable drwNewCopy = dr.getConstantState().newDrawable().mutate();
I managed to copy the drawable using following code:
drawable.mutate().getConstantState().newDrawable();
Here mutate() makes the drawable mutable to avoid sharing its state, and getConstantState().newDrawable() creates a new copy.
Thus different ImageViews use different drawables and there's no stretching.
Use BitmapFactory to convert the drawable into bitmap separately make or perform changes on it.
The above solutions won't work for me, But it works
val myDrawable = DrawableCompat.wrap(view.background).mutate() as GradientDrawable
myDrawable.setColor(ContextCompat.getColor(view.context, R.color.White))
I am trying to set an image on the right side of my button after the button has been clicked. I want to do this via code.
I have seen how to change the background resource via code but I am not able to find any examples showing how to change the sides via code. Is it possible?
You need to use the
public void setCompoundDrawables (Drawable left, Drawable top, Drawable right,
Drawable bottom)
method with null for any that are not needed.
Usually you can change using this
Drawable draw = getResources().getDrawable(R.drawable.facebook);
myButton.setCompoundDrawablesWithIntrinsicBounds(null, null, draw, null);
Be aware you can miss the button text.