Change thumb color from a seekbar - android

I have a seekbar and i am trying to change the thumb color. I found two photos that describes what i actually want to do.
I have this seekbar (default) :
and i am trying to change it's color thumb like this.. :
(I know how to change background progress color but not the thumb color)..
seekBar1calling.getProgressDrawable().setColorFilter(Color.WHITE, Mode.SRC_IN);
Is there any way to achieve this using java code? Thanks in advance!!

I just used an online style generator which gave me what i needed
http://android-holo-colors.com/
Just choose the widget and color you want and you are ready! ;)

Just use:
mySeekbar.getThumb().setColorFilter(myColor, PorterDuff.Mode.MULTIPLY);
Edit: method getThumb is only available since API 16+ (Jelly Bean).

The other answers are old and didn't work for me, this did.
seekbar.setThumbTintList(ContextCompat.getColorStateList(this, R.color.disabled));

Here is a quick and easy solution which worked for me as expected. Just implement this code after you initialized the seekbar to avoid NPE's.
You have to define the color itself under res/values/colors.xml
int blue = ContextCompat.getColor(context, R.color.seekbar_blue);
filterSeekBar.getThumb().setColorFilter(blue, PorterDuff.Mode.SRC_ATOP);
filterSeekBar.getProgressDrawable().setColorFilter(blue, PorterDuff.Mode.SRC_ATOP);
That's all!
Note: The color of the Progress-Background is not as dark as the Button / Thumb itself.
If you want to know more about the PorfuerDuff part check this answer on Stackoverflow:
Phasmal & Lee's answer to PorfuerDuff modes

Related

Android Chip_How to make chip background color transparent programmatically?

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)

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

How to reset a button's background color to default?

I read a couple of posts but none of them had the working solution.
Once you do
button.setBackgroundColor(0x00000000);
How do you revert the button's background color back to default color?
use:
btn.setBackgroundResource(android.R.drawable.btn_default);
If the background color was set using
btn.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
it can be reset using:
btn.getBackground().clearColorFilter();
In contrast to button.setBackgroundColor() setting the color this way preserves the button's shape.
Nobody mentioned TRANSPARENT
use it like this
findViewById(R.id.button_id).setBackgroundColor(Color.TRANSPARENT);
Thank me later
this worked better for me :
Button defbtn=new Button(this);
btn.setBackground(defbtn.getBackground());

android set button background programmatically

I would like to know how to set the button color programatically?
I have coded the following but fails:
Button11.setBackgroundColor(R.color.red);
Thanks!!
R.color.red is an ID (which is also an int), but is not a color.
Use one of the following instead:
// If you're in an activity:
Button11.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not:
Button11.setBackgroundColor(Button11.getContext().getResources().getColor(R.color.red));
Or, alternatively:
Button11.setBackgroundColor(Color.RED); // From android.graphics.Color
Or, for more pro skills:
Button11.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB
Old thread, but learned something new, hope this might help someone.
If you want to change the background color but retain other styles, then below might help.
button.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
The answer you're looking for in 2020 and beyond:
setColorFilter(color, mode) is deprecated since API 29 (as discussed here)
button.setBackgroundColor(color) messes with the button style
Now the proper way to set a buttons color is using BlendModeColorFilter() (see documentation).
Usage:
btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY)
If you work with older APIs too:
fun setButtonColor(btn: Button, color: Int) {
if (Build.VERSION.SDK_INT >= 29)
btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY)
else
btn.background.setColorFilter(color, PorterDuff.Mode.MULTIPLY)
}
Please vote to help others finding this answer - it took me quite a while figuring this out ^^
You can set your desired color to the button programmatically like:
Button11.setBackgroundColor(Android.Graphics.Color.parseColor("#738b28"));
Also you can give the text color for a button like:
Button11.setTextColor(Android.Graphics.Color.parseColor("#FFFFFF"));
I have found that Android Studio gives me a warning that getColor() is deprecated when trying to do this:
Button11.setBackgroundColor(getResources().getColor(R.color.red))
So I found doing the method below to be the simple, up-to-date solution:
Button11.setBackgroundColor(ContextCompat.getColor(context, R.color.red))
You want to avoid hard-coding in the color argument, as it is considered bad code style.
Edit: After using setBackgroundColor() with my own button, I saw that the internal button padding expanded. I couldn't find any way of changing it back to having both height and width set to "wrap_content". Maybe its a bug.
Source:
https://stackoverflow.com/a/32202256/6030520
For not changing the size of button on setting the background color:
button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
this didn't change the size of the button and works with the old android versions too.
Using setBackgroundColor() affects the style.
So, declare a new style of the same properties with respect to the previous button, with a a different color.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/green"/>
<corners android:radius="10dp"/>
</shape>
Now, use OnClick method.
location.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
location.setBackgroundResource(R.drawable.green);
}
});
this changes the button but looks similar to changing the background.
Further from #finnmglas, the Java answer as of 2021 is:
if (Build.VERSION.SDK_INT >= 29)
btn.getBackground().setColorFilter(new BlendModeColorFilter(color, BlendMode.MULTIPLY));
else
btn.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
button.setBackgroundColor(getResources().getColor(R.color.red);
Sets the background color for this view.
Parameters: color the color of the background
R.color.red is a reference generated at the compilation in gen.
You can use the below code for the button color without getting effected the style if used any
yourbtnid.background.setColorFilter(ContextCompat.getColor(context, yourcolor), PorterDuff.Mode.SRC)

Categories

Resources