Android: Center an image

Solution 1:

In LinearLayout, use: android:layout_gravity="center".

In RelativeLayout, use: android:layout_centerInParent="true".

Solution 2:

If you are using a LinearLayout , use the gravity attribute :

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

    <ImageView android:layout_width="wrap_content"
        android:id="@+id/imageView1"
        android:src="@drawable/icon"
        android:layout_height="wrap_content"
        android:scaleType="centerInside" />
</LinearLayout>

If you are using a RelativeLayout , you can use android:layout_centerInParent as follows :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"
        android:scaleType="centerInside"
        android:layout_centerInParent="true" />

</RelativeLayout>

Solution 3:

Technically both answers above are correct, but since you are setting your ImageView to fill_parent instead of wrap_content, the image within is not centered, but the ImageView itself is.

Give your ImageView the attributes:

android:scaleType="centerInside"
android:gravity="center"

The scaleType is really only necessary in this case if the image exceeds the size of the ImageView. You may also want different scaleTypes. In conclusion, android:gravity is what you're looking for in this case, but if your ImageView is set to wrap_content, you should set the ImageView's gravity (android:layout_gravity) to be centered within the parent.

Solution 4:

Simply add this to your ImageView.

android:layout_gravity="center" 

Solution 5:

Here are 2 ways you can center an image (or images) both vertically and horizontally in LinearLayout.

(1) Use the android:layout_gravity="center" attribute in ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        layout_height="wrap_content"
        android:src="@drawable/icon"
        android:layout_gravity="center"/>
</LinearLayout>

How this works: the android:layout_gravity="center" attribute in ImageView centers itself (i.e. the image) vertically and horizontally relative to its parent (LinearLayout).

-- OR --

(2) Alternatively, you can use the android:gravity="center" attribute in LinearLayout and omit the android:layout_gravity="center" attribute in ImageView :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"> 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        layout_height="wrap_content"
        android:src="@drawable/icon"
        android:layout_gravity="center"/>
</LinearLayout>

How this works: the android:gravity="center" attribute in LinearLayout centers its child/children (in this case, it's the image) vertically and horizontally.