Change layout background in Android Studio

Viewed 36

Although I used to change the background with the code below, but now the program crashes

binding.cardLAYOUT.setBackgroundResource( R.drawable.gift_card_100);

or

binding.cardLAYOUT.setBackground(ContextCompat.getDrawable(this, R.drawable.gift_card_1));

I did not change anything, the images and codes are the same as before

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cardLAYOUT"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="24dp"
    android:background="@drawable/gift_card_13"
    android:clickable="true"
    app:layout_constraintDimensionRatio="100:60"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline32">
2 Answers

Recently I've been using android:src = "@mipmap/your_picture" to insert a picture. I know that you're trying to set a background, but if your background is .png, you should save it in mipmap.mdpi in stead of drawable. Maybe that's why ur programm crashes.

The problem was solved by moving the png files to the drawable-xxxhdpi directory

Related