I am able to set my shape's stroke color by doing this:
GradientDrawable shape = (GradientDrawable) myview.getBackground();
shape.setStroke(strokeWidth, color);
Q: How do I get the current color of my shape's stroke programmatically?
Note that I am using API level 19 (so I cannot use shape.getColor() which anyway would return the filling color of the shape I believe, and not it's stroke's color).
As #pskink mentioned, Checking android sources shows that there is no such API for getting the current stroke color (which is odd and a pity).
Eventually what I did to overcome my problem, is keeping my own variable in code that indicates the current stroke color.
Related
No matter which way I create, set its alpha, or assign a drawable to DividerItemDecoration in my RecyclerView, the drawable is always rendered fully opaque in my view. Why is this and how can I get my divider to be transparent?
I've looked in the source for DividerItemDecoration and there's nowhere that the private mDivider field is having its alpha modified. It calls mDivider.draw(canvas), whose method comment says "Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter)."
I've tried creating the Drawable as an XML shape with a transparent color. I've tried creating the Drawable as a single pixel PNG. I've tried setting the alpha on the Drawable programmatically with setAlpha(). I've tried assigning the Drawable to the DividerItemDecoration programmatically with setDrawable(). I've tried assigning the Drawable in the theme with #android:attr/listDivider. All of these methods yield the same result. The Drawable is rendered fully opaque.
Ok, figured this out. The problem was the background color of the RecyclerView wasn't set. I was looking for the divider to draw on top of the item views, at the bottom of them. But it's actually drawing the divider view between the item views, with the RecyclerView color behind it (if set) or else the color of the view containing the RecyclerView. I was looking for the transparent color to show through the light color of the item view, instead it was showing through the default dark color of the screen behind the RecyclerView. Problem solved!
Android SDK provides the following icons.
Is there a way to set a color to those .. and if possible, how to do so?
<ImageView
android:id="#+id/share_button"
style="#style/IconNav"
android:src="#android:drawable/ic_menu_share"/>
<ImageView
android:id="#+id/bookmark_button"
style="#style/IconNav"
android:src="#android:drawable/ic_input_get"/>
UPDATE
After doing a complete refresh of the project, it turns out the tint attribute in Xml did the trick.
For the short answer
.. this is the solution that worked for me - adding the property to the ImageView xml:
android:tint="#color/grass_dark"
The answer from #goldenb is a thorough run through of the different ways to solve for this, so am marking that one as the answer.
You can indeed use a tint as a way of changing an ImageView's colour, BUT you should be reminded that the android:tint will always be applied on top of the original colour.
as stated by blogger danlew
ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it
applies the tint on top of the existing color. So, for example, if the
source asset is black, and you want it to be #77FFFFFF (a translucent
shade of white), you'll actually end up getting that shade of white
with a black background beneath it.
android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.
One possible alternative would be for you to use android ColorFilter
According to the official documentation:
A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.
There are lots of more or less complex things you can do with ColorFilter but how can you apply this then?
One simple example from another so question is:
//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
or
imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))
Or a more complete answer here in SO from here
ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);
// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );
You should check these links if you want to learn more
SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?
setColorFilter()
Fast Android asset theming with ColorFilter
SO-Modifying the color of an android drawable
You can use a bitmap with a tint. Add this to your drawables folder.
ic_input_get_colored.xml :
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#android:drawable/ic_input_get"
android:tint="#color/yourDesiredColor"/>
I was looking through the Android source code and it seems like it's not possible to get the stroke width of a GradientDrawable without getting it via reflection. The reason I'm trying to get the stroke width is because I wrote the GradientDrawable in xml and am trying to dynamically update it.
If it were a ShapeDrawable, the stroke width would be available via the ShapeDrawable's paint:
((ShapeDrawable) view.getBackground()).getPaint().getStrokeWidth();
You can dynamically update the stroke of a GradientDrawable with something like:
((GradientDrawable) mView.getBackground().mutate()).setStroke(width, color);
as explained in How to change stroke color dynamically?
There are more options to change the stroke, explained around https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setStroke(int,%20int)
I'm still looking for a way of getting the stroke width.
(I know this was asked over 3 years ago, but someone might still read it now)
Let's say I have an ShapeDrawable in Android. Not a bitmap. I would like to replace single color (stroke color) in it by another color dynamically. The new desired color is not known at design time and can't be put into drawable resource.
For example, go from picture 1 (where black color represents the color I want to replace, and checkered background is the background not covered by the shape outline):
to image 2, where red is the color I want:
I can build the shape using alpha masks if necessary - i.e. have the white or black colors transparent, if necessary, or make the outline green, for example. The white fill color must remain white in the final result.
Is it possible to achieve that with standard color filters - ColorMatrixColorFilter, or PorterDuffColorFilter ?
If so, I'm having hard time figuring out specific filter.
I assume custom ColorFilter is not possible.
In the specific case of having a black border that you want to set to an arbitrary color, and a white interior, you can use a PorterDuff filter in ADD mode. Since it's saturating it won't have any effect on the white area, and since black is zero adding the color will effectively set all black areas to that color.
e.g. to set it to red:
drawable.setColorFilter(0x00ff0000, PorterDuff.Mode.ADD);
This tutorial may be helpful. According to this one, you can replace the white color portion of an image with desired color.
Changing color of drawable icon programmatically http://takeoffandroid.com/uncategorized/changing-color-of-drawable-icon-programmatically/
So I am trying to adjust the alpha of a view programmatically. As of API 11 there is View.setAlpha(alpha) which works great. My app otherwise supports back to API 4 so is there another way to set the alpha for a view?
Apply an AlphaAnimation to the view.
Try View.setBackgroundColor with a color that includes transparency as the first set of hex values before the RGB values (for fully visible red that would be #FFFF0000 - FF for alpha and FF0000 for RGB).
How to set background color of a View