How set background image and fit that image entire card for card view in Android?
Asked Answered
L

2

6

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

Langley answered 29/6, 2020 at 5:23 Comment(2)
You can set cardview background image with android:background="@drawable/app_ads_background"Warga
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.Langley
T
2

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>
Tarsometatarsus answered 29/6, 2020 at 8:49 Comment(14)
Thanks for the reply, can you explain more a bit. a little bit confusion on the explanation.Langley
In ConstraintLayout, If yout want a view with width (or height) wrap_content, you need to constraint position with layout_constraint, or if with match_parent, you do not need to constraint anything. These features depend on ConstraintLayoutTarsometatarsus
FYI, I usually add layout_constraintStart & layout_constraintEnd and set 0dp to layout_width to match parent horizontally.Tarsometatarsus
Thanks for the reply, What is the value to be added in layout_constraintStart and layout_constraintEnd?Langley
Sorry, layout_constraintStart/End is not a truly attr, it just in shortening and means StartToStartOf, StartToEndOf, EndToStartOf, EndToEndOf .. etc, And the value what we constraint with is parentTarsometatarsus
Can you please write code for that, even though after it comes.. please refer the image I attached Link below the code.Langley
You want your image fit to cardview right? You can try change the ImageView params with my answer above.Tarsometatarsus
But it's not working, it remains same.. can write the code fully? please.Langley
I have Updated answer, please check.Tarsometatarsus
Sorry, bro even though I put as you said, it not working... I think I need to scale image.Langley
You can do that as also, but I guess you will face same problem on different resolution .. Sorry for not help :(Tarsometatarsus
Thank for your help bro.Langley
I am just adding android:scaleX="1.1" android:scaleY="1.1" to adjust the image,it working,but seems it not good approch.Langley
You're right. I think it will occur some effects of blur with image if it is scaled.Tarsometatarsus
W
0

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.

Weinstein answered 15/12, 2020 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.