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?
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.
When I open a new android studio project, the default color for button is purple.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With