PuchuKing33
PuchuKing33

Reputation: 381

Android textview center positioning

I am trying to place 3 textviews in the center of the screen, with a bit of space between them since they are gonna be used as hyperlinks.

the result so far is:

enter image description here

and the code is:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="@string/about" />

    <TextView
        android:id="@+id/TextTabletopRPG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Tabletop RPG"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextDarkHeresy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Dark Heresy"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/Text40k"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center"
        android:text="40k Universe"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Upvotes: 1

Views: 28297

Answers (5)

Punithapriya M
Punithapriya M

Reputation: 21

If you want to show the only one textview instead of another object try adding this:

android:id="@+id/emptyElement"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text=""

Upvotes: 2

Batuhan Coşkun
Batuhan Coşkun

Reputation: 2967

Full Edit: Try this for all centered in the view. You can change android:gravity="center" with android:gravity="center_horizontal" or android:gravity="center_vertical" on RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:text="@string/about" />

    <TextView
        android:id="@+id/TextTabletopRPG"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textView1"
        android:layout_margin="5dp"
        android:text="Tabletop RPG"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextDarkHeresy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextTabletopRPG"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:text="Dark Heresy"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/Text40k"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextDarkHeresy"
        android:layout_centerHorizontal="true"
        android:layout_margin="5dp"
        android:text="40k Universe"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Note: I added margin's for good display.

Upvotes: 3

Chris
Chris

Reputation: 4593

If you simply want the textviews to be centered horizontally as per the image example you provided you can do this:

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Test ME" />

If you want them to be in center of entire screen ie centered both vertically and horizontally without affecting the top paragraph you must wrap them in a seperate LinearLayout with property gravity set like this:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal">

Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:text="@string/about" />

    <!--wrap your centered textview with this-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="vertical"
        android:gravity="center_vertical|center_horizontal">

          <TextView
             android:id="@+id/TextTabletopRPG"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             android:text="Tabletop RPG"
             android:textAppearance="?android:attr/textAppearanceMedium" />

     </LinearLayout>

</LinearLayout>

Upvotes: 6

barweiss
barweiss

Reputation: 39

try adding:

android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"

Upvotes: 3

Alex K
Alex K

Reputation: 8338

If you want there to be more space, you can use margins. Add this to each TextView's XML:

android:layout_margin="6dp"

Upvotes: 1

Related Questions