I'm using the lasted support design : 28, alpha3.
I use using the "Theme.MaterialComponents.Light.NoActionBar" as the theme for my application and "MaterialButton" instead of a normal "Button" in my layouts.
I can set the BackgroundTind from the XML as normal but i can't change it via java.
I tried:
deliverSwitch.setBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));
deliverSwitch.setSupportBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));
but none of them worked... I also tried to clear the current tint by leaving the setBackgroundTintList null and it doesn't work either.
I couldn't get it working either. As a workaround I did the following: First you get the current background Drawable, then you tint it with the desired color and set the new background with setBackgroundDrawable for your Material Button.
Drawable background = materialButton.getBackground();
background.setTint(ContextCompat.getColor(getContext(), R.color.bg_button_primary));
materialButton.setBackgroundDrawable(background);
I hope that helps.
Related
I want to make the android chip background color to transparent.
From material version higher than 1.1.0,
we need to set app:chipBackgroundColor="#color/transparent" but also app:chipSurfaceColor="#color/transparent"
Ok, I see. But how can it do by kotlin code?
background color can be set by code below...
chipBackgroundColor = ColorStateList.valueOf(ContextCompat.getColor(context, android.R.color.transparent))
But, I can't find the code that changes its surface color!!
Is there any solution?
chip.chipBackgroundColor = getColorStateList(your color)
I need to reuse XML layout and change button color programmatically.
In Android 5 applying app:backgroundTint in XML changes button color but I need to do it programmatically and I do it in Recyclerview:
holder.button.supportBackgroundTintList = ContextCompat.getColorStateList(context, backgroundColorRes)
This has no effect.
The setSupportBackgroundTintList() method is annotated with #RestrictTo({Scope.LIBRARY_GROUP}), which means you are not supposed to call it directly. Instead, you should use ViewCompat.setBackgroundTintList().
Try changing your code to this instead:
val colorStateList = ContextCompat.getColorStateList(context, backgroundColorRes)
ViewCompat.setBackgroundTintList(holder.button, colorStateList)
If you look at the source code for ViewCompat.setBackgroundTintList(), you'll see that it does different things for API 21+ (Android 5 and higher) than earlier versions. Chances are good that the "support" background tint is only applied on earlier versions of Android, and ViewCompat will make it so that you don't have to think about that.
I am changing the app theme with a switch toggle. Then all the XML contents work perfectly. But I need a color in my java code.
I tried :
getResources.getColor(R.attr.text_color);
but it is not working.
Thank you.
I tried this code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.setBackgroundTintList(context.getResources().getColorStateList(Color.parseColor("#ff8800")));
But I'm getting an error: android.content.res.Resources$NotFoundException
I'm getting the color hex from external source so I can't embed it in colors.xml.
Also I want to change the tint, not the background so setBackground is not an option.
I figured I can't use getColorStateList() so I searched for another way to do it.
At the end I was able to set color tint using the following code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
This worked as if I changed the backgroundTint property in the xml file, so it's perfect for my problem.
I was able to manage using the following line. change it to your circumstances.
myView.getBackground().setTint(currentView.getResources().getColor(R.color.colorAccent));
For Kotlin ,
I modified #Krestek answer :
someLayout.background.setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP)
You can't do this like that because getColorStateList method expect int id of resource, and you are passing RGB color int. You should create color state list following this link
and then set it like this:
.getColorStateList(R.color.your_xml_name)
I'm stuck with a very strange issue.
I can't change the TextColor of some TextViews around the app, neither with the android:textColor="" nor by setting a style. It only works if I change it at runtime. This problem appeared without any change related to the activity in question. The most strange thing is that in the Preview the colors are fine, but when I run it, the colors are always the same.
What might be overriding the textColor value set in the XML ?
Thx Anticipately
P.S:
Along all the App I can only change the color of the textHint
I had to remove all the N preview stuff from my sdk to make things normal again. Don't forget the cache too.
sdk/extras/android/m2repository/com/android/support/design|support-v-13|ect./24~
The syntax of xml line you wrote is wrong
instead of
android:setTextColor=""
use
android:textColor=""
You can change the Text Color in layout XML by using android:textColor
example:
android:textColor="#0E0E9A"
it overrides the style.xml
the only way to override your layout.xml is by code
example:
mEditText.setTextColor(Color.DKGRAY);