How to set random gradient background color? - android

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

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

Set GradientDrawable Color in Adapter

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.

drawable design 3 corners with one same color and else 1 corner with other color

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

Randomly Displaying Background Color in Android App

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

mixing two gradient drawable for a view in android

I am trying to draw some colors in the background of a view.
The colors are to be placed as linear gradients and in different directions.
So i did the following:
private Orientation[] orientations = new Orientation[]{Orientation.BL_TR, Orientation.BR_TL, Orientation.TL_BR, Orientation.TR_BL, Orientation.TOP_BOTTOM};
public void drawBackgroudGradient(RelativeLayout backgroundView, int[] colors){
if (colors != null){
if (colors.length > 1){
for (int i=0; i<colors.length; i++){
backgroundView.setBackgroundDrawable(getGradientDrawable(colors[i], orientations[i]));
}
}else{
//in case of only one color just set that color as background
backgroundView.setBackgroundColor(colors[0]);
}
}
}
private GradientDrawable getGradientDrawable(int color, Orientation orientation){
int[] colors = new int[] {color, Color.WHITE};
GradientDrawable drawable = new GradientDrawable(orientation, colors);
drawable.setAlpha(125);
return drawable;
}
I have each drawable going from the starting color to transparent so that all the gradients are visible.
But the problem is that all the colors are not showing up. Only the last color is drawn via the gradient drawable. the rest can not be seen.
Could someone please help me figure out how to mix and show all the colors?
Thanks.
Sunny
create LayerDrawable and mix them:
private LayerDrawable getGradientDrawable(int color, Orientation orientation){
int[] colors = new int[] {color, Color.WHITE};
GradientDrawable drawable1 = new GradientDrawable(orientation, colors);
drawable1.setAlpha(125);
GradientDrawable drawable2 = new GradientDrawable();
drawable2.setStroke(4, Color.parseColor("#FFFFFF"));
drawable2.setColor(Color.TRANSPARENT);
return new LayerDrawable(new Drawable[]{drawable1 , drawable2});
}
drawable2 placed on top of drawable1.

Categories

Resources