how to show progress bar(circle) in an activity having a listview before loading the listview with data
Solution 1:
There are several methods of showing a progress bar (circle) while loading an activity
. In your case, one with a ListView
in it.
IN ACTIONBAR
If you are using an ActionBar
, you can call the ProgressBar
like this (this could go in your onCreate()
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
And after you are done displaying the list, to hide it.
setProgressBarIndeterminateVisibility(false);
IN THE LAYOUT (The XML)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pbHeaderProgress"
style="@style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="@android:color/transparent"
android:divider="#00000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling"
android:smoothScrollbar="false" >
</ListView>
</LinearLayout>
And in your activity (Java)
I use an AsyncTask to fetch data for my lists. SO, in the AsyncTask's onPreExecute()
I use something like this:
// CAST THE LINEARLAYOUT HOLDING THE MAIN PROGRESS (SPINNER)
LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
@Override
protected void onPreExecute() {
// SHOW THE SPINNER WHILE LOADING FEEDS
linlaHeaderProgress.setVisibility(View.VISIBLE);
}
and in the onPostExecute()
, after setting the adapter to the ListView
:
@Override
protected void onPostExecute(Void result) {
// SET THE ADAPTER TO THE LISTVIEW
lv.setAdapter(adapter);
// CHANGE THE LOADINGMORE STATUS TO PERMIT FETCHING MORE DATA
loadingMore = false;
// HIDE THE SPINNER AFTER LOADING FEEDS
linlaHeaderProgress.setVisibility(View.GONE);
}
EDIT: This is how it looks in my app while loading one of several ListViews
Solution 2:
You can do this easier.
Source: http://www.tutorialspoint.com/android/android_loading_spinner.htm
It helped me.
Layout:
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
After defining it in xml, you have to get its reference in java file through ProgressBar class. Its syntax is given below:
private ProgressBar spinner;
spinner = (ProgressBar)findViewById(R.id.progressBar1);
After that you can make its disappear , and bring it back when needed through setVisibility Method. Its syntax is given below:
spinner.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);