Android change color of shape Instance - android

I have three buttons with same ripple shape.
when I enter activity I change color of single buttons shape.
RippleDrawable bg = (RippleDrawable) button.getBackground();
GradientDrawable gradient = (GradientDrawable) bg.findDrawableByLayerId(R.id.ripple_color);
gradient.setColor(Color.Black);
but when I reenter activity all buttons have this new color.
How to change only this instance of shape instead of modifying shape itself

From the Drawable documentation:
By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.
So your problem is here:
GradientDrawable gradient = (GradientDrawable) bg.findDrawableByLayerId(R.id.ripple_color);
gradient.setColor(Color.Black);
To avoid the issue mentioned in the documentation, simply call mutate() on the drawable before changing its state.
GradientDrawable gradient =
(GradientDrawable) bg.findDrawableByLayerId(R.id.ripple_color).mutate();
gradient.setColor(Color.Black);

Related

button dynamic background color with rounded edges

I managed to have rounded edge for my button.
I also managed to have a dynamic background color (taken from a webservice).
The problem is when doing this :
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(Color.parseColor(currentQuestion.backgroundColorButton));
One overrides the other, therefore I cannot have rounded edges AND dynamic background color.
I cannot use a dynamic color in the selector (as it's a static XML).
I cannot set the rounded edges programmatically (the method doesn't exists as far as I know).
How do I do ?
Use this
String backgroundColor= "#fc0000"; // set dynamic color here
btn.setBackgroundColor(Color.parseColor(backgroundColor));
another Examples:
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);
use this :-
final int color = Color.parseColor(homeCatPOJOS.get(position).getColor());
then implement it in background color :-
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(color));
I think you are getting color code in String, first convert into int then implement it.
And for round edges make XML file for it and implement it statically.

Color set by setColorFilter disappears on prelollipop devices

I have a TabLayout which has icons. The idea is to change the colors runtime.
I have and xml drawable file with states: state_pressed, state_selected and default with the same white picture so I can put color later.
I take the drawables for different states:
Drawable[] drawables = stateListDrawable.getConstantState();
and the for every drawable state I put color from another array:
drawables[i].setColorFilter(colors[i], PorterDuff.Mode.MULTIPLY);
The issue is that the color is visible in the beginning, but when I start to click on the icons all the icons becomes white again and I lose the tint.
Everything is working as expected on lollipop and above.
Use the tint method from the v4 Support library
drawables[i] = DrawableCompat.wrap(drawables[i])
DrawableCompat.setTint(drawables[i], colors[i])
I have found my solution, which does not look clean at all, but at least it is working :)
I have created CustomStateListDrawable which extends from StateListDrawable and added the drawables for the different states. Then I have overridden all the methods in the class to see which ones are called and tried to change the colors there. The called late enough(my changes will not be overridden after I make them) was getState(). I have created also a ColorStateList object to hold my colors so the code will look like this:
private ColorStateList colorStateList;
public int[] getState() {
if (colorStateList != null) {
// Resolve the color for the current state
int color = colorStateList.getColorForState(super.getState(), 0);
// Get the current drawable and changed its color.
if (getCurrent() != null) {
getCurrent().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
}
return super.getState();
}
Basically every time when there is a change in the state I get the current drawable and change its color.

how to make rounded corners button on runtime even if background is already set in xml

I understand how to make Button has rounded corners using GradientDrawable. The question I have now is if I set my background already in xml and retrieve it by
int[] attrsArray = new int[] {
android.R.attr.background
};
TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
Drawable background = ta.getDrawable(0);
ta.recycle();
so I have a Drawable type of background now. next thing I want to do is call setBackgroundDrawable() and pass the drawable background, but I won't be able to set rounded corners with Drawable type. I can't just cast it to GradientDrawable either. Is there a way to achieve this? this way makes things a lot easier because I'll be able to give Button any background and have the rounded corner.
Thanks
Not sure if your use case is same as mine but I had to once do something similar and I had an invisible Button with a RoundedImageView behind it. The RoundedImageView was below:
https://github.com/vinc3m1/RoundedImageView
Using that class you can set the radius of the image.

?android:attr/selectableItemBackground with another existing background

I have a 9patch set as the background of my layout. However I still want to provide touch feedback by using the selectableItemBackground attr.
I've tried using a <layer-list> with the 9patch and selectableItemBackground as the android:drawable of the second <item>, however that did not work.
I could also try making a selector and overlay the gradient drawable android uses for selectableItemBackground in list_selector_background_pressed.xml with a <layer-list>. But in 4.4 KitKat the selected background color is actually gray instead of blue in JellyBeans, so I can't really hardcode it :(
There has to be an easier way, right guys? D:
I've tried using a with the 9patch and
selectableItemBackground as the android:drawable of the second ,
however that did not work.
Yes, drawable attribute in a layer-list (or state-list) does not accept an attr value. You would see a Resource.NotFoundException. A look at LayerDrawable's (or StateListDrawable's) source code explains why: the value that you provide is assumed to be a drawable's id.
But, you can retrieve a theme and platform-specific drawable for an attribute in code:
// Attribute array
int[] attrs = new int[] { android.R.attr.selectableItemBackground };
TypedArray a = getTheme().obtainStyledAttributes(attrs);
// Drawable held by attribute 'selectableItemBackground' is at index '0'
Drawable d = a.getDrawable(0);
a.recycle();
Now, you can create a LayerDrawable:
LayerDrawable ld = new LayerDrawable(new Drawable[] {
// Nine Path Drawable
getResources().getDrawable(R.drawable.Your_Nine_Path),
// Drawable from attribute
d });
// Set the background to 'ld'
yourLayoutContainer.setBackground(ld);
You'll also need to set yourLayoutContainer's clickable attribute:
android:clickable="true"

Android ListView ScrollView change edge glow color from blue

Is it possible to change the ScrollView and ListView edge glow color from blue to orange.?
If you want to change color of glow, when you pull listview (the blue glow effect)
use
int glowDrawableId = getResources().getIdentifier("overscroll_glow", "drawable", "android");
int edgeDrawableId = getResources().getIdentifier("overscroll_edge", "drawable", "android");
Drawable androidGlow = ContextCompat.getDrawable(this, glowDrawableId);
Drawable androidEdge = ContextCompat.getDrawable(this, edgeDrawableId);
androidGlow.setColorFilter(getResources().getColor(R.color.white_20), PorterDuff.Mode.SRC_IN);
androidEdge.setColorFilter(getResources().getColor(R.color.white_20), PorterDuff.Mode.SRC_IN);
mode SRC_IN will show only your color. Place this code in Application class if color will be same or before initialisation of each individual listview (if you want different glow of different listviews).
If you asking about color change of fadingedge ( a shadow a top and a bottom of listview)
change color of colorCacheHint inside listview
use the Android Holo Colors Generator by Jérôme Van Der Linden
http://android-holo-colors.com/
The Android Holo Colors Generator allows you to easily create Android
components such as editext or spinner with your own colours for your
Android application. It will generate all necessary nine patch assets
plus associated XML drawables and styles which you can copy straight
into your project.
Select the views you want to generate colors for and add pngs & the theme from the XML it generates
Yes. Create drawable you like and set it for the listview:
android:overScrollHeader="#drawable/header"
android:overScrollFooter="#drawable/footer"

Categories

Resources