I don't understand why all the colors give the same result. All the textviews background draw in gray, although I have black, white and red. What is the problem here?
<color name="color1">#FFFFFF</color>
<color name="color2">#000000</color>
<color name="color3">#FF0000</color?
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(R.id.color1,R.color.color1);
map.put(R.id.color2,R.color.color2);
map.put(R.id.color3,R.color.color3);
GradientDrawable gradientDrawable;
TextView textView;
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
textView = findTextView(entry.getKey());
gradientDrawable = (GradientDrawable) textView.getBackground().mutate();
gradientDrawable.setColor(entry.getValue());
gradientDrawable.invalidateSelf();
}
Change it to:
gradientDrawable.setColor(getResources().getColor(entry.getValue()));
http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setColor(int)
The parameter it takes is:
argb The color used to fill the shape
By passing it the R resource int directly it's basically a random number generated by R. You need to "decode it" into a 0xAARRGGBB colour value using Resources.getColor()
http://developer.android.com/reference/android/content/res/Resources.html#getColor(int)
They probably all look the same because the ints are close to each other in value.
Related
I got the color from the button background and store as a String in the database. Later I want to use this color String in my recyclerView adapter to set the color of my TextView. Below is my code:
#Override
public void onBindViewHolder(NoteListAdapter.NoteListHolder holder, int position) {
current = data.get(position);
final String text = current.getText();
final String get_tag_text = current.getTag();
final String get_tag_color = current.getTag_color();
int[] colors = {Color.parseColor(get_tag_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
holder.note_text.setText(text);
holder.tv_tag_text.setBackground(gd);
holder.tv_tag_text.setText(get_tag_text);
}
The error I got is "Unknown color". The saved color format in the database is (The saved color format is android.graphics.drawable.GradientDrawable#d1790a4)
Below is the code to get the color from a button background drawable file and also my button xml code
color = (GradientDrawable) tag_watchlist.getBackground().mutate();
tag_color= color.toString();
<Button
android:id="#+id/tag_watch"
style="#style/tag_buttons"
android:background="#drawable/watchlist_button"
android:text="Watchlist" />
drawable file code for the button background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#a40ce1"/>
<corners android:radius="10dp"/>
</shape>
Can anyone tell me how to resolve this issue??
Edited answer
You are getting exception Caused by: java.lang.IllegalArgumentException: Unknown color mean that you are not passing the color in supported formats to method Color.parseColor.
Make sure you pass the values in following format
#RRGGBB
#AARRGGBB
Here is the valid example
Color.parseColor("#FF4081")
For more information look at documentation Color.parseColor
As per your requirement, you can achieve this API level 24 onward. If you are using current minSdkVersion 24, try below
Change your model class to save color as Integer instead String.
GradientDrawable gradientDrawable = (GradientDrawable) tag_watchlist.getBackground().mutate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
int color = gradientDrawable.getColor().getDefaultColor();
Log.d("TAG","Color is :"+color);
current.setTagColor(color); // where current is your model class
}
To get the color back from model
int color = current.getTagColor();
You need to provide at least two colors for the GradientDrawable the startColor and the endColor
It will probably throw an exception java.lang.IllegalArgumentException: needs >= 2 number of colors with this code:
int[] colors = {Color.parseColor(get_tag_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
Change your code with this:
int[] colors = {Color.parseColor(start_color), Color.parseColor(end_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
If you have get_tag_color for both of your startColor and endColor then replace accordingly but that won't be helpful with GradientDrawable.
Colour Code : e32b0f, d03559, I want to design its background drawable please help me
Try,
int colors[] = { 0xff255779, 0xfcc6c0cd };
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
view.setBackgroundDrawable(gradientDrawable);
1. choose color and add color hex code in colors array.
2. GradientDrawable using and set orientation properties.[ex. GradientDrawable.Orientation.TOP_BOTTOM]
3. set view background [ex. Linearlayout.setBackgroundDrawable(gradientDrawable);]
I want to extract the RGB value from color
My color is declared in xml as
<color name="color_primary">#009688</color>
Now I want the RGB value of this in my Activity at runtime.
How to convert color_primary in Color object ?
Any help? Thanks in advance.
Your color is an int. You can use
int color = getResources().getColor(R.color.color_primary);
in onCreate of your Activity. If you need the RGB components of your color, you can use the Color class:
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
I have a ImageView where I put a Bitmap (left), sometimes I wanna see bitmap with a semitransparent blue layer (right). I try with a ColorFilter (LightingColorFilter and PorterDuffColorFilter) but I get a dark blue, how can I do this with ColorFilter or anything else?
Thanks.
EDIT (I tried this and other variants)
//ColorFilter filter = new PorterDuffColorFilter(color.wather, PorterDuff.Mode.DST_OVER);
ColorFilter filter = new LightingColorFilter(color.mul, color.wather);
// mul = 0xFFFFFFFF and wather = 0x7000FFFF
BitmapScaler scaler = new BitmapScaler();
imagen.setImageBitmap(scaler.getScaled());
imagen.setColorFilter(filter);
I tried diferents mul, add values and always get this:
I've already finds the mul, add values, I had a problem with color.xml because I used ID number instead the color RGB number, big mistake. Transparence (Alpha) are ignore but I can get the effect downing intensity of add value.
int mul = 0xFFFFFF;
int add = 0x005050;
filter = new LightingColorFilter(mul, add);
Thanks Elior for your help.
I need a little help with the thing that I want to achieve. I'm using BitmapShader in my application to draw on a canvas. I'm setting a custom png file as shader to my paint variable and I want to change the color of shader.
Here is an example code which I'm using :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.particle_point);
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
mPaint.setShader(shader);
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
mPaint.setColorFilter(filter);
I find that I can change it's color by using :
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
, but I need to be able to change it's color by using a custom color picker,which returns color code similar to this : -234423123.
So is there any way that I can use this color code and set it as color to my paint variable.
Thanks in advance!
The color you are getting converted to hex is: FFFFFFFFF206FCAD. So youst need to get rid of the 8 leading Fs:
int color = -234423123;//0xFFFFFFFFF206FCAD
int myColor = 0x00000000FFFFFFFF & color;
myColor should be ok.
Just to add a little bit more detailed to the Moss's answer. As he suggest you can use myColor as the value you want and to set the right value to your shader you have to add myColor to your LightingColorFilter like this :
ColorFilter filter = new LightingColorFilter(myColor , myColor );
And it should work.
To get the hex String:
"#"+Integer.toHexString(n));
But your color picker just returns the color's int value which should be enough to work with!
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x000000FF );
Just change the value which represents the color to the int your colorpicker returns... (without 0x in front of it ofcourse)...
If I ain't wrong this should work just as fine!