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!
Related
I would like to change (map) white color to blue color. How can I do this on ImageView in Android? I tried setColorFilter by PorterDuff / LightingColorFilter / ColorMatrixColorFilter but I can't figure out how to set it up. There is a transparent background around the image.
Try to use Background Tint color function in xml or java code.
or
I would suggest another way. Take on frame layout and place view and then image view.
white portion in image should be transparent use PNG image.
and you can give color to base view color which ever you want.
In Android, is there a way to dynamically change the color of the view depending on what kind of background it is in?
For example, if I have a floating TextView with a white text color on a ListView with a black background, the text color will be visible clearly. However, as I scroll down, if the next section of ListView has a white background the texts in the TextView won't be visible anymore since it is white on white.
Is there any way in Android to change the text color of this TextView as it reaches the background with similar/same color?
Well, it depends on the type of background.
If you are talking about a background as in a view(linearlayout, surfaceview, etc) wit ha color, you have to get the position the textview has on the screen, and set a color based on the background color at that point. But it depends on the type of color. A bitmap allows you to get a specific pixel:
bmp.getPixel(x, y);
And then you can determine the color based on that.
However, with a SurfaceView it becomes harder because there is no native method for that.
Further, a gradient-filled background makes it harder to get at a particular point. But if you use a view and have set the backgroundcolor, you can:
if (view.getBackground() == Color.RED){//View = the view with a background set using setBackground or android:background="#color or #drawable or whatever"
tv.setTextColor(Color.WHITE); //then you adjsut the textview color
}
Please be aware that the above example only works if the view has a background defined as android:background or setBackground and there is a color. When you use a bitmap, you can get the pixel and the color based on that pixel
not hard stuff
if view.getBackground() == .RED{
yourStaff.text.setTextColor(Color.WHITE);
}
Description
When attempting to change the color of a drawable that is on top of a dark background, I ran into an issue where the color would be slightly changed.
In the image below, you will see that the top view that has a white background has a dark blueish icon color, but the view below which is on top of a dark background has a light blueish color.
Image #1
The drawable colors should be consistent regardless of the background color of the containing view.
Code
ImageView fairInfoEmailImageView = (ImageView) getView().findViewById(R.id.fairInfoEmailImageView);
fairInfoEmailImageView.setColorFilter(dashboardIconColor, PorterDuff.Mode.MULTIPLY);
ImageView fairInfoSurveyImageView = (ImageView) getView().findViewById(R.id.fairInfoSurveyImageView);
fairInfoSurveyImageView.setColorFilter(dashboardIconColor, PorterDuff.Mode.MULTIPLY);
Note*: The below image is implemented by both the ImageView's drawables being white and then applying a MULTIPLY color filter with the overridden color.
Attempts
Attempted ColorFilters, Tints, etc, but I believe these are bringing the background color into the equation.
Question
Is there a way to dynamically change the color of a drawable without the use of a ColorFilter?
Feel free to ask for more code or any questions about the problem.
Those two icons in your image are the same color. R19 G89 B116 according to MSPaint.
It is a bit of an optical illusion that makes the colors seem different with different backgrounds, but they are the same.
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/
I'm trying to make a circle of one color on a background of another.
background = new ShapeDrawable(new OvalShape());
background.getPaint().setColor(main.getResources().getColor(R.color.XXX));
view.SetBackground(background);
will work for the colored circle, and
view.setBackgroundColor(getResources().getColor(R.color.XXX));
will work for the background, but they're mutually exclusive. It just ends up with what I did last. Is there a way to make the circle on another overlapping view or something like that?
setBackgroundColor() is basically a short cut for changing the view's background to a colour drawable.
To do what you want you could try one of the 2 things described below:
Put a view in a FrameLayout, set the background colour in the FrameLayout, and put the shape in the view.
You could also try to use ImageView, which can have a background and another drawable with setImageDrawable() method.