Getting RGB at Runtime from color - android

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);

Related

How to convert String to int Color value

I want to set background with gradient. This's my code:
val startColor = "0xFFAC235E"
val endColor = "0xFF640C35"
val gradient = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
intArrayOf(
startColor.toInt(),
endColor.toInt()
)
)
view.background = gradient
and it through an exception:
java.lang.NumberFormatException: For input string: "0xFFAC235E"
If I replace startColor = 0xFFAC235E, the code above work fine. But that's not what I want.
I need put color as param String. Is there anyway to convert it?
Try replacing 0x with #.
For ex:
startColor.replace("0x", "#")
Generally we define colors with hex color codes. So, I think this will work for you.
Edit
You have to parse the color string to convert it into integer.
Color.parseColor(startColor.replace("0x", "#"))

setStrokeColor not working programmatically

I'm trying to set my color for the outline of my button, but I don't get it to work
I'm using material button and when I use
button.setStrokeColorResource(Color.parseColor(#e4dcd4))
is not working and tells me this
Expected a color resource id (R.color.) but received an RGB integer
I tried almost everything I could found about in stack, but I can't get it to set this strokeColor programmatically
Edit
Almost all setColors use #ColorInt , but this strokeColor uses #ColorRes, which is not working for me, also there is setStrokeColor
public void setStrokeColor(#Nullable ColorStateList strokeColor) {
if (isUsingOriginalBackground()) {
materialButtonHelper.setStrokeColor(strokeColor);
}
}
But I can't get it to work either.
It worked like this
val colorInt = Color.parseColor("#e4dcd4")
val csl = ColorStateList.valueOf(colorInt)
my_button.strokeColor = csl
You might try this
button.setStrokeColor(ContextCompat.getColor(this, R.color.your_color_xml));
Other way you can do is
ShapeDrawable gradientDrawable = (ShapeDrawable)button.getBackground();
gradientDrawable.setStroke(2, your_color);
Also as #Gabriele said you can get an int as a color as :
//From RGB
int colorRGB = Color.rgb(255,0,0);
//From HEX String
int colorHEX = Color.parseColor("#FF11AA");
You have to set the width of the stroke because the default value is 0.
<Button
app:strokeWidth="2dp"
../>
button.strokeColor = ColorStateList.valueOf(Color.parseColor("#e4dcd4"))
or
// if color define in color.xml
button.strokeColor = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.yourColorCOde))
// if you have different state and you want to set programmatically then do as :-
var states = arrayOf(
intArrayOf(R.attr.state_enabled),
intArrayOf(-R.attr.state_enabled),
intArrayOf(-R.attr.state_checked),
intArrayOf(R.attr.state_pressed)
)
// Color list define respect of state
var colors = intArrayOf(
Color.BLACK,
Color.RED,
Color.GREEN,
Color.BLUE
)
// Set stroke color
button.strokeColor = ColorStateList(states, colors)

Why are all the colors the same?

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.

Android BitmapShader change color

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!

how to give background colour to textview in android?

How to give background colour to textview in Android?
textView.setBackgroundResource(resourceId) //from resource
textView.setBackgroundColor(int color) // e.g. Color.RED
for custom colors
int color = Color.rgb(int red, int green, int blue);

Categories

Resources