4
 <Button
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:text="Explore"
    android:id="@+id/btn_dialog"
    android:gravity="center_vertical|center_horizontal"
    android:layout_below="@+id/text_dialog"
    android:layout_marginBottom="20dp"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffff" />

This is my colors.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#00ccff</color>
    <color name="colorPrimaryDark">#00ccff</color>
    <color name="colorAccent">#00ccff</color>
    <color name="darkPrimary">#212121</color>
    <color name="darkPrimaryDark">#000000</color>
    <color name="whitecolor">#FFFFFF</color>
    <color name="switchcolor">#f0f0f0</color>
</resources>

The style file is given below:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

When I am trying to set the background color like this: android:background="#f0f0f0" , there is no change in the background color. I am trying to fix for about an hour or so. Please any help is appreciated.

Wowo Ot
  • 1,362
  • 13
  • 21
S M Vaidhyanathan
  • 320
  • 1
  • 4
  • 13
  • Does this answer your question? [Android button background color](https://stackoverflow.com/questions/18070008/android-button-background-color) – hata Apr 13 '20 at 12:54
  • 1
    Try giving the button app:backgroundTint="" to the button to change the background color – Kartik Apr 14 '20 at 05:15
  • https://stackoverflow.com/questions/63328270/android-button-background-is-taking-the-primary-color/63331089#63331089 – Gabriele Mariotti Sep 15 '21 at 10:42

7 Answers7

5

Use app:backgroundTint in your layout XML. It will change the color of the background from the primary color of the app

Will
  • 1,123
  • 1
  • 9
  • 22
  • it worked for me but still do not know what caused problem as it was working fine till couple days back and it suddenly started showing colorPrimary by default and now I am forced to add this app:backgroundTint="@color/white" as an additional attribute in Buttons – Akhilesh Jul 04 '21 at 20:18
1

change this in style file :

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

to

 <style name="AppTheme" parent="Theme.AppCompat">
  • 3
    Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes – Ran Marciano Dec 07 '20 at 12:13
1

I have searched all over the net looking for an answer to this question and have not found a single solution that would work properly in my Android project.

After a couple of hours, the only thing I figured out was: -create a new one style in "res/values"

-add in it a parameter named as "primaryColor" (repeating the name of the default color from the res/values/themes/themes.xml directive)

-set the desired color in it

-set the given style as the "theme" attribute for our button

What did I get:

my_button_style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="My_button_style">
        <item name="colorPrimary">@color/someColor</item>
    </style>
</resources>

and my button in GroupView.xml:

<Button
            style="@style/My_button_style"
            android:theme="@style/My_button_style"
    ...
/>

It works with Android API 20 and lower, while another solutions aren't. P.S. I am sure this solution is not entirely correct, but I am not the only one faced with this problem, and I have not found another solution on the net. I tried to solve the problem with selectors, but it didn't work. The solution to the problem using Java code does not suit me Thanks for attention!

1

Since you are using a Theme.MaterialComponents.* your Button is replaced at runtime by a MaterialButton.

Currently the backgroundTint is still the default MaterialButton style. It means that if you are using a custom android:background, you have to make sure to null out backgroundTint to avoid that the custom background doesn't get tinted with the attr/colorPrimary defined in your theme.

You have to add app:backgroundTint="@null":

app:backgroundTint="@null" android:background="@drawable/background"

1

That's because you set your theme to Theme.MaterialComponents.* Check this for more details

0

change in themes.xml file

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

to

 <style name="Theme.MyApplication" parent="Theme.AppCompat.DayNight.DarkActionBar">
Xlife
  • 21
  • 2
  • 2
    Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes – Alessio Jan 23 '21 at 17:44
0

First create btn_style in your styles.xml or themes.xml

<style name="btn_style" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@null</item>
</style>

then in you layout apply the style and use the color or drawable that you want

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:background="@color/Green"
            style="@style/btn_style"
            android:text="Button"/>
Wowo Ot
  • 1,362
  • 13
  • 21