0

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>
Krushna Chulet
  • 1,625
  • 1
  • 10
  • 9
turtleboy
  • 8,210
  • 27
  • 100
  • 199

6 Answers6

0

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>
Andros
  • 4,069
  • 1
  • 22
  • 30
  • the second textview is now on top of the banner. Edit 1 is what i have now. how can i set the second textview to the center of the screen – turtleboy Oct 24 '13 at 13:38
0

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

kshirish
  • 1,489
  • 3
  • 11
  • 12
0

use

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
suresh
  • 123
  • 2
  • 6
0

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>
Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
0

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

android enthusiast
  • 2,062
  • 2
  • 24
  • 45
0

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>
Danger
  • 433
  • 5
  • 17