Add and remove a TextView dynamically in Android
Asked Answered
B

8

5

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.

Boogie answered 9/7, 2013 at 6:23 Comment(2)
Why not set the view to gone? textView.setVisibility(View.GONE)Shockheaded
I tried that. It does the same thing. I get a blank activity.Boogie
S
3

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.

Sherard answered 9/7, 2013 at 6:25 Comment(0)
A
16
    txtview.setVisibility(View.GONE) ;
    txtview.setVisibility(View.VISIBLE) ;
Ablate answered 9/7, 2013 at 6:26 Comment(0)
S
3

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.

Sherard answered 9/7, 2013 at 6:25 Comment(0)
L
1

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.

Larue answered 9/7, 2013 at 6:26 Comment(1)
does it remove particular element from child of layout by setting its visibility to gone ?Sherysherye
I
1

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

Ibbetson answered 30/3, 2017 at 10:55 Comment(0)
E
0

Instead Add/Remove try to Change the Vissibility status.

Eggbeater answered 9/7, 2013 at 6:26 Comment(0)
K
0

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

Knickerbocker answered 9/7, 2013 at 6:31 Comment(0)
S
0

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.

Shockheaded answered 9/7, 2013 at 6:32 Comment(0)
B
0
rowtxt.setText("Hello" +  ++i);//for adding textview
rltv.addView(rowtxt);

rowtxt.setVisibility(View.GONE);//use for disable textview
            rltv.removeViewAt(--i);
Bobette answered 3/9, 2016 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.