Shah
Shah

Reputation: 5020

Show/Hide the Custom ListView in android

Can anybody guide me how can i Show/Hide the Custom ListView (BaseAdapter based) from my Activity.

For some more elaboration I have an activty which has a BUTTON and a LISTVIEW, I have an Inner Class MyAdapter extends with Base Adapter.

Now pressing a BUTTON on my ACTIVITY SHOW/HIDE the ListView.

CAn anybody guide.

Thanks

Upvotes: 0

Views: 4331

Answers (2)

Emil Adz
Emil Adz

Reputation: 41129

Just change the visibility of your ListView on the press of the button. using this line:

listView.setVisibility(View.VISIBLE);   

To show. and:

listView.setVisibility(View.GONE);

to hide.

you can also use: listView.setVisibility(View.INVISIBLE); to hide the listView but still take it's screen place.

Upvotes: 3

SKK
SKK

Reputation: 5271

You could try this. Using a toggle button:

public class ShowHideListViewActivity extends Activity {

ToggleButton tb;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylistviewlayout);

    tb = (ToggleButton) findViewById(R.id.toggleButton1);
    lv = (ListView) findViewById(R.id.listView1);

    //You could set ListView Adapter here.

    tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked)
            {
                lv.setVisibility(View.VISIBLE);
            }
            else
            {
                //lv.setVisibility(View.GONE);
                lv.setVisibility(View.INVISIBLE);
            }
        }
    });
 }
}

XML file:

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

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ToggleButton" 
    android:textOn="Show"
    android:textOff="Hide"/>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="406dp" >
</ListView>

</LinearLayout>

Upvotes: 0

Related Questions