I\'ve got a linear layout and an image...
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.
Simply add this to your ImageView.
android:layout_gravity="center"
android:scaleType ="centerInside" --> this line use to center an image,, but you need to keep height and width of an image as wrap_context
<ImageView
android:id ="@+id/imageView6"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:scaleType ="centerInside"
android:layout_marginTop ="20dp"
app:srcCompat ="@drawable/myimage" />
change layout weight according you will get....
Enter this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.03">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:layout_gravity="center"
android:src="@drawable/logo" />
</LinearLayout>
try this.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:scaleType="centerInside"
android:src="@drawable/logo" />
</LinearLayout>
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>