mixing two gradient drawable for a view in android - 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.

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

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

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

selectableItemBackground color change for pre-lollipop versions

For Lollipop, ripple color can simply be modified using colorControlHighlight. But for pre-Lollipop (< API21), how can I change the color of pressed state achieved by setting the background to ?attr/selectableItemBackground
You can simply use this:
<item name="colorControlHighlight">#color/yourColor</item>
Edit: this is now possible using AppCompat and backgroundTint
backgroundTint="#color/yourColor"
Previous solution:
Not sure this is what you want but I ended up doing this programmatically:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ColorStateList colors = new ColorStateList(new int[][]{
new int[]{android.R.attr.state_enabled},
}, new int[]{pressed});
GradientDrawable item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(normal);
RippleDrawable ripple = new RippleDrawable(colors, item, null);
button.setBackgroundDrawable(ripple);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
GradientDrawable item;
item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(pressed);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, item);
item = new GradientDrawable();
item.setCornerRadius(radius);
item.setColor(normal);
stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, item);
button.setBackgroundDrawable(stateListDrawable);
}

Change Button Color programmatically

I have some Buttons in my App at different places. I want to color them according to the location they are in. (Blue Button in Fragment1, Red Button in Fragment2)
I already tried ColorFilter, but that doesn't work with API < 21. I know it is possible to create xml for that, but I don't want to create one for every section in my App. Is it possible to create somehow Backgroud Drawables, which have the color I specify and have the Button animations? (Ripple and selector for API < 21)
I found the solution:
**
* Sets the color of a button.
* #param button The Button which should be colored.
* #param color The color as a already retrieved value.
*/
public static void setButtonColor(Button button, int color) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//calculate darker color
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.8f;
int darkColor = Color.HSVToColor(hsv);
StateListDrawable listDrawable = new StateListDrawable();
GradientDrawable standard = new GradientDrawable();
GradientDrawable pressed = new GradientDrawable();
standard.setColor(color);
standard.setCornerRadius(3);
pressed.setColor(darkColor);
pressed.setCornerRadius(3);
listDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
listDrawable.addState(new int[]{android.R.attr.state_focused}, pressed);
listDrawable.addState(StateSet.WILD_CARD, standard);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
button.setBackgroundDrawable(listDrawable);
} else {
button.setBackground(listDrawable);
}
} else {
button.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
I don't know if this is the best selection, but it works.

Categories

Resources