Search code examples
androidviewandroid-linearlayouttextviewlayout-gravity

how to center a textview in linearlayout


I have the following view in xml. I have a textview at the top which is a banner. I then have a listview which holds messages and another textview which holds the String 'There are no messages'.

At runtime i check to see if there are any messages in the DB. If there are none then i hide the listview and show the textviewnomessages.

I'd like the textviewnomessages in the center of the view, not under the banner. How can i specify the textviewnomessages to be in the center of the screen. thanks

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/carefreebgscaled"

    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
        android:id="@+id/textviewmessageslabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@color/blue_alpha_background"
        android:text="Messages"
         android:textColor="#FFFFFF"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <ListView
        android:id="@+id/listviewmessages"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ccffffff"
        android:cacheColorHint="#0000"
        android:padding="5dp" >

    </ListView>

    <TextView
    android:id="@+id/textviewnomessageslabel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="You have no messages."
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#0000FF" />

</LinearLayout>

.[Edit 1]

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carefreebgscaled" >

<TextView
        android:id="@+id/textviewmessageslabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@color/blue_alpha_background"
        android:text="Messages"
         android:textColor="#FFFFFF"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <ListView
        android:id="@+id/listviewmessages"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ccffffff"
        android:cacheColorHint="#0000"
        android:padding="5dp" >

    </ListView>

    <TextView
    android:id="@+id/textviewnomessageslabel"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="You have no messages."
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#0000FF"
    android:layout_centerInParent="true" />

</RelativeLayout>

Solution

  • Okay, based on what you changed in your edit, if you set the text view in question to "wrap_content" for both layout_width and layout_height, it should work as intended.

    <TextView
        android:id="@+id/textviewnomessageslabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You have no messages."
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#0000FF"
        android:layout_centerInParent="true" />
    

    You should also set your listview to be below your top text view, which becomes the following:

    <ListView
        android:id="@+id/listviewmessages"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/textviewmessageslabel"
        android:background="#ccffffff"
        android:cacheColorHint="#0000"
        android:padding="5dp" >
    
    </ListView>