Zorgan
Zorgan

Reputation: 9123

How to place Button in the center of the screen in a LinearLayout?

Here's my xml using a LinearLayout. Both the Toolbar and the Button are at the top of the screen. However I want the Button centrally horizontal and centrally vertical. Also the Button is floated to the left and not in the center.

xml

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorVeryDark"
        android:elevation="4dp"

        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    <Button
        android:id="@+id/button2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="text"
        android:textSize="22dp"
        android:layout_gravity="center_vertical"
        android:background="@color/colorPrimaryDark"
        />

</LinearLayout>

How would I do this?

Upvotes: 0

Views: 6754

Answers (6)

Puneet Verma
Puneet Verma

Reputation: 1473

Add these two lines in you Button

android:gravity="center"
android:layout_gravity="center_horizontal"

refer this image for proper understanding of use of gravity

enter image description here

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

Try this: use a RelativeLayout and use android:layout_centerInParent="true" for the Button

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button2"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@color/colorPrimaryDark"
            android:text="text"
            android:textSize="22dp" />

    </RelativeLayout>

</LinearLayout>

Output:

enter image description here

Upvotes: 3

Ana
Ana

Reputation: 174

Just use this line of code in your button xml design-

android:gravity="center"

For more ideas you can check this link- Center a button in a Linear layout

Upvotes: 0

david.mar22
david.mar22

Reputation: 41

Use a RelativeLayout then you add the code:

android:gravity="center"
android:layout_gravity="center_horizontal"

in your Button XML.

In alternative, you can use the "design" tool of AndroidStudio for layouts: you can add a button and then drag wherever you want in the preview.

However, there are tons of post like this: next time try to search better :D

Upvotes: 0

Francesco Taioli
Francesco Taioli

Reputation: 2856

try to use gravity

<Button
            android:id="@+id/okbutton"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:text="text"
            android:gravity="center"
            android:layout_gravity="center_horizontal"
            android:textSize="50sp" />

Upvotes: 0

Ivan Wooll
Ivan Wooll

Reputation: 4323

Unfortunately LinearLayouts are not the best for this. FrameLayout, RelativeLayout or ConstraintLayout will give you the flexibility to do what you want.

Upvotes: 0

Related Questions