4

I cannot get the image in the center of desktop. The image is alway on the left side. It is LinearLayout I some ScrollView.

        <android.support.v7.widget.CardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardCornerRadius="80dp"
            app:cardElevation="10dp"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/titelbild280x280"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"/>

        </android.support.v7.widget.CardView>
1
  • Can you post your complete XML file?
    – sanoJ
    Jul 9, 2019 at 17:20

6 Answers 6

8

CardView is an extension of FrameLayout. So you should use the same layout values as you would in a FrameLayout.
To center a child view you use:

android:layout_gravity="center"

So your layout becomes:

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="80dp"
        app:cardElevation="10dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/titelbild280x280"
            />

    </android.support.v7.widget.CardView>

However, note that the CardView in this example uses wrap_content, so it will firmly wrap the image and there will be nothing left to center. For the gravity to have effect the CardView has to be larger than the ImageView.

1
  • I've made even the image very small and changed to match content in CardView but anyway it doesn't work <android.support.v7.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" ..... <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/titelbild_100x100" />
    – leccepen
    Jul 9, 2019 at 15:12
2

You have to use a layout (ConstraintLayout or RelativeLayout or LinearLayout) for managing multiple view in card view Currently android UI system deeply coupled with ConstraintLayout for handling complex layout and making it easily, so following codes maybe help to you

<android.support.v7.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="80dp"
    app:cardElevation="10dp"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/titelbild280x280"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

Thanks.

0

Try this one Hope it will work for you

 <android.support.v7.widget.CardView
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="80dp"
        app:cardElevation="10dp"
        android:gravity="center"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/featureimage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:src="@drawable/image"/>


    </RelativeLayout>


</android.support.v7.widget.CardView>
0

Simply try this:

<?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="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/some_img" />

</androidx.cardview.widget.CardView>
0

Please try following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp"
        android:orientation="vertical">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/titelbild280x280"
                android:layout_gravity="center"/>
    </android.support.v7.widget.CardView>

</LinearLayout>

I hope its works for you.

1
  • welcome , if its working for you please give me some points and mark as a right answer Jul 11, 2019 at 6:27
0

The easiest way that worked for me was using relative layout inside my cardView and then using the android:layout_centerInParent="true". This works perfectly when the width and height of the of the relative layout are match parent (Parent being the cardView).

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.