John R
John R

Reputation: 2064

Text not align in center

I want to set text in button in center but its not working. And is there any other way to write code. I want top half screen empty and below half having 4 buttons.

Code-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="4" >

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="4" >

    <Button
            android:id="@+id/intro"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Introduction" />

    <Button
            android:id="@+id/typ"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@null"
            android:layout_weight="1"
            android:text="Types" />

    <Button
            android:id="@+id/app"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@null"
            android:text="Application" />

    <Button
            android:id="@+id/ben"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_vertical|center_horizontal"
            android:background="@null"
            android:layout_weight="1"
            android:text="Benefits" />

    </LinearLayout>

</LinearLayout>

Image-

enter image description here

Upvotes: 0

Views: 131

Answers (2)

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

This is my recipe:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:orientation="vertical"
    >
    <View
        android:id="@+id/top_space_waster"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="4"
    />
    <Button
        android:id="@+id/intro"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Introduction"
    />
    <Button
        android:id="@+id/typ"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Types"
    />
    <Button
        android:id="@+id/app"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="1"
        android:background="@null"
        android:text="Application"
    />
    <Button
        android:id="@+id/ben"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Benefits"
    />
</LinearLayout>

Note the layout_height="0dp", needed for weights to work.
Note also that there's a space wasting View that occupies the upper half of the screen.

Upvotes: 1

vipul mittal
vipul mittal

Reputation: 17401

As the width of these buttons is set to match_parent setting layout_gravity wont have any effect. You need to set gravity like this:

<Button
        android:id="@+id/intro"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Introduction" />

<Button
        android:id="@+id/typ"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Types" />

<Button
        android:id="@+id/app"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:background="@null"
        android:text="Application" />

<Button
        android:id="@+id/ben"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Benefits" />

And also try to run your app and test sometimes IDE shows text this way but it's actually well centered.

Upvotes: 1

Related Questions