How to add button tint programmatically

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

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

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

    In addition to Shayne3000's answer you can also use a color resource (not only an int color). Kotlin version:

    var indicatorViewDrawable = itemHolder.indicatorView.background
    indicatorViewDrawable = DrawableCompat.wrap(indicatorViewDrawable)
    val color = ResourcesCompat.getColor(context.resources, R.color.AppGreenColor, null) // get your color from resources
    DrawableCompat.setTint(indicatorViewDrawable, color)
    itemHolder.indicatorView.background = indicatorViewDrawable
    
    0 讨论(0)
  • 2020-12-01 00:36

    You can use DrawableCompat e.g.

    public static Drawable setTint(Drawable drawable, int color) {
        final Drawable newDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(newDrawable, color);
        return newDrawable;
    }
    
    0 讨论(0)
  • 2020-12-01 00:38

    If you are using Kotlin and Material Design, you can change color of your MaterialButton like this:

    myButton.background.setTintList(ContextCompat.getColorStateList(context, R.color.myColor))
    

    You can improve it even better by creating an extension function for your MaterialButton in order to make you code more readable and your coding little more convenient:

    fun MaterialButton.changeColor(color: Int) {
        this.background.setTintList(ContextCompat.getColorStateList(context, color))
    }
    

    Then, you can use your function everywhere like this:

    myButton.changeColor(R.color.myColor)
    
    0 讨论(0)
  • 2020-12-01 00:40

    this is easily handled in the new Material Button from material design library, first, add the dependency:

    implementation 'com.google.android.material:material:1.1.0-alpha07'
    

    then in your XML, use this for your button:

    <com.google.android.material.button.MaterialButton
        android:id="@+id/accept"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/i_accept"
        android:textSize="18sp"
        app:backgroundTint="@color/grayBackground_500" />
    

    and when you want to change the color, here's the code in Kotlin, It's not deprecated and it can be used prior to Android 21:

    accept.backgroundTintList = ColorStateList.valueOf(ResourcesCompat.getColor(resources, 
    R.color.colorPrimary, theme))
    
    0 讨论(0)
  • 2020-12-01 00:41

    You could use

    button.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.blue_100)));
    

    But I would recommend you to use a support library drawable tinting which just got released yesterday:

    Drawable drawable = ...;
    
    // Wrap the drawable so that future tinting calls work
    // on pre-v21 devices. Always use the returned drawable.
    drawable = DrawableCompat.wrap(drawable);
    
    // We can now set a tint
    DrawableCompat.setTint(drawable, Color.RED);
    // ...or a tint list
    DrawableCompat.setTintList(drawable, myColorStateList);
    // ...and a different tint mode
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);
    

    You can find more in this blog post (see section "Drawable tinting")

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

    Seems like views have own mechanics for tint management, so better will be put tint list:

    ViewCompat.setBackgroundTintList(
        editText, 
        ColorStateList.valueOf(errorColor));
    
    0 讨论(0)
提交回复
热议问题