I have a view with EditText, Button and ListView.
Button’s onClick() parses some site (this part works OK), but it takes some time to display the parsed info in ListView, so I want to display small ProgressBar on the place, where the ListView should take place after a while.
So, I add this to my layout (I write important part here):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout1"
android:weightSum="1.0">
<EditText
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight=".86" />
<ImageButton
android:layout_height="match_parent"
android:layout_width="0px"
android:layout_weight=".14" />
</LinearLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@id/layout1"
android:gravity="center"
android:layout_centerInParent="true"
android:id="@+id/progressbar" >
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@android:style/Widget.ProgressBar.Small" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout1">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
In source code:
progressBar = (RelativeLayout) findViewById(R.id.progressbar);
And everytime I want to show the ProgressBar, I do this:
progressBar.setVisibility(View.VISIBLE);
for disabling:
progressBar.setVisibility(View.INVISIBLE);
But this doesn’t work.
How can I work this out?
There’s no need to manipulate your
ProgressBar‘s visibility.ListViewhas a feature called “empty view” which is shown only if yourListViewis empty. Here’s what you need to do to use it:ListActivity, useandroid:id="@android:id/list"for yourListViewandandroid:id="@android:id/empty"for yourProgressBar.ListActivity, there’s asetEmptyView()method of your
ListView, which lets you do the same thing.The above method fits best if you don’t need to empty your
ListView(e.g. data is loaded only once, at the activity startup). If you need to set data after that, it is better to useViewSwitcher, which also has great advantage – it lets you apply transition animation. For details onViewSwitcher, take a look at this.