In this android project im creating a default button style. my styles.xml
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:buttonStyle">@style/ButtonStlye123</item> </style> <style name="ButtonStlye123" parent="android:style/Widget.Button"> <item name="android:textSize">19dp</item> <item name="android:layout_margin">8dp</item> <item name="android:padding">8dp</item> <item name="android:textColor">@color/ColorDivider</item> <item name="android:background">@color/ColorAccent</item> </style>
my button in a fragment.
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btnLogin" android:id="@+id/btn_login" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:theme="@style/ButtonStlye123" android:onClick="login" />
In the preview in android studio it looks exactly like I want, but when I run the app on my device the background color is gray ("default button color"). If i change the text color in my styles.xml like:
<item name="android:textColor">@color/ColorPrimary</item>
it changes (also on my device) so the custom style is loading, i tryd different colors (that worked for the text) for the button but it wont change.
Why isnt my background color loading?
To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.
Just go to res/values/styles.edit the xml file to change the color of action bar. Code for styles.
Using colors (background, text, border) Using custom shapes like circle, rounded corners and more. You can add images to your buttons to customize them. Using drawables to make gradients, dotted borders and more.
Try use AppCompactButton instead of
<Button
use
<androidx.appcompat.widget.AppCompatButton
that will do the trick
Update: 01/06/2021
Found out that the above will not work on earlier Android versions. For materiel design Button use
app:backgroundTint="@color/green"
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