[xml] Center a button in a Linear layout

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>

This question is related to xml android layout

The answer is


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


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>

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

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

Add this

android:gravity="center"

In LinearLayout.


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>

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>

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


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


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"

You can use the RelativeLayout.


complete and working sample from my machine...

_x000D_
_x000D_
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"_x000D_
    xmlns:tools="http://schemas.android.com/tools"_x000D_
    android:layout_width="fill_parent"_x000D_
    android:layout_height="fill_parent"_x000D_
    android:paddingLeft="@dimen/activity_horizontal_margin"_x000D_
    android:paddingRight="@dimen/activity_horizontal_margin"_x000D_
    android:paddingTop="@dimen/activity_vertical_margin"_x000D_
    android:paddingBottom="@dimen/activity_vertical_margin"_x000D_
    android:orientation="vertical"_x000D_
    tools:context=".MainActivity"_x000D_
    android:gravity="center"_x000D_
    android:textAlignment="center">_x000D_
_x000D_
_x000D_
    <TextView_x000D_
        android:layout_width="fill_parent"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:textAppearance="?android:attr/textAppearanceLarge"_x000D_
        android:text="My Apps!"_x000D_
        android:id="@+id/textView"_x000D_
        android:gravity="center"_x000D_
        android:layout_marginBottom="20dp"_x000D_
     />_x000D_
_x000D_
    <Button_x000D_
        android:layout_width="220dp"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:text="SPOTIFY STREAMER"_x000D_
        android:id="@+id/button_spotify"_x000D_
        android:gravity="center"_x000D_
        android:layout_below="@+id/textView"_x000D_
        android:padding="20dp"_x000D_
        />_x000D_
_x000D_
    <Button_x000D_
        android:layout_width="220dp"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:text="SCORES"_x000D_
        android:id="@+id/button_scores"_x000D_
        android:gravity="center"_x000D_
        android:layout_below="@+id/textView"_x000D_
        android:padding="20dp"_x000D_
        />_x000D_
_x000D_
_x000D_
    <Button_x000D_
        android:layout_width="220dp"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:layout_centerInParent="true"_x000D_
        android:text="LIBRARY APP"_x000D_
        android:id="@+id/button_library"_x000D_
        android:gravity="center"_x000D_
        android:layout_below="@+id/textView"_x000D_
        android:padding="20dp"_x000D_
        />_x000D_
_x000D_
    <Button_x000D_
        android:layout_width="220dp"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:layout_centerInParent="true"_x000D_
        android:text="BUILD IT BIGGER"_x000D_
        android:id="@+id/button_buildit"_x000D_
        android:gravity="center"_x000D_
        android:layout_below="@+id/textView"_x000D_
        android:padding="20dp"_x000D_
        />_x000D_
_x000D_
    <Button_x000D_
        android:layout_width="220dp"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:layout_centerInParent="true"_x000D_
        android:text="BACON READER"_x000D_
        android:id="@+id/button_bacon"_x000D_
        android:gravity="center"_x000D_
        android:layout_below="@+id/textView"_x000D_
        android:padding="20dp"_x000D_
        />_x000D_
_x000D_
    <Button_x000D_
        android:layout_width="220dp"_x000D_
        android:layout_height="wrap_content"_x000D_
        android:layout_centerInParent="true"_x000D_
        android:text="CAPSTONE: MY OWN APP"_x000D_
        android:id="@+id/button_capstone"_x000D_
        android:gravity="center"_x000D_
        android:layout_below="@+id/textView"_x000D_
        android:padding="20dp"_x000D_
        />_x000D_
_x000D_
</LinearLayout>
_x000D_
_x000D_
_x000D_


use

android:layout_centerHorizontal="true"


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"

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"

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


Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to layout

This view is not constrained What's the difference between align-content and align-items? CSS Flex Box Layout: full-width row and columns Fill remaining vertical space with CSS using display:flex What is setContentView(R.layout.main)? How to change line color in EditText Scrolling a flexbox with overflowing content UICollectionView - Horizontal scroll, horizontal layout? How to style a div to be a responsive square? 100% width Twitter Bootstrap 3 template