Daniel Bejan
Daniel Bejan

Reputation: 1468

Center align image in ImageView - Android

I have a problem with centering an image inside an ImageView. These are my RecyclerView rows enter image description here

And this is my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerViewRowParent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?selectableItemBackground"
    android:minHeight="64dip">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="?listPreferredItemHeight"
        android:orientation="horizontal"
        android:weightSum="100">


        <ImageView
            android:id="@+id/ivDeliveryState"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical|left"
            android:layout_weight="15"
            android:scaleType="center"
            android:src="@drawable/ic_home_blue_grey_500_24dp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="60"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:scrollHorizontally="false"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/tvSubtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:scrollHorizontally="false"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="35"
            android:orientation="horizontal"

            android:weightSum="100">

            <TextView
                android:id="@+id/tvNotification"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="25"
                android:gravity="center"
                android:lines="1"
                android:maxLength="3"
                android:textColor="@color/blue_gray_500"
                android:textSize="14sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/handle"
                android:layout_width="0dip"
                android:layout_weight="75"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:scaleType="centerInside"
                android:src="@drawable/ic_dehaze_blue_grey_500_24dp" />

        </LinearLayout>
    </LinearLayout>

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

I want to center the hamburger icon (with id="@+id/handle") on the right horizontally and vertically inside the ImageView but I had no luck until now. What property am I misusing or what am I doing wrong? Thank you!

Upvotes: 0

Views: 2930

Answers (2)

Hayuki
Hayuki

Reputation: 611

I assume you wanted to center the image on the far right of the layout. If that's the case, I think you just miscalculated the percentages. The LinearLayout that contains @+id/tvNotification and @+id/handle should be of layout_weight="25" and not 35 like it is in your code:

<LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="25"
            android:orientation="horizontal"

            android:weightSum="100">

            <TextView
                android:id="@+id/tvNotification"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="25"
                android:gravity="center"
                android:lines="1"
                android:maxLength="3"
                android:textColor="@color/blue_gray_500"
                android:textSize="14sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/handle"
                android:layout_width="0dip"
                android:layout_weight="75"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:scaleType="centerInside"
                android:src="@drawable/ic_dehaze_blue_grey_500_24dp" />

        </LinearLayout>

Upvotes: 1

Marcio Granzotto
Marcio Granzotto

Reputation: 574

set the parent LinearLayout gravity to center

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="35"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="100">

Upvotes: 1

Related Questions