Change layout background in Android Studio

Issue

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">

Solution

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

Answered By – Rahim

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published