1

How can I format the text displayed when the list is empty in an Android ListView?

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:dividerHeight="1dip" />

<TextView
    android:id="@+id/android:empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_list"
    android:textColor="@color/white"
    android:textSize="25sp"
     />

I want to set the typeface to a typeface from asset..

mTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/myFont.ttf");
mEmptyTextView = (TextView)findViewById(**????**)    
mEmptyTextView.setTypeface(mTypeFace);
0

2 Answers 2

2

you will need to use android.R.id.empty for initializing mEmptyTextView. try it as :

mTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/myFont.ttf");
mEmptyTextView = (TextView)findViewById(android.R.id.empty);
mEmptyTextView.setTypeface(mTypeFace);
Sign up to request clarification or add additional context in comments.

1 Comment

Will it work for ListFragment? In list_content.xml for API 22 for ListFragment there's @+android:id/internalEmpty is used.
0
<TextView
    android:id="@+id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test_list"
    android:textColor="@color/white"
    android:textSize="25sp"/>

Then you can find this view by id (dont use android: prefix):

import <your.package>.R;
...
mEmptyTextView = (TextView)findViewById(R.id.empty);

Also ListView has a special method to set empty view:

ListView#setEmptyView

http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)

BTW you need somehow hide empty TextView (without calling #setVisible(View.GONE) on TextView) by placing it into some sort of container and hide a container - but this is a bad way.

So either inflate TextView from different layout or create him at runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.