android: how to align image in the horizontal center of an imageview?
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
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.
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.
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"
/>