Home > Enterprise >  How to change the color of material button when it's pressed on?
How to change the color of material button when it's pressed on?

Time:05-18

I changed the color of materialButton in themes.xml by overlaying the default theme:

<style name="ShapeAppearanceOverlay.Roomie.Button" parent="Widget.MaterialComponents.Button">
        <item name="cornerFamily">rounded</item>
        <item name="colorPrimary">#C4C4C4</item>
        <item name="cornerSize">50%</item>
    </style>

<style name="IncrementButtonTheme" parent="Widget.MaterialComponents.Button">
        <item name="materialThemeOverlay">@style/ShapeAppearanceOverlay.Roomie.Button</item>
    </style>

<com.google.android.material.button.MaterialButton
            android:id="@ id/btn_plus"
            style="@style/IncrementButtonTheme"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:insetLeft="0dp"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:onClick="increment"
            app:icon="@drawable/ic_plus"
            app:iconGravity="textStart"
            app:iconPadding="0dp"
            app:iconSize="30dp"
            app:layout_constraintHorizontal_weight="5"
            app:layout_constraintBottom_toTopOf="@ id/btn_zapisz_produkt"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/ilosc"
            app:layout_constraintTop_toBottomOf="@id/nazwa_marki"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Roomie.Button" />

But now i want to set another background color for when it's pressed on. How can I do that?

CodePudding user response:

One of the best ways to create enable/disable effect is by overriding the MaterialButton class and apply ColorMatrix on it

import android.content.Context
import android.graphics.Canvas
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.util.AttributeSet
import com.google.android.material.button.MaterialButton

class MaterialButtonCustom @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
): MaterialButton(context,attrs,defStyleAttr) {
    var disabled = false
        set(value) {
            field = value
            requestLayout()
        }

    private val paint = Paint()

    init {
        val cm = ColorMatrix()
        cm.set(
            floatArrayOf(
                0.6f, 0.6f, 0.6f, 0f, 0f,
                0.6f, 0.6f, 0.6f, 0f, 0f,
                0.6f, 0.6f, 0.6f, 0f, 0f,
                0f, 0f, 0f, 1f, 0f
            )
        )
        paint.colorFilter = ColorMatrixColorFilter(cm)
    }

    override fun dispatchDraw(canvas: Canvas?) {
        if (disabled) {
            canvas?.saveLayer(null, paint)
        }

        super.dispatchDraw(canvas)

        if (disabled) {
            canvas?.restore()
        }
    }

    override fun draw(canvas: Canvas?) {
        if (disabled) {
            canvas?.saveLayer(null, paint)
        }

        super.draw(canvas)

        if (disabled) {
            canvas?.restore()
        }
    }
}

and use that in your Activity like this

class MainActivity : AppCompatActivity() {
    lateinit var btn_plus:MaterialButtonCustom
    var isActive: Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn_plus = findViewById(R.id.btn_plus)
        setBtnEnabled()
        btn_plus.setOnClickListener {
            isActive = !isActive
            if(isActive){
                setBtnDisabled()
            }else{
                setBtnEnabled()
            }
        }

    }

    private fun setBtnDisabled() {
        btn_plus.disabled = true
    }

    private fun setBtnEnabled() {
        btn_plus.disabled = false
    }
}

CodePudding user response:

You can try this one:

btn_plus.setOnClickListener(v ->
    {
        int color = Color.parseColor("#99cc00");
        btn_plus.getBackground().mutate().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC));
    });
  • Related