Search code examples
androidxmlbuttonandroid-buttonandroid-theme

background image not showing only a blue square


I am using a button and setting the background to a png in my drawables folder. This is a method I have used on many occasions before. Using just a button and not imagebutton as it allows the flexibility to have relevant text on the button without unnecessary images.

    <Button
    android:id="@+id/button"
    android:onClick="holiday"
    android:layout_width="299dp"
    android:layout_height="199dp"
    android:layout_marginTop="70dp"
    android:background="@drawable/glassbtn63"
    android:gravity="center"
    android:padding="10dp"
    android:text="@string/set1"
    android:textColor="@color/blue"
    android:textSize="40sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

This code is working as required in previous apps but not is a new one, Does anyone know why?


Solution

  • It happens because you are using a Theme.MaterialComponents.* theme.
    The Button is replaced by a MaterialButton and the background is tinted with the ?attr/colorPrimary defined in your theme.

    Add app:backgroundTint="@null" in your Button to avoid it:

            <Button
                android:background="@drawable/..."
                app:backgroundTint="@null"