Android - Cannot change textcolor from xml - android

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

Related

Setting Default Colors from android Programmatically

I am having a problem, i know how to set colors programmatically using the following method
For Example:
imageButton.setColorFilter(ContextCompat.getColor(this,R.color.colorAccent));
This works just fine what about if i don't want to set the colors from colors.xml for example
the following colors in XML
android:background="#android:color/holo_orange_dark"
I want to set the colors above programmatically i don't want to use the colors in colors.xml
Use this
getResources().getColor(android.R.color.holo_orange_dark)
Here's the full code:
imageButton.setColorFilter(getResources().getColor(android.R.color.holo_orange_dark));
Hope this helps. Feel free to ask for clarifications...

Changing BackgroundTint of a MaterialButton

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.

How to set TextColor from Custom style attribute(R.attr.text_color) Android

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.

How to set layout background tint from string programmatically?

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)

android change view's background color when state changes

how to use color state list for background?
I know android:background="#drawable/drawable_selector", but android:background="#color/color_selector" will cause exceptions.
but android:background="#FFFFFF" works again, can anyone explains why?
now i want to change a layout's background color(not a drawable) when it's pressed,
how to do it?
dynamically you can change like this. Use this if it is useful for you -
textView.setBackgroundColor(Color.parseColor(getResources().getString(R.string.red)));
put the color in res/values/colors.xml,
like #FFFFFF,
and then create a drawable xml in drawable directory,
,that is ok.

Categories

Resources