Reputation: 1566
I am styling my app and want to set the button background color and keep them material-like.
Set button background color
I am create style for button, set the colorPrimary
and set button style and colorButtonNormal
. 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>
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
.
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
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?
Upvotes: 10
Views: 14860
Reputation: 9283
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.
Upvotes: 16