How to Add Button Tint Programmatically

How to add button tint programmatically

According to the documentation the related method to android:backgroundTint is setBackgroundTintList(ColorStateList list)

Update

Follow this link to know how create a Color State List Resource.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#your_color_here" />
</selector>

then load it using

setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name));

where contextInstance is an instance of a Context


using AppCompart

btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary));

How to programmatically set backgroundTint of FloatingActionButton with ColorStateList?

This issue has been resolved as of Android Support Library 25.1.0

See: https://code.google.com/p/android/issues/detail?id=227428

Setting button tint on radio button programmatically

You can use setButtonTintList (ColorStateList tint)

Applies a tint to the button drawable. Does not modify the current tint mode, which is SRC_IN by default.

Subsequent calls to setButtonDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using setTintList(ColorStateList).

SAMPLE CODE

public class MainActivity extends AppCompatActivity {

RadioButton radioButton;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

radioButton = findViewById(R.id.radioButton);

ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{getResources().getColor(R.color.colorPrimaryDark)}
},
new int[]{getResources().getColor(R.color.colorAccent)}
);

radioButton.setButtonTintList(myColorStateList);

}

}

Can't change backgroundTint programmatically

To change programmatically the text color and the background color use:

  • the method setBackgroundTintList to change the background color selector.
  • the method setTextColor to change the text color

Something like:

    button.setBackgroundTintList(ContextCompat.getColorStateList(this,R.color.button_selector));
button.setTextColor(ContextCompat.getColor(this, R.color....));

In any case there are some issues in your style.

  • use a MaterialComponents.Button.* style as parent
  • use backgroundTint instead of android:backgroundTint
  • use android:textAppearance to define your text style

Something like:

<style name="buttonStyle" parent="@style/Widget.MaterialComponents.Button">
<item name="android:textColor">@color/default_button_textColor</item>
<item name="backgroundTint">@color/my_selector</item>
<item name="android:textAppearance">@style/my_textAppearance</item>
</style>
<style name="my_textAppearance" parent="@style/TextAppearance.MaterialComponents.Button">
<item name="android:textSize">18sp</item>
<item name="android:textAllCaps">true</item>
</style>

Change selector assigned to tint programmatically

Best way I found so far is to make a new color state list programmatically and assign it to the button, yikes, the goal was to avoid setting visual attributes like colors programmatically ...

        ColorStateList buttonStates = new ColorStateList(
new int[][] {
{ -android.R.attr.state_enabled },
{}
},
new int[] {
Color.RED,
Color.BLUE
}
);

buttonMinus.setImageTintList(buttonStates);

How to set tint for an image view programmatically in android?

UPDATE:

@ADev has newer solution in his answer here, but his solution requires newer support library - 25.4.0 or above.


You can change the tint, quite easily in code via:

imageView.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint

If you want color tint then

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);

For Vector Drawable

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);

How to set layout background tint from string programmatically?

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.

Programmatically change backgroundTint of ImageView with Vector Asset for background

Instead of using ImageView you can use AppCompatImageView, Because setBackgroundTintList is supported from API level 21, If you use AppCompatImageView you can change the tint color using setSupportBackgroundTintList.

so change you ImageView like this,

<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorRed"
android:background="@drawable/ic_delete"/>

So that you can call setSupportBackgroundTintList to set the tint color like this,

imageView.setSupportBackgroundTintList(ContextCompat.getColorStateList(this, R.color.colorBlue));

setBackgroundTintList for button programmatically with a hex value / ColorDrawable

on API +21

btn.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#buttonColor")));

or Compat

         Drawable drawable = new ColorDrawable(Color.parseColor("color"));
// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTint(drawable,Color.parseColor("colorTint"));
//or tint list
//DrawableCompat.setTintList(drawable,ColorStateList.valueOf(Color.parseColor("#ffffff")));
btn.setBackground(drawable); //apply drwable with tint to the ctn


Related Topics



Leave a reply



Submit