Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center an ImageView in a LinearLayout?

Tags:

android

The following XML code doesn't work to center an ImageView in a LinearLayout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"     android:layout_height="wrap_content">     <ImageView         android:id="@+id/myimg"          android:layout_width="36dip"         android:layout_height="36dip"         android:scaleType="fitXY"         android:layout_gravity="center_horizontal"         android:background="@drawable/my_background"     /> </LinearLayout> 

And this is RelativeLayout code which surprisingly works:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:layout_width="fill_parent"     android:layout_height="wrap_content">     <ImageView         android:id="@+id/myimg"          android:layout_width="36dip"         android:layout_height="36dip"         android:scaleType="fitXY"         android:layout_centerHorizontal="true"         android:background="@drawable/my_background"     /> </RelativeLayout> 

Can someone tell me why? Thanks!

like image 452
user256239 Avatar asked Jul 20 '10 19:07

user256239


People also ask

How do you center something in LinearLayout?

Android LinearLayout – Center Align To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

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.

How can I center my image in android?

CENTER_CROP. Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax: android:scaleType="centerCrop" .

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 .


2 Answers

You have to set vertical orientation on your LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView     android:id="@+id/myimg"      android:layout_width="36dip"     android:layout_height="36dip"     android:scaleType="fitXY"     android:layout_gravity="center_horizontal"     android:background="@drawable/my_background" /> 

like image 165
Pajeh Avatar answered Sep 19 '22 19:09

Pajeh


Sorry, but above code didn't work for me, so i posting my Code which worked for me is

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@color/colorPrimary"     android:gravity="center_horizontal|center_vertical"     android:orientation="vertical">     <ImageView         android:id="@+id/ivPic"         android:layout_width="70dp"         android:layout_height="70dp" />  </LinearLayout> 

I hope it helps for others who need.

like image 34
ULHAS PATIL Avatar answered Sep 19 '22 19:09

ULHAS PATIL