Question

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>
Was it helpful?

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>

OTHER TIPS

You can set the visibility of TextView using setvisibility() at the runtime.

use

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"

You can do it by changing the height to "fill_parent" :

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

Or by changing his parent to a RelativeLayout and use :

android:layout_centerInParent="true

[EDIT1]

with the RelativeLayout :

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

    <TextView
            android:id="@+id/textviewmessageslabel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            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:layout_below="@id/textviewmessageslabel"
            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"
            android:gravity="center"
            android:layout_centerInParent="true" />

</RelativeLayout>

add " android:gravity="center" " to your TextView

Try putting your TextView into RelativeLayout as immediate parent:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <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"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="211dp"
            android:gravity="center"/>
    </RelativeLayout>

</LinearLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top