Can't change background color on MaterialButton without change colorAccent
1st Solution
You can use app:backgroundTint
to change back ground color of MaterialButton
<com.google.android.material.button.MaterialButton android:id="@+id/bittrexJsonViewButton" android:layout_width="0dp" android:layout_height="@dimen/min_height" android:layout_marginStart="@dimen/half_default_margin" android:layout_marginEnd="@dimen/half_default_margin" app:backgroundTint="@android:color/holo_orange_dark" android:text="@string/json_view" app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton" app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />
2nd Solution
MaterialButton
uses colorPrimary
as background when button is in active state and colorOnSurface
when disabled. So, you can define it in your theme and apply it on material buttons
With the MaterialButton
you have 2 options:
Using the
backgroundTint
attribute as suggest by Zaid MirzaIf you want to override some theme attributes from a default style you can use new
materialThemeOverlay
attribute. It is the best option in my opinion.
Something like:
<style name="Widget.App.ButtonStyle" parent="Widget.MaterialComponents.Button"> <item name="materialThemeOverlay">@style/GreenButtonThemeOverlay</item></style><style name="GreenButtonThemeOverlay"> <item name="colorPrimary">@color/green</item></style>
and then:
<com.google.android.material.button.MaterialButton style="Widget.App.ButtonStyle" ../>
It requires at least the version 1.1.0 of the library.
If you want to set custom drawable you need to make the app:backgroundTint="@null"
. For just changing the background colour app:backgroundTint="@color/yourColor"
I'm currently using 1.3.0-alpha01
<com.google.android.material.button.MaterialButton android:id="@+id/bittrexJsonViewButton" android:layout_width="0dp" android:layout_height="@dimen/min_height" android:layout_marginStart="@dimen/half_default_margin" android:layout_marginEnd="@dimen/half_default_margin" app:backgroundTint="@null" android:background="@drawable/your_custom_drawable" android:text="@string/json_view" app:layout_constraintBottom_toBottomOf="@+id/binanceJsonViewButton" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/binanceJsonViewButton" app:layout_constraintTop_toTopOf="@+id/binanceJsonViewButton" />