android: how to align image in the horizontal center of an imageview?

AndroidAlignment

Android Problem Overview


I've tried all scaletypes, but all of them result in the image to be at the left corner of the imageview.

 <ImageView
    android:id="@+id/image"
	android:scaleType="centerInside"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    
    android:layout_marginRight="6dip"
	android:background="#0000" 
    android:src="@drawable/icon1" />

Android Solutions


Solution 1 - Android

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

Solution 2 - Android

Your ImageView has the attribute wrap_content. I would think that the Image is centered inside the imageview but the imageview itself is not centered in the parentview. If you have only the imageview on the screen try match_parent instead of wrap_content. If you have more then one view in the layout you have to center the imageview.

Solution 3 - Android

You can center the ImageView itself by using:

android:layout_centerVertical="true" or android:layout_centerHorizontal="true" For vertical or horizontal. Or to center inside the parent:

android:layout_centerInParent="true"

But it sounds like you are trying to center the actual source image inside the imageview itself, which I am not sure on.

Solution 4 - Android

This works for me when aligning image view in linearlayout.

<ImageView android:id="@android:id/empty"
    android:src="@drawable/find_icon"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:scaleType="center"
    />

Solution 5 - Android

Try this code :

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal">

            <ImageView
                android:id="@+id/imgBusiness"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/back_detail" />
</LinearLayout>

Solution 6 - Android

Using "fill_parent" alone for the layout_width will do the trick:

 <ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dip"
    android:background="#0000" 
    android:src="@drawable/icon1" />

Solution 7 - Android

Try:

android:layout_height="wrap_content"
android:scaleType="fitStart"

on the image in the RelativeLayout

Solution 8 - Android

For me android:gravity="center" did the trick in the parent layout element.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/fullImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/fullImageView"
        android:layout_gravity="center" />

</LinearLayout>

Solution 9 - Android

you can horizontal your image view in a linear layout using:

 android:layout_gravity="center"

it will center your image to the parent element, if you just want to center horizontally you can use:

 android:layout_gravity="center_horizontal"
  

Solution 10 - Android

Give width of image as match_parent and height as required, say 300 dp.

<ImageView
    android:id = "@+id/imgXYZ"
    android:layout_width = "match_parent"
    android:layout_height = "300dp"
    android:src="@drawable/imageXYZ"
    />

Solution 11 - Android

Use this attribute: android:layout_centerHorizontal="true"

Solution 12 - Android

This is code in xml of how to center an ImageView, I used "layout_centerHorizontal".

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_centerHorizontal="true"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img2"
    />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />
</LinearLayout>

or this other example...

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img2"
    />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />
</LinearLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYangView Question on Stackoverflow
Solution 1 - AndroidNeil ChanView Answer on Stackoverflow
Solution 2 - AndroidJanuszView Answer on Stackoverflow
Solution 3 - AndroidMatt SwansonView Answer on Stackoverflow
Solution 4 - AndroidklaydzeView Answer on Stackoverflow
Solution 5 - AndroidAbbes YassineView Answer on Stackoverflow
Solution 6 - Androidcharles youngView Answer on Stackoverflow
Solution 7 - AndroidMark TicknerView Answer on Stackoverflow
Solution 8 - AndroidKovács EdeView Answer on Stackoverflow
Solution 9 - Androidhasanga lakdinuView Answer on Stackoverflow
Solution 10 - AndroidKaustubh SrivastavaView Answer on Stackoverflow
Solution 11 - AndroidTom R.View Answer on Stackoverflow
Solution 12 - AndroidDarckBlezzerView Answer on Stackoverflow