Follow this Blog!

Dec 10, 2012

f Comment

Android: How to Make ListView Disappear or Invisible Dynamically?

Amazon I use Eclipse IDE to develop my Android application. I am using a ListView in my app but I'd like to it be able to disappear and reappear under my command.

This is useful in my app because I'd like to use ListView to show multiple possible locations for a given query (e.g. Starbucks) entered by user ONLY when use enters a query. When user is playing with the Google Map embedded in my app the ListView should be invisible.

I am using Windows 7, Eclipse Indigo, Android 4.1 (API 16).
Solution
The solution is easy. In your corresponding activity class first you get the reference to the ListView like the following:
ListView mPossibleLocationListView = (ListView) findViewById(R.id.possibleLocationView);
Then you call setVisibility() to make the ListView disappear.
mPossibleLocationListView.setVisibility(View.GONE);
The ListView will be entirely invisible, not taking any space in the layout at all.

When you'd like to make the ListView visible again, simply define the items of the list through an Adapter and call the following to make the ListView appear with the defined adapter:
...
mPossibleLocationListView.setAdapter(adapter); 
mPossibleLocationListView.setVisibility(View.VISIBLE);
...
There are plenty of examples on the Internet about how to define items for a ListView.

If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael