5
votes

Here I am trying to fit the entire image as the background for card view. but it takes some space as shown in the picture. I tried android:scaleType="centerCrop" and fitXY and others also but it not responding. In attached image violet color represents empty space that occupied. I need that spaces need to occupied with the background image. Here is my code.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/voilet">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/application_ads_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerInside"
            android:src="@drawable/app_ads_background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />

        <Button
            android:id="@+id/application_ads_install_button"
            android:layout_width="74dp"
            android:layout_height="34dp"
            android:layout_marginEnd="48dp"
            android:background="@color/voilet"
            android:text="@string/application_ads_install"
            android:textColor="@color/white"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/application_ads_image"
            app:layout_constraintTop_toTopOf="@+id/application_ads_image"
            app:layout_constraintVertical_bias="0.674" />

        <TextView
            android:id="@+id/application_ads_review"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/application_ads_review"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="@+id/application_ads_image"
            app:layout_constraintEnd_toStartOf="@+id/application_ads_install_button"
            app:layout_constraintHorizontal_bias="0.27"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/application_ads_name"
            app:layout_constraintVertical_bias="0.567" />

        <TextView
            android:id="@+id/application_ads_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="Zira"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="@+id/application_ads_image"
            app:layout_constraintHorizontal_bias="0.176"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

This is Image of my output as per my code

2
You can set cardview background image with android:background="@drawable/app_ads_background"Noban Hasan
Thanks for reply. Image can't be set as Background Image For a Card View. Even though I made the change as you tell, it's not working.Natheeshkumar Rangasamy

2 Answers

2
votes

In ConstraintLayout, if you want to make width & height fit to parents, there are two options.

1.Within layout_constraint attrs, you need to set 0dp on layout_width & layout_height

<ImageView
        android:id="@+id/application_ads_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

2.If you set match_parent, you don't need to set layout_constraint. If set, it will effect just like wrap_content

<ImageView
        android:id="@+id/application_ads_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"/>

Update

I write a sample for you, you can check the result like this

Result

<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/voilet">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/application_ads_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/app_ads_background"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <Button
            android:id="@+id/application_ads_install_button"
            android:layout_width="74dp"
            android:layout_height="34dp"
            android:layout_margin="4dp"
            android:background="@color/voilet"
            android:text="INSTALL"
            android:textColor="#FFF"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

        <TextView
            android:id="@+id/application_ads_review"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginStart="16dp"
            android:text="5/5"
            android:textColor="#FFF"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/application_ads_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:text="Zira"
            android:textColor="#FFF"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
0
votes

I tried with your layout file using my local resource drawable image with ImageView fitXY or matrix scaleType, ImageView picture fits into the card view perfectly. something like the following

<ImageView
        android:id="@+id/application_ads_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/a1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

a1 is my local drawable image file, @drawable/a1. If you still have issue, i will suggest you to check with image/icons dimensions or check the parent view where you are using this layout card.