How to set layout background tint from string programmatically? - android

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)

Related

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.

Android - Cannot change textcolor from xml

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

Android convert Transparent color to drawable

Is there a way how can I set the buttonDrawable of a CheckBox to transparent ?
Setting it like this: setButtonDrawable(Color.TRANSPARENT); doesn't work. I don't remember if there was a solution converting a color as a Drawable and use it like this for example. What I've searched so far and I saw it worked is to set the Transparent Color from XML but I don't have one and I would like to do it programmatically.
you can give new ColorDrawable(Color.TRANSPARENT) a try
I tried this and it worked:
new ColorDrawable(Color.parseColor("#00ffffff"));

How to change background image of a ImageButton in android

I am facing a situation in which-
1.First I have to set the background image of a ImageButton to a default image.
2.Now at runtime based on some condition I have to change the background image of that ImageButton to some other image.
I have tried this but it doesn't work and the background image remains to default image and
doesn't change.
btn.invalidateDrawable((Drawable)getResources().getDrawable(R.drawable.info));
btn.setBackgroundDrawable((Drawable)getResources().getDrawable(R.drawable.infonewred));
How can I achieve this goal.
Have you tried btn.setBackgroundResource(R.drawable.infonewred).
I suggest you set the "default" Background directly in your XML-Layout-File via android:background.
If you have to change the Background in Runtime just use btn.setBackgroundResource(R.drawable.imagename);
since you haven't provided xml, i am guessing you are providing src to imageButton.. so to change the source of the image , what you are doing is changing the image background which will not be visible since it is the source which will be visible to you.. so to change source..
do this.
btn.setImageDrawable()
rather than..
btn.setBackgroundDrawable()
I know that's old but i got the solution as i'm also was searching for that, the correct answer is to use setImageResource , so the code will be like that:
btn.invalidateDrawable(getResources().getDrawable(R.drawable.info));
btn.setImageResource(R.drawable.infonewred);
Try the method setImageResource
Ex:
if(someCondition()){
btn.setImageResource(R.drawable.info);
} else {
btn.setImageResource(R.drawable.infonewred);
}
Just try following code to change at runtime and for default view set background property in layout xml:
if(yourCondition)
btn.setBackgroundResource(R.drawable.infonewred);
in activity file you can write
btn.setBackgroundResource(R.drawable.sliderr);
OR
in your xml layout file you can add one extra line to you button xml code
android:src="#drawable/sliderr"

Color change of a button in code not reflecting

I'm trying to change a color of a button in code as below
btn_no5.setTextColor(0x8E35EF);
the above change is not reflecting(purple color), the text is disappearing after execution of above piece of code. But if i use the color in xml its reflecting. So how to change this through java ?
The color you are using is fully transparent. Use 0xFF8E35EF instead. The first byte is the alpha (transparency) channel.
use like this,
btn_no5.setTextColor(Color.parseColor("#8E35EF"));

Categories

Resources