2

I have an image square image, and my ImageView in my CardView is also square, but it shows me a picture like this: Picture

Original picture:

Original

This is my Layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@android:color/white"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true"
    android:layout_margin="10dp">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="374dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/illness_image"
            android:layout_width="364dp"
            android:layout_height="364dp"
            android:background="?attr/colorPrimaryDark"
            android:cropToPadding="true" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/illness_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textAppearance="?attr/textAppearanceHeadline6"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/illness_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Description"
                android:textAppearance="?attr/textAppearanceBody2"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/illness_engagement"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="Engagment"
                android:textAppearance="?attr/textAppearanceBody2"
                android:textSize="10sp" />
        </LinearLayout>
    </LinearLayout>
</android.support.design.card.MaterialCardView>

EDIT: Sorry, but i don't get it. I can't insert my XML code.

9
  • Show xml file..
    – Piyush
    Dec 18, 2018 at 10:00
  • post your cardview and layout file
    – M D
    Dec 18, 2018 at 10:00
  • @Oliver Weidner, show your ImageView property on xml or add code of ImageView in this question. Dec 18, 2018 at 10:01
  • Sorry i had problems to insert it. Now its there. Dec 18, 2018 at 10:01
  • 2
    Remove this tag android:cropToPadding="true" and use android:scaleType="fitXY" on ImageView. Dec 18, 2018 at 10:03

4 Answers 4

3

Just add android:layout_gravity="center" in your ImageView it will work

Try this

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardBackgroundColor="@android:color/white"
    app:cardElevation="2dp"
    app:cardPreventCornerOverlap="true">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/illness_image"
            android:layout_width="350dp"
            android:layout_height="364dp"
            android:layout_gravity="center"
            android:scaleType="centerInside"
            android:src="@drawable/dishu" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/illness_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textAppearance="?attr/textAppearanceHeadline6"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/illness_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Description"
                android:textAppearance="?attr/textAppearanceBody2"
                android:textSize="10sp" />

            <TextView
                android:id="@+id/illness_engagement"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="Engagment"
                android:textAppearance="?attr/textAppearanceBody2"
                android:textSize="10sp" />
        </LinearLayout>
    </LinearLayout>
</android.support.design.card.MaterialCardView>
13
  • It's not working, its shows me the same circumcised picture, but thank you. Dec 18, 2018 at 10:07
  • @OliverWeidner change the width of LinearLayout your to android:layout_width="match_parent"
    – AskNilesh
    Dec 18, 2018 at 10:10
  • It looks better, but the whole picture isn't there yet. www1.xup.in/exec/ximg.php?fid=16331743 Dec 18, 2018 at 10:16
  • 2
    There we go now its there! Thank you pretty much! Dec 18, 2018 at 10:40
  • 1
    Thank you wish you the same! :) Dec 18, 2018 at 10:42
0

Put this LOC

        android:scaleType="centerInside"

to your ImageView

<ImageView
            android:id="@+id/illness_image"
            android:layout_width="364dp"
            android:scaleType="centerInside"
            android:layout_height="364dp"
            android:background="?attr/colorPrimaryDark"
            android:cropToPadding="true" />
0
0

You can use this like the following:-

<ImageView
       android:id="@+id/illness_image"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:scaleType="fitXY"
       android:background="?attr/colorPrimaryDark"
/>
0

Try this

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.design.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true"
android:layout_margin="10dp">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/illness_image"
        android:layout_width="match_parent"
        android:layout_height="364dp"
        android:background="?attr/colorPrimaryDark"
        android:scaleType="fitXY"
       />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/illness_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textAppearance="?attr/textAppearanceHeadline6"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/illness_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Description"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/illness_engagement"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="Engagment"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textSize="10sp" />
    </LinearLayout>
</LinearLayout>

1
  • remove android:cropToPadding and then try
    – Ajay Gohel
    Dec 18, 2018 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.