Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App with Java Creating the Screen Layout Setting Colors

Majdi Jaigirdar
Majdi Jaigirdar
2,984 Points

Background colour doesn't change, stays as default purple

The colour of the button doesn't change to white when I change it. And the android white doesn't seem to be alliable from the resource page, so I had to add it from to the XML code, this is what my button view code looks like:

<Button
        android:id="@+id/showFactButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="1dp"
        android:background="@android:color/white"
        android:text="Show another fun fact" />

However the button is still the default purple, I think the issue might have to do with Aneroids new 'material you' colour theme? When I tried to use @color/white or #FFFFFFFF it was still purple. When I run the app on my Pixel 6 , the button is a light purple colour instead.

Any ideas on why this is happening and how to change or fix this?

Edit & answer: I got help from ChatGPT So basically newer version of android use android:backgroundTint to define the button backgrounds, so I need to use: android:backgroundTint="@color/white" Instead of android:background="@android:color/white"

Majdi Jaigirdar
Majdi Jaigirdar
2,984 Points

Answer: I got help from ChatGPT, it said:

"In newer versions of Android, the button color is typically defined using the backgroundTint attribute instead of the background attribute. The background attribute is used for setting the background drawable for the button, while backgroundTint is used to specify the color tint applied to the background.

To change the button color to white, you can modify your code as follows:

<Button
    android:id="@+id/showFactButton"
    ...
    android:backgroundTint="@color/white"
    android:text="Show another fun fact" />

Make sure you have a white color defined in your colors.xml file under the res/values/ directory. If you don't have it, you can add it manually:

<color name="white">#FFFFFF</color>

With these changes, the button should now have a white background color."

So basically new version of android use android:backgroundTint to define the button backgrounds, so I need to use:

android:backgroundTint="@color/white"