Alexey Markov
Alexey Markov

Reputation: 1566

android - Material button color

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

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:

IDE

Works on Android 4.x too

But doesn't work on 5.x

enter image description here

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:

enter image description here

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

Answers (1)

Jaden Gu
Jaden Gu

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

Related Questions