Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 738
Alexey Markov Avatar asked Apr 26 '16 20:04

Alexey Markov


People also ask

How change material button background color in android programmatically?

To programmatically set or change Android Button background color, we may call pass the method Button. setBackgroundColor() on the button reference and pass Color object as argument.

What is default color of button in android?

When I open a new android studio project, the default color for button is purple.


1 Answers

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.

like image 176
Jaden Gu Avatar answered Nov 08 '22 22:11

Jaden Gu