Center a button in a Linear layout

XmlAndroidLayout

Xml Problem Overview


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>

Xml Solutions


Solution 1 - Xml

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>

Solution 2 - Xml

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>

Solution 3 - Xml

Have you tried defining android:gravity="center_vertical|center_horizontal" inside the layout and setting android:layout_weight="1" in the image?

Solution 4 - Xml

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"

Solution 5 - Xml

Add this

android:gravity="center"

In LinearLayout.

Solution 6 - Xml

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>`

Solution 7 - Xml

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>

Solution 8 - Xml

You can use the RelativeLayout.

Solution 9 - Xml

<?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.

Solution 10 - Xml

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>

Solution 11 - Xml

complete and working sample from my machine...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center"
    android:textAlignment="center">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="My Apps!"
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_marginBottom="20dp"
     />

    <Button
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:text="SPOTIFY STREAMER"
        android:id="@+id/button_spotify"
        android:gravity="center"
        android:layout_below="@+id/textView"
        android:padding="20dp"
        />

    <Button
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:text="SCORES"
        android:id="@+id/button_scores"
        android:gravity="center"
        android:layout_below="@+id/textView"
        android:padding="20dp"
        />


    <Button
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="LIBRARY APP"
        android:id="@+id/button_library"
        android:gravity="center"
        android:layout_below="@+id/textView"
        android:padding="20dp"
        />

    <Button
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="BUILD IT BIGGER"
        android:id="@+id/button_buildit"
        android:gravity="center"
        android:layout_below="@+id/textView"
        android:padding="20dp"
        />

    <Button
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="BACON READER"
        android:id="@+id/button_bacon"
        android:gravity="center"
        android:layout_below="@+id/textView"
        android:padding="20dp"
        />

    <Button
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="CAPSTONE: MY OWN APP"
        android:id="@+id/button_capstone"
        android:gravity="center"
        android:layout_below="@+id/textView"
        android:padding="20dp"
        />

</LinearLayout>

Solution 12 - Xml

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">

Solution 13 - Xml

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

Solution 14 - Xml

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>

enter image description here

Solution 15 - Xml

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>

Solution 16 - Xml

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"/>

Solution 17 - Xml

This is working for me.

android:layout_alignParentEnd="true"

Solution 18 - Xml

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.

Solution 19 - Xml

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>

Solution 20 - Xml

use

> android:layout_centerHorizontal="true"

Solution 21 - Xml

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthemaninthesuitcaseView Question on Stackoverflow
Solution 1 - Xmllancha90View Answer on Stackoverflow
Solution 2 - XmlDave WebbView Answer on Stackoverflow
Solution 3 - XmlruiView Answer on Stackoverflow
Solution 4 - XmlhopcraftView Answer on Stackoverflow
Solution 5 - XmlTanmay SahooView Answer on Stackoverflow
Solution 6 - XmlHans PourView Answer on Stackoverflow
Solution 7 - XmlJavierView Answer on Stackoverflow
Solution 8 - XmlAmit kumarView Answer on Stackoverflow
Solution 9 - XmlVishView Answer on Stackoverflow
Solution 10 - XmlReza MamunView Answer on Stackoverflow
Solution 11 - XmlJoe HealyView Answer on Stackoverflow
Solution 12 - XmlAbubakarView Answer on Stackoverflow
Solution 13 - XmlMohamed AdelView Answer on Stackoverflow
Solution 14 - XmlOskarView Answer on Stackoverflow
Solution 15 - Xmluser4058115View Answer on Stackoverflow
Solution 16 - XmlA-SharabianiView Answer on Stackoverflow
Solution 17 - XmlMarciView Answer on Stackoverflow
Solution 18 - XmlAbubakarView Answer on Stackoverflow
Solution 19 - XmlVarghese JohnView Answer on Stackoverflow
Solution 20 - XmlMayank MishraView Answer on Stackoverflow
Solution 21 - Xmluser2090145View Answer on Stackoverflow