android - Material button color
Just using AppCompatButton by setting the attribute "app:backgroundTint="@color/wildberries"
and make sure your activity extends AppCompatActivity.
I just use it in my project. It works like a charm in both 5.X and pre-5.X.
Alexey Markov
Updated on June 06, 2022Comments
-
Alexey Markov almost 2 years
I am styling my app and want to set the button background color and keep them material-like.
I want
Set button background color
I do
1.
I am create style for button, set the
colorPrimary
and set button style andcolorButtonNormal
. And it works.style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored"> <item name="android:textColor">@android:color/white</item> <item name="colorButtonNormal">@color/colorAccent</item> </style> ... <item name="android:buttonStyle">@style/AppTheme.Button</item> <item name="colorButtonNormal">@color/colorAccent</item>
2.
But I want to do one button with different color. I tried to use
backgroundTint
, but It doesn't work. So I create style:<style name="AppTheme.Button.Wildberries" parent="Widget.AppCompat.Button.Colored"> <item name="colorButtonNormal">@color/wildberries</item> </style>
It works on Android 4.x, but on Android 5.x button is still
colorAccent
.3.
I create style-v21 for button:
<style name="AppTheme.Button.Wildberries" parent="@android:style/Widget.Material.Button"> <item name="android:backgroundTint">@color/wildberries</item> </style>
Works in Android Studio:
Works on Android 4.x too
But doesn't work on 5.x
4.
Also I tried to use
AppCompat.Button
:<android.support.v7.widget.AppCompatButton xmlns:app="http://schemas.android.com/apk/res-auto" app:backgroundTint="@color/wildberries" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Перейти на сайт" android:id="@+id/WildBtn" android:textSize="14dp" android:textColor="@android:color/white" android:theme="@style/AppTheme.Button.Wildberries" android:stateListAnimator="@null" />
And got nothing:
On Android 4.x device button is still purple, on 5.x still green.
So, what is the way to set button background color on android 4.x and hihger?
-
Brais Gabin over 7 yearsJust a warnning:
app:backgroundTint="@color/wildberries"
isn't implemented in all theView
s. -
user924 over 2 yearsit will override colors for all states, disabled button will be the same as enabled button