Android Button background color not changing
AndroidAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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"
Solution 2 - Android
Please Use androidx.appcompat.widget.AppCompatButton insteas of Button.
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_id_Login"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@id/textInnputLayout_editText_id_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/Login_screen_button_text"
android:background="@drawable/login_button_style"
android:textAllCaps="false">
</androidx.appcompat.widget.AppCompatButton>
Solution 3 - Android
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
Replace with
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Changing MaterialComponents to AppCompat works for me..
Solution 4 - Android
You might be using targetSdkVersion 30
material theme
The solution is change theme.xml
style
From
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
To
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Solution 5 - Android
I think you need to change your attribute from "theme" to "style". These files are intended for different things, so are used differently.
Style is used to assign attributes to individual views or components, and Theme is used to apply attributes to the entire app.
So try this instead:
<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"
<!-- This attribute was changed -->
android:style="@style/ButtonStlye123"
android:onClick="login" />
You should also split these different types of XML into the correct files as appropriate (styles.xml and themes.xml - instead of keeping them all in the same file). This is convention, and won't effect the code compilation or execution, but is recommended as a code style.
Solution 6 - Android
if you are using Android Studio Version 4.1 and above (Beta) check your themes.xml file in values.
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
</style>
change the attribute to @null
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@null</item> <!-- HERE -->
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
</style>
for other old versions of Android Studio
just change this attribute
<item name="colorPrimary">@color/colorPrimary</item>
to
<item name="colorPrimary">@color/@null</item>
finally set the background of the button to the drawable icon you want. example:
<Button
android:id="@+id/btnSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher_background" /> <!--the drawable you want-->
Solution 7 - Android
You only need to change your App Theme from Theme.MaterialComponents.DayNight.DarkActionBar
to Theme.AppCompat.Light.NoActionBar
inside styles.xml file
example :
from : style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"
To : style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
Solution 8 - Android
Use this attribute backgroundTint
it will help you. It work for me. Example :
android:backgroundTint="@color/md_orange_800"
Solution 9 - Android
your style should be like this....
<style name="ButtonStlye123" >
<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>
and in layout
<Button
android:id="@+id/btn3"
style="@style/ButtonStlye123"
android:layout_width="@dimen/sixzero"
android:layout_height="@dimen/sixzero"
android:text="3" />
Solution 10 - Android
I am using targetSdk 30 and compileSdk 32 and I am using Button widget. Android Studio version 2021.1.1
In my case, following change worked,
In AndroidManifest.xml, under "application" tag changing "android:theme" from
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
to
android:theme="@style/Theme.MaterialComponents.DayNight.NoActionBar"
I moved to Theme.AppCompat.Light.NoActionBar to remove the title bar. At first I tried to change "style" to "Theme.AppCompat.Light.NoActionBar" inside themes.xml but it didn't work for me.