Randomly Displaying Background Color in Android App - android

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

Related

Show Dynamic colors on recyclerview list numberings

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

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)

Change the TextInputLayout outline box color programmatically

I would like to change outline of the TextInputLayout programmatically, but I cannot seem to get it to work. There is an option to do it via XML (question by other SO user using XML), but that is unusable for me as I need to have dynamic coloring. I currently have the following layout:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="#+id/color_outline"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Choose color"/>
</com.google.android.material.textfield.TextInputLayout>
I've attempted to apply coloring by looking at the various box methods of the TextInputLayout, but it did not have any effect.
internal fun String.toIntColor() = Integer.parseInt(this.replaceFirst("#", ""), 16)
val colorOutline: TextInputLayout = view.findViewById(R.id.color_outline)
colorOutline.boxStrokeColor = "#006699".toIntColor()
How can I color it dynamically, like in the picture below?
Current situation:
Desired situation: (photoshopped)
Similar question, but focussing on XML
You can the method setBoxStrokeColorStateList.
Something like:
//Color from rgb
int color = Color.rgb(255,0,0);
//Color from hex string
int color2 = Color.parseColor("#FF11AA");
int[][] states = new int[][] {
new int[] { android.R.attr.state_focused}, // focused
new int[] { android.R.attr.state_hovered}, // hovered
new int[] { android.R.attr.state_enabled}, // enabled
new int[] { } //
};
int[] colors = new int[] {
color,
color,
color,
color2
};
ColorStateList myColorList = new ColorStateList(states, colors);
textInputLayout.setBoxStrokeColorStateList(myColorList);
In Kotlin
I have modified the #Gabriele's answer to make it working for me
You can define an extension function as :
private fun TextInputLayout.setBoxStrokeColorSelector() {
//Color from rgb
int color = Color.rgb(255,0,0);
//Color from hex string
val defaultColor = ContextCompat.getColor(context,R.color.indicator_def)
val states = arrayOf {
intArrayOf(android.R.attr.state_focused), // focused
// intArrayOf(android.R.attr.state_hovered), // hovered
intArrayOf(android.R.attr.state_enabled), // enabled
intArrayOf() // default
}
val colors = intArrayOf(color, // focused color
/*color,*/ // hovered color
color, // enabled color
defaultColor) // default color
val myColorList = ColorStateList(states, colors)
setBoxStrokeColorStateList(myColorList)
}
and just call it for any TextInputLayout in your app like
TextInputLayout.setBoxStrokeColorSelector(ContextCompat.getColor(this, R.color.colorPrimary))

How to set random gradient background color?

I have 3 gradient color in my drawable folder. i have to set these 3 colors randomly in recycle view items which i am getting from API. How to do it?
Declare global variables:
Random r = new Random();
int[] colors = new int[]{0xFF616261,0xFF131313, 0xFF125FF8};
Create GradientDrawable instance with randomly generated colors
GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {getRandom(random, colors), getRandom(random, colors)});
drawable.setCornerRadius(0f);
view.setBackgroundDrawable(drawable);
Code for random picking colors
private void getRandom(Random random, int[] colors) {
return random.nextInt(colors.length);
}

Android Number Picker Text Colour Random Color

So I have been browsing a lot and not seen anything that can help me, (yes I have seen the answer here) but it would not work with a random number(or if you could help me make it work would be amazing), if anyone could help me I am willing to offer a small paypal gift since this is driving me nuts. I will share what I'm trying currently and my list of colors
public void setNumberPickerTextColor(NumberPicker numberPicker, int color){
EditText et = ((EditText) numberPicker.getChildAt(0));
et.setTextColor(getResources().getColor(color));
}
this is my random color
private int [] textColours = new int[]{
R.color.text_color_1, R.color.text_color_2, R.color.text_color_3,
R.color.text_color_4, R.color.text_color_5, R.color.text_color_6,
R.color.text_color_7, R.color.text_color_8, R.color.text_color_9,
R.color.text_color_10
};
int randomColorPicker = (int)(Math.random() * textColours.length);
setNumberPickerTextColor(pickerOne, randomColorPicker);
So I used the link in the description with help from 0X0nosugar
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
//set the picker text color from the method below and the random number above
setNumberPickerTextColor(pickerOne, color);
setNumberPickerTextColor(pickerTwo, color);
setNumberPickerTextColor(pickerThree, color);
setNumberPickerTextColor(pickerTolerance, color);
and then using the answer from the link above this will randomly generate a random color everytime

Categories

Resources