I'm trying to center an ImageView
inside a LinearLayout
horizontally and vertically, but I just can't do it.
The main reason why I'm not using a RelativeLayout
for that is because I need the layout_weight
(my Activity
consists of four columns that should be equally divided, and also responsive to different screen widths, each column having an ImageView
centered and unstretched).
Here is my xml so far:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:baselineAligned="false"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_speak" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_readtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_readtext" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_edit" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_config"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_config" />
</LinearLayout>
</LinearLayout>
This question is related to
android
android-layout
android-linearlayout
I tried solutions mentioned here but It didn't help me. I mind the solution is layout_width
have to use wrap_content
as value.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" >
Here's some sample code. This worked for me.
<LinearLayout
android:gravity="center"
>
<TextView
android:layout_gravity="center"
/>
<Button
android:layout_gravity="center"
/>
</LinearLayout>
So you're designing the Linear Layout to place all its contents (TextView and Button) in its center, and then the TextView and Button are placed relative to the center of the Linear Layout.
If you are looking for an answer in java code,
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setGravity(Gravity.CENTER);
// add children
android:gravity
can be used on a Layout to align its children.
android:layout_gravity
can be used on any view to align itself in its parent.
NOTE: If self or children is not centering as expected, check if width/height is
match_parent
and change to something else
android:layout_gravity
is used for the layout itself
Use android:gravity="center"
for children of your LinearLayout
So your code should be:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" >
Source: Stackoverflow.com