Reputation: 2604
I usually set the background color on a button simply in XML using android:background or programatically using setBackgroundColor but now with the AndroidX library (i assume...), these has no effect on the button color
it seems that setting android:backgroundTint is working but this works only for APIs 21 and above
how can i achieve this ?
example:
Shouldnt this produce a layout with a red backgrounded button...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#af2222"/>
</LinearLayout>
Upvotes: 3
Views: 2540
Reputation: 13575
I use this in android x instead of <Button
and background color will changes
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:background="@color/colorAccent"
android:text="Done"
/>
Upvotes: 4
Reputation: 2604
As svkaka's answer mentioned, i used app:backgroundTint="#af2222"
in xml to solve the issue.
Programatically, i used this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) statusButton.backgroundTintList = ColorStateList.valueOf(MY_COLOR)
else statusButton.setBackgroundColor(MY_COLOR)
Also to note, this is required on Both material and support design button since the android:background="#fff"
on both now seems to have no effect
Upvotes: 0
Reputation: 4032
You can try using: app:backgroundTint="#af2222"
and remove android:background="#af2222"
it should support <21 also
Upvotes: 2
Reputation: 636
You need to add style to your button tag as if you are using Androidx with material design.
style="@style/Widget.MaterialComponents.Button"
This style will let you set primaryColor as background color.
So your whole code will look like:
<android.support.design.button.MaterialButton
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Messages"
android:minWidth="200dp"
app:cornerRadius=”16dp”
app:icon="@drawable/ic_action_setting"
app:cornerRadius="@dimen/_16sdp"
app:backgroundTint="@color/colorAccent"
app:iconTint="@color/light_pitch"
app:iconPadding="-12dp"
/>
Cheers!
Upvotes: 2