0

When creating a custom ListView in XML, you can add the android:id/empty tag to the ID to make that view show when the ListView is empty; as mentioned in this Android documentation. I'm just wondering, does anyone know how to do the equivalent dynamically in code?

UPDATE This is what I have:

public class CustomList extends ListView {
     public CustomList(Context context) {
         // some initialization
         TextView text = new TextView(context);
         // some parameters like text size, color, and font are set here
         setEmptyView(text);
     }
}

The setEmptyView seems ineffective, in fact is just causes everything to be gone when the ListView goes empty.

3
  • Check my updated answer what I tried using setEmptyView and it works. Commented Sep 23, 2011 at 5:09
  • I can assure you that it's just setting up the adapter, some touch and click event listeners, and nothing more. Commented Sep 23, 2011 at 5:44
  • Not getting what you are trying to do at all. Commented Sep 23, 2011 at 5:56

1 Answer 1

3

Very simple. Use FrameLayout.

Frame layouts are one of the simplest and most efficient types of layouts used by Android developers to organize view controls. They are used less often than some other layouts, simply because they are generally used to display only one view, or views which overlap. The frame layout is often used as a container layout, as it generally only has a single child view (often another layout, used to organize more than one view).

For example :

<FrameLayout android:id="@+id/list_and_empty_message_container"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:orientation="vertical">
        <!-- List view -->
        <ListView android:id="@+id/main_list_view"
            android:background="@android:color/transparent"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:cacheColorHint="#00000000" android:layout_gravity="top"
            android:divider="#D7C3F8" android:dividerHeight="2dip" />
        <TextView android:text="No info Found." android:id="@+id/list_empty_txt"
            android:layout_height="wrap_content" android:textSize="16.1sp"
            android:textStyle="bold" android:typeface="monospace"
            android:layout_marginLeft="5dip" android:layout_gravity="center_vertical"
            android:gravity="center" android:textColor="#000000"
            android:layout_width="fill_parent" android:visibility="gone" />
    </FrameLayout>

And count your data adapter. If that is empty set textview as visible, else set visibility to gone.

Put below code in your onCreate(),

TextView emptyText =  (TextView) findViewById(R.id.list_empty_txt);;
        if(Your_data_array.size() <= 0)
            emptyText.setVisibility(View.VISIBLE);
        else
            emptyText.setVisibility(View.GONE);

It will work.

* ___________Updated__________*

Take a look in ListActivity class, there is a method that sets empty message if your data source is empty.

public void onContentChanged() {  
    super.onContentChanged();  
    View emptyView = findViewById(com.android.internal.R.id.empty);  
    mList = (ListView)findViewById(com.android.internal.R.id.list);  
    if (mList == null) {  
        throw new RuntimeException(  
                "Your content must have a ListView whose id attribute is " +  
                "'android.R.id.list'");  
    }  
       //Take a look here
    if (emptyView != null) {  
        mList.setEmptyView(emptyView);  
    }  
    mList.setOnItemClickListener(mOnClickListener);  
    if (mFinishedStart) {  
        setListAdapter(mAdapter);  
    }  
    mHandler.post(mRequestFocus);  
    mFinishedStart = true;  
} 

I will help you to solve your problem.

Happy coding :)

Sign up to request clarification or add additional context in comments.

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.