I set color of my TextView randomly. I have array String[] colors = {"BLUE", "RED", "GREEN", "BLACK", "YELLOW", "GRAY"}. I need to check if color of my TextView in this array and return index. How can I do it?
First get the text (or background) color from your TextView. Then simply iterate over your list of colors (not color names) and return the index when you find a match.
When you use List instead of an array the indexOf() function already does this for you.
int textColor = textView.getCurrentTextColor();
List<Integer> colors = Arrays.asList(Color.BLUE, Color.RED, Color.GREEN, Color.BLACK, Color.YELLOW, Color.GRAY);
int index = colors.indexOf(textColor);
Related
I want to achieve something like this:
See the image here
A list with circular shape drawable with a fill of a light variant color of the image tint color
I have tried textrdrawablelibrary but it does not give me the same. I have tried the following code too but to no avail:
public int darkenColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f; // value component
color = Color.HSVToColor(hsv);
return color;
}
public String[] mColors = {
"5E97F6",
"9CCC65",
"FF8A65",
"9E9E9E",
"9FA8DA",
"90A4AE",
"AED581",
"F6BF26",
"FFA726",
"4DD0E1",
"BA68C8",
"A1887F",
};
// all colors used by gmail application :)
light colors
// genrating random num from 0 to 11 because you can add more or less
int i1 = new Random().nextInt(11);
//genrating shape with colors
GradientDrawable draw = new GradientDrawable();
draw.setShape(GradientDrawable.OVAL);
draw.setColor(Color.parseColor ("#"+mColors[i1]));
// assigning to textview
contact_name_circle.setBackground(draw); //textview
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)
I am currently working on an android APP and I have a list of predefined color in my colors.xml.
Now what I want to achieve is that I want to give the user the opportunity of selecting a color and from the color the user select, I want to display various color (randomly) in the background that matches the global color the user selects, such that those colors will be changing at interval. These colors that will be displayed randomly are already defined in my colors.xml file
Any Guidance or a lead sample on this requirement will be invaluable.
Thank you in advance
private int[] colors = {Color.RED, Color.YELLOW, Color.BLUE, Color.CYAN, Color.GREEN};
Random ranndom = new Random();
int ranndomColor = ranndom.nextInt(5);
whatever.setBackground(colors[ranndomColor]);
or get the colors from "colors.xml:
int color1 = getResources().getColor(R.color.color1);
int color2 = getResources().getColor(R.color.color2);
int color3 = getResources().getColor(R.color.color3);
int color4 = getResources().getColor(R.color.color4);
int color5 = getResources().getColor(R.color.color5);
private int[] colors = {color1, color2, color3, color4, color5};
Random ranndom = new Random();
int ranndomColor = ranndom.nextInt(5);
whatever.setBackground(colors[ranndomColor]);
I made this Icon in Photoshop, but now I want to make the gradiant in Android and use it as the Textcolor for a Textview
The colors and settings from Photoshop is:
Style: Linear
Angle: 90*
Colors: #00a8d9 (0%), #223595 (20%), #f1328f(40%), #ee3031(60%), #f9e63a(80%), #009958(100%)
The numbers in the parentes is the location value.
I have tried the following, but its only showing one color
LinearGradient linearGradient = new LinearGradient(20,30,40,50, new int[] {Color.parseColor("#00a8d9"), Color.parseColor("#223595"), Color.parseColor("#f1328f"), Color.parseColor("#ee3031"), Color.parseColor("#f9e63a"), Color.parseColor("#009958")},new float[]{0,0,0,0,0,0},
Shader.TileMode.REPEAT);
textView.getPaint().setShader(linearGradient);
I managed to replicate the gradient from the picture to the code with this solution:
textView = (TextView) v.findViewById(R.id.fontcolorwhite);
LinearGradient linearGradient = new LinearGradient(60,-70,60,70, new int[]{Color.parseColor("#00a8d9"), Color.parseColor("#4244b8"), Color.parseColor("#f1328f"), Color.parseColor("#ee3031"), Color.parseColor("#fde92d"), Color.parseColor("#009e54")},null,
Shader.TileMode.MIRROR);
textView.getPaint().setShader(linearGradient);
Another problem is that the gradient dosn't look the same with different screen sizes. This will be in a another question
You may Try this.
Try to set this as your view background colour and set colour as per your need.
GradientDrawable rainbow = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});
textview.setBackground(rainbow);
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);