Question

I have set up a FrameLayout which has a TextView on top of a ListView. Now, in the MainActivity, after executing some code, I check whether the ListView is empty. If it is, I display the TextView, if not, I remove the TextView.

The code is as follows:

Following is the main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvAddItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Add some subjects"
        android:textSize="20sp" />

    <ListView
        android:id="@+id/ItemsList"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

</FrameLayout>

In the MainActivity class, I do the following:

MainActivity.java

TextView tvNoItem = (TextView) findViewById(R.id.tvAddItem);
ListView subListView = (ListView) findViewById(R.id.itemsList);
FrameLayout fl = (FrameLayout) findViewById(R.id.flMain);

ArrayList<Item> itemsList;

//some code

if(itemsList.isEmpty()) {
    fl.addView(tvNoItem);
} else {
    fl.removeView(tvNoItem);
}

Now, when I run it and I add some items to the list, the TextView (tvNoItem) is removed indeed. But all I see is a blank list - the list items are not visible. [BTW, the list is working fine. When I remove the TextView from main.xml, I can see all the list items.] Please help.

Was it helpful?

Solution 2

Why don't you use ListView's emptyView option.

Here is a nice post about showing empty view while your adapter is empty. It'll be automatically shown when your Adapter's source is empty.

Android – ListView => setEmptyView()

You don't need to check whether you data source (ArrayList) is empty, android framework will handle all the hiding and showing implementation.

Make sure to call setEmptyView() before you call setAdapter() on the ListView.

OTHER TIPS

    txtview.setVisibility(View.GONE) ;
    txtview.setVisibility(View.VISIBLE) ;

Instead of add and remove the TextView try to hide and show it. Use for it the View.VISIBLE and VIEW.GONE properties:

if(itemsList.isEmpty()) {
    tvNoItem.setVisibility(View.VISIBLE);
} else {
    tvNoItem.setVisibility(View.GONE);
}

Hope that helps.

Instead Add/Remove try to Change the Vissibility status.

Actually there is a tips & tricks to show a View in when the listView is empty

<FrameLayout
android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

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

<ViewStub
android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout="@layout/empty" />
</FrameLayout>

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime.The layout @layout/empty will be inflated only when the ViewStub visibility will change to View.VISIBLE. Once the @layout/empty view hierarchy is inflated, it replaces the ViewStub in the global layout.

ViewStub documentation

Source

1) First, there is some problem with your code. Your tvNoItem is already defined in xml layout, so you shouldn't add it again. You only add views that are dynamically created, eg

textView tv = new TextView(this);
... //customize your text view
fl.addView(tv);

You really should just set your textview xml with android:visibility="gone" and make it visible when you want.

2) Use LinearLayout instead of FrameLayout if you want the textview on top of your listview. In your listview, change android:layout_height="0dip" to android:layout_height="wrap_content", and remove the layout_weight.

rowtxt.setText("Hello" +  ++i);//for adding textview
rltv.addView(rowtxt);

rowtxt.setVisibility(View.GONE);//use for disable textview
            rltv.removeViewAt(--i);

Before getting views dynamically in layouts reset first that

LinearLayout lc2 = (LinearLayout) findViewById(R.id.leftcode1);
LinearLayout lc3 = (LinearLayout) findViewById(R.id.lcvalue1);

lc2.removeAllViews();
lc3.removeAllViews();

Above lines are worked for me to prevent duplicates dynamically adding textviews in that linear layouts

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top