0

I am new to Android and trying to create a simple app. However, I cannot seem to be able to set the background color of a button independently from the colorPrimary of the Layout.

Here is my activity_main code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/red_to_blue_back">


    <Button
        android:id="@+id/btnMovieList"
        style="@style/Theme.MovieShow"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/round_button"
        android:backgroundTint="#4CAF50"
        android:text="Movie List" />


</RelativeLayout>

And here what I have in my created drawable file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/custom"/>
    <corners android:radius="150dp"/>

</shape>

However, the button does not take the color of the drawable file, but whatever color is set as a colorPrimary in the themes.xml file.

Any thoughts?

Thanks!

  • Delete the style="@style/Theme.MovieShow" on the button – MichaelStoddart Nov 16 '20 at 09:21
  • You could defined button color on few levels: theme, style, backgroundTint, background. Your issue may be also related to this: https://stackoverflow.com/questions/64722733/android-background-drawable-not-working-in-button-since-android-studio-4-1 – 3mpty Nov 16 '20 at 09:41

3 Answers3

0

Hmmmmm...

Try removing the background tint attribute in xml. Also, ensure your drawable color doesn't have transparency.

If you must use the style attribute, create another one specific for your button and specify it's own colorPrimary..

This is because if your custom style has the main style as it's parent, it automatically uses some of its attribute.

Comments are welcome. Happy coding

0

To start from scratch first do some cleanup:

  • remove android:backgroundTint from button layout
  • remove android:style from button layout
  • go to manifest and change theme setup to non-material now (more in this answer)

With such setup you can start to experiment with android:background.

3mpty
  • 1,354
  • 8
  • 16
0

Thanks guys. I changed the theme to be non material and now it works