Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" /> 
like image 415
Yang Avatar asked Apr 06 '10 18:04

Yang


People also ask

How do I center an image on android?

In LinearLayout , use: android:layout_gravity="center" . In RelativeLayout , use: android:layout_centerInParent="true" .

How do I center an image in RelativeLayout Android?

RelativeLayout attributes You can change layout_align parent like top, left, and right. android:layout_centerHorizontal : This attribute is used to center the element horizontally within its parent container. You can change layout_center like layout_centerVertical or layout_centerInParent .

How do you center an image in LinearLayout?

Try layout_gravity and gravity attributes in the LinearLayout to make all the childs to be centered by default. This above is a splash screen activity. So there are two childs - ImageView and TextView. Both are center aligned.


2 Answers

<ImageView     android:id="@+id/img"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center" /> 
like image 148
Neil Chan Avatar answered Sep 24 '22 18:09

Neil Chan


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.

like image 35
Janusz Avatar answered Sep 23 '22 18:09

Janusz