Search code examples
androidlayouttextviewcentering

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>

Solution

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