Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change background color on MaterialButton without change colorAccent

Android Studio 3.2.1 Here my layout:

<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"                 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" /> 

to change MaterialButton's background I change colorAccent in styles.xml

<item name="colorAccent">@color/colorAccent</item> 

Nice. It's work.

But the problem is: I do not want to change colorAccent. I want to use background's color for MaterialButton's different from colorAccent

Attribute:

android:background="#aabbcc" 

not help.

like image 507
Alexei Avatar asked Mar 10 '19 11:03

Alexei


People also ask

How change material button background color in android programmatically?

I try change color to MaterialButton with this code: var materialButton = findViewByid(R.id....) as MaterialButton materialButton. setBackgroundColor( ContextCompat. getColor(this@MyActivity, R.

Do not set the background MaterialButton manages its own background drawable?

MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.

How do you give a button a color in flutter?

To change the Text Button color in Flutter, simply add the style parameter inside the Text Button and assign the TextButton. styleFrom() with the primary property set to any color of your choice.


1 Answers

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

like image 186
Zaid Mirza Avatar answered Oct 05 '22 19:10

Zaid Mirza