0

I'm trying to display a TextView on top of a ListView from within a ListActivity, but the text is not showing up.

LayoutInflater inflater = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View layout = inflater.inflate( R.layout.main, null );
TextView myTextView = (TextView)layout.findViewById( R.id.myTextView );
myTextView.setVisibility( View.VISIBLE );
myTextView.setText( R.string.emptyList );
setContentView( layout );

Here is the main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/myTextView"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:textSize="16sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>    
    <ListView  
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
</LinearLayout>

I want an "Empty list" notice to go at the top of the screen in case the list view is empty.

Phillip
  • 5,366
  • 10
  • 43
  • 62

3 Answers3

3

Why dont you use setEmptyView for your ListView and do your notification that way.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:layout_width="fill_parent"
    android:layout_height="300dip"
    android:id="@+id/list_view" />
<TextView
    android:id="@+id/empty_list_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="List view is empty"
    android:visibility="gone" />
</LinearLayout>

And call in your code.

ListView listView = (ListView) findViewById( R.id.list_view );
listView.setEmptyView( findViewById( R.id.empty_list_view ) );
listView.setAdapter(yourAdapter);

Check these for more info.

Correct use of setEmtpyView in AdapterView

http://www.technotalkative.com/android-listview-setemptyview/

Community
  • 1
  • 1
Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
1

You could use a FrameLayout instead of a LinearLayout, that way both the TextView and the ListView will share the same layout space (although not strictly necessary).

Then you can set the ListView empty view to myTextView, and it will take care of showing or hiding the empty view automatically for you.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
0

I'm not sure what you mean regarding the TextView. Are you trying to place something above the list when the activity loads? In that case adding it above the list in the LinearLayout will do just fine.

To get the "empty list" notice you are referring to, make sure activity you are using extends ListActivity, not just Activity, and that the respective views are titled "list" and "empty."

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

<TextView
    android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/empty_user_list"/>

</LinearLayout>
Rarw
  • 7,645
  • 3
  • 28
  • 46