vimal kumar
vimal kumar

Reputation: 325

android LInear layout centering a button

In Linear layout am trying to center the button but i cant what is the coding for centering the button suggestion please

this is my Linear layout

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
            <Button
                android:layout_width="98"
                android:layout_height="58"
                android:layout_gravity="center"
                android:text="S T A G E"
                android:id="@+id/stage1"
                android:background="#65f077cc" />

        </LinearLayout>
    </HorizontalScrollView>

Thank you in advnce

Upvotes: 0

Views: 53

Answers (3)

Fasiha
Fasiha

Reputation: 506

If you want to center an item in the center of the screen use a Relativelayout because linear layout is use for displaying items in a row.

Like This

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

<Button
    android:id="@+id/stage1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="#65f077cc"
    android:text="S T A G E" />


</RelativeLayout>

If you want to center an item in the center of a row use the below sample.

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

<Button
    android:id="@+id/stage1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#65f077cc"
    android:text="S T A G E" />

</LinearLayout>

Although if you are strict with linear layout ONLY than you can try this.

<?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:gravity="center"
android:orientation="vertical" >

<Button
    android:id="@+id/stage1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#65f077cc"
    android:text="S T A G E" />

</LinearLayout> 

Upvotes: 0

milez
milez

Reputation: 2201

First remove things that might affect it. Orientations is best to be define at as high level as possible, like in your parent LinearLayout. From Button, remove (for now, add later if you still think you want them) android:orientation="horizontal", and android:layout_gravity="right|center_vertical".

Also fix the height of the button to be android:layout_height="wrap_content".

To LinearLayout, add

android:gravity="center"

Here is a good read on the differences of gravity and layout_gravity: http://sandipchitale.blogspot.fi/2010/05/linearlayout-gravity-and-layoutgravity.html

Additionally, fill_parent is deprecated, so you should switch to match_parent instead, where appropriate.

Edit: the final layout

    <LinearLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:layout_width="98"
            android:layout_height="58"
            android:text="S T A G E"
            android:id="@+id/stage1"
            android:background="#65f077cc" />

Upvotes: 1

Ketan
Ketan

Reputation: 443

try this. Add weight to the button. android:weight="1"

Upvotes: 0

Related Questions