How to add button tint programmatically

前端 未结 15 827
时光取名叫无心
时光取名叫无心 2020-11-30 23:38

In the new AppCompat library, we can tint the button this way:

相关标签:
15条回答
  • 2020-12-01 00:16

    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));
    
    0 讨论(0)
  • 2020-12-01 00:21

    There are three options for it using setBackgroundTintList

    int myColor = Color.BLACK;
    
    1. button.setBackgroundTintList(new ColorStateList(EMPTY, new int[] { myColor }));
    2. button.setBackgroundTintList(ColorStateList.valueOf(myColor));
    3. button.setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.my_color));
    0 讨论(0)
  • 2020-12-01 00:22

    Have you tried something like this?

    button.setBackgroundTintList(getResources().getColorStateList(R.id.blue_100));
    

    note that getResources() will only work in an activity. But it can be called on every context too.

    0 讨论(0)
  • 2020-12-01 00:24

    The suggested answer here doesn't work properly on android 5.0 if your XML based color state list references themed attributes.. For instance, I have an xml color state list like so:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="?colorPrimary" android:state_enabled="true"/>
        <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
    </selector>
    

    Using this as my backgroundTint from xml works just fine on android 5.0 and everything else. However if I try to set this in code like this:

    (Don't do this)

    myButton.setSupportButtonTintList(ContextCompat.getColorStateList(myButton.getContext(), R.color.btn_tint_primary));
    

    It actually doesn't matter if I pass the Activity or the button's context to ContextCompat.getColorStateList() method, neither will give me the proper color state list with respect to the theme the button is within. This is because using theme attributes in color state lists wasn't supported until api 23 and ContextCompat does not do anything special to resolve these. Instead you must use AppCompatResources.getColorStateList() which does its own resource parsing/theme attribute resolution on devices < API 23.

    Instead, you must use this:

    myButton.setSupportBackgroundTintList(AppCompatResources.getColorStateList(myButton.getContext(), R.color.btn_tint_primary));
    

    TLDR: use AppCompatResources and not -ContextCompat- if you'll need resolved themed resources across all API versions of android.

    For more information on the topic, see this article.

    0 讨论(0)
  • 2020-12-01 00:25

    For ImageButton you can use:

    favoriteImageButton.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
    
    0 讨论(0)
  • 2020-12-01 00:28

    In properly extending dimsuz's answer by providing a real code situation, see the following code snippet:

        Drawable buttonDrawable = button.getBackground();
        buttonDrawable = DrawableCompat.wrap(buttonDrawable);
        //the color is a direct color int and not a color resource
        DrawableCompat.setTint(buttonDrawable, Color.RED);
        button.setBackground(buttonDrawable);
    

    This solution is for the scenario where a drawable is used as the button's background. It works on pre-Lollipop devices as well.

    0 讨论(0)
提交回复
热议问题