How to Show Progress Bar(Circle) in an Activity Having a Listview Before Loading the Listview with Data

how to show progress bar(circle) in an activity having a listview before loading the listview with data

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

Sample Image

ListView with Load More Button and Circle ProgressBar

I came to a solution without adding/removing the footer. Just put the two views (Load More Button and ProgressBar) in LinearLayout. Add the linearlayout as listview footer for first time only. Then change between the button and progressbar by changing visibility property.

Here is the updated code:

layout/list_footer.xml

<?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">
<Button
android:id="@+id/load_more_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Load More"/>

<ProgressBar android:id="@+id/load_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminate="true" />
</LinearLayout>

call this method at the end of Activity.onCreate() to add footer and handle button click (load data, hide button and show progress).

 private void prepareListFooter() {

View view = (View) LayoutInflater.from(this).inflate(R.layout.list_footer, null);

loadMoreButton = (Button) view.findViewById(R.id.load_more_button);
progressBar = (ProgressBar) view.findViewById(R.id.load_progress);
listview.addFooterView(view);
loadMoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//downloadMore();
loadMoreButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
});
}

also this code after adapter change to show the button and hide the progress.

adapter.notifyDataSetChanged();
loadMoreButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);

display ProgressBar ontop of ListView

Try this..

Remove android:layout_alignParentTop="true" and android:layout_gravity="center_horizontal" and add android:layout_centerInParent="true"

<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" >

<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>

</RelativeLayout>

Show a ProgressBar (circle) when the item from the Listview is clicked

Start a new activity passing some parameters you need and create your ListView using exectute method by the AsyncTask class, where you can set your progress bar just using onPreExectute method.

Android Volley - Show progress bar in listview adapter when loading image

You need to change ImageListener.onResponse method to the following:

@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap bitmap = response.getBitmap();
if(bitmap != null){
progressNewsList.setVisibility(View.GONE);
imgNewsItem.setVisibility(View.VISIBLE);
imgNewsItem.setImageBitmap(bitmap);
}
}

The following is the java doc for the ImageListener:

Interface for the response handlers on image requests. The call flow
is this: 1. Upon being attached to a request, onResponse(response,
true) will be invoked to reflect any cached data that was already
available. If the data was available, response.getBitmap() will be
non-null
. 2. After a network response returns, only one of the
following cases will happen: - onResponse(response, false) will be
called if the image was loaded. or - onErrorResponse will be called if
there was an error loading the image

Show Progress Indicator before fill listview using CursorLoader

Add a small ProgressBar to your layout file, with its visibility set to "gone". Just before you create your CursorLoader, set its visibility to "visible". It will appear. In onLoadFinished, set the ProgressBar visibility to "gone". It will disappear, and your ListView will load.

I'd use the small style progressbar. To learn more about this, see the reference docs for android.widget.ProgressBar.

BTW, visibility is controlled with View.setVisibility.

Android (Java): Show Progress-bar while Loading table view data

The problem is you are running new thread on your doInBackground method and you have placed your showAttendanceTable() code inside runOnUiThread() which is causing too many work to consume main thread.Just copy paste the below code, replace it with your doInBackground() method and everything will work fine.

     protected Void doInBackground ( String ... params ) {

showAttendanceTable();
}
return null ;
}

Copy this much part of your code from showAttendanceTable() to onPreExecute() method:

 TableLayout tableLayout = findViewById(R.id.table_layout);
int day_in_month = getDayInMonth(month_year);
int row_size = sid_array.length+1;
TableRow[] rows = new TableRow[row_size];
TextView[] rolls_tv = new TextView[row_size];
TextView[] names_tv = new TextView[row_size];
TextView[][] status_tv = new TextView[row_size][day_in_month+1];
for (int i = 0; i < row_size; i++) {
rolls_tv[i] = new TextView(this);
names_tv[i] = new TextView(this);
for (int j = 1; j <= day_in_month; j++) {
status_tv[i][j] = new TextView(this);
}
}


Related Topics



Leave a reply



Submit