Skizit Skizit - 2 months ago 19
Android Question

Android: Center an image

I've got a linear layout and an image...

<?xml version="1.0" encoding="utf-8"?>

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

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

</LinearLayout>


How do I dynamically center my image so that it will appear in the center of the screen on all devices?

Answer

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

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