Oliver Weidner
Oliver Weidner

Reputation: 305

ImageView isn't center inside CardView

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.

Upvotes: 2

Views: 1342

Answers (4)

Ajay Gohel
Ajay Gohel

Reputation: 243

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>

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69699

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>

Upvotes: 3

ॐ Rakesh Kumar
ॐ Rakesh Kumar

Reputation: 1314

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

Upvotes: 0

GianhTran
GianhTran

Reputation: 3711

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

Upvotes: 0

Related Questions