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 :)