I am using a linear layout to display a pretty light initial screen. It has 1 button that is supposed to centre in the screen both horizontally and vertically. However no matter what I try to do the button will top align centre. I have included the XML below, can some one point me in the right direction?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="@drawable/findme"></ImageButton>
</LinearLayout>
layout_width
to "wrap_content". Subsequently the layout_gravity
did what it was supposed to do !
Center using a LinearLayout:
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/findme" />
</LinearLayout>
If you want to center an item in the middle of the screen don't use a LinearLayout
as these are meant for displaying a number of items in a row.
Use a RelativeLayout
instead.
So replace:
android:layout_gravity="center_vertical|center_horizontal"
for the relevant RelativeLayout
option:
android:layout_centerInParent="true"
So your layout file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/findme"></ImageButton>
</RelativeLayout>
Have you tried defining android:gravity="center_vertical|center_horizontal"
inside the layout and setting android:layout_weight="1"
in the image?
android:weight="1"
in the button to center it though.)
A commonly-used method that works with linear layout is to set a property on the image button
android:layout_gravity="center"
You can choose whether to left-align, center-align, or right-align each object in the linear layout. Note that the above line is precisely the same as
android:layout_gravity="center_vertical|center_horizontal"
Add this
android:gravity="center"
In LinearLayout.
android:gravity="center"
If you use LinearLayout
you can add gravity center:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
</LinearLayout>`
easy with this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="visible"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/pbEndTrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Gettings" />
</LinearLayout>
You can use the RelativeLayout
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity = "center"
android:background="@drawable/findme">
</ImageButton>
</LinearLayout>
The above code will work.
As per the Android documentation for XML Attributes of android:layout_gravity, we can do it easily :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/findme"></ImageButton>
</LinearLayout>
complete and working sample from my machine...
You can do so by using the gravity="center" attribute in the parent class which makes its child position in the center.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center">
just use ( to make it in the center of your layout)
android:layout_gravity="center"
and use
android:layout_marginBottom="80dp"
android:layout_marginTop="80dp"
to change postion
Set to true android:layout_alignParentTop="true"
and android:layout_centerHorizontal="true"
in the Button, like this:
<RelativeLayout 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"
>
<Button
android:id="@+id/switch_flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/turn_on_flashlight"
android:textColor="@android:color/black"
android:onClick="action_trn"
android:background="@android:color/holo_green_light"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="5dp" />
</RelativeLayout>
https://i.stack.imgur.com/44D7i.jpg
You also can Try this Code:
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2.0"
android:background="@drawable/anyBackground" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:id="@+id/img_mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/yourImage" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:id="@+id/img_save"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/yourImage" />
</LinearLayout>
</LinearLayout>
You can also set the width of the LinearLayout to wrap_content
and use android:layout_centerInParent
, android:layout_centerVertical="true"
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/findme"/>
This is working for me.
android:layout_alignParentEnd="true"
In this case, you also can use a RelativeLayout As a parent of that ImageButton
android:layout_centerInParent="true"
then use the above code in the ImageButton.
Did you try this?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:background="#303030"
>
<RelativeLayout
android:id="@+id/widget42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
<ImageButton
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="map"
android:src="@drawable/outbox_pressed"
android:background="@null"
android:layout_toRightOf="@+id/location"
/>
<ImageButton
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="location"
android:src="@drawable/inbox_pressed"
android:background="@null"
/>
</RelativeLayout>
<ImageButton
android:id="@+id/home"
android:src="@drawable/button_back"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:orientation="horizontal"
android:background="#252525"
>
<EditText
android:id="@+id/searchfield"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/customshape"
/>
<ImageButton
android:id="@+id/search_button"
android:src="@drawable/search_press"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:apiKey="apikey" />
</TableLayout>
use
android:layout_centerHorizontal="true"
It would be easier to use relative layouts, but for linear layouts I usually center by making sure the width matches parent :
android:layout_width="match_parent"
and then just give margins to right and left accordingly.
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
Success story sharing