4

I have a GridView which shows a grid of 15 Views for the first time. Then, on click of a button, I add 5 more grid views. So, my question is: How to add a ProgressBar at the bottom of a page, when those 5 Views are loading ? Like a spinner loading and the 5 Views get updated.

Hi i have a tab host tabwidget for tabs and gridview inside framlayout..now if i want the progressbar then should i need to inclue this linearlayout after frame layout or inside frame laoyout?

user1340801
  • 423
  • 3
  • 10
  • 21

4 Answers4

1

Add this below your GridView in the XML.

<LinearLayout
    android:id="@+id/linlaProgressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <ProgressBar
        style="@style/Spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp" />
</LinearLayout>

And in your Activity, where you are loading the data (assuming, it is an AsyncTask), in your onPreExecute() show it:

@Override
protected void onPreExecute() {
    // SHOW THE BOTTOM PROGRESS BAR (SPINNER) WHILE LOADING MORE PHOTOS
    linlaProgressBar.setVisibility(View.VISIBLE);
}

And in the onPostExecute(), hide it:

@Override
protected void onPostExecute() {
    // SHOW THE BOTTOM PROGRESS BAR (SPINNER) WHILE LOADING MORE PHOTOS
    linlaProgressBar.setVisibility(View.GONE);
}

If you are not using an AsyncTask, then set the visibility to View.VISIBLE at the start of the method where you start downloading the data and set it to View.GONE either after or just before you set the Adapter.

EDIT: Adding additional info.

Couple of things.

  1. You are downloading data off the Internet for which, I would recommend switching to AsycnTask instead of using a conventional () Method.
  2. Check out my answer a few days ago on a similar question here: https://stackoverflow.com/a/13265776/450534

In that answer, you will find a complete solution that will suit your exact needs. Well, almost entirely anyway. You may have to make a few modifications and adapt to a few things yourself. But by and large, it will answer all your questions. I use it in my apps and they function as you say, the Google Play loading text at the bottom. And it really is complete. :-)

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • Thanks a lot ..it worked ..but one thing i need is that this linearlayout must be attached to the gridview ..like as he scrolls up this should be attached with loading symbol and then should disappear.. – user1340801 Nov 28 '12 at 10:24
  • @user1340801: It will keep displaying as long as the data is loading. Once it is done, the `View.GONE` will make hide it. – Siddharth Lele Nov 28 '12 at 10:26
  • i understood it..i need it ti be attached to the griview above..like they have in google play when you scroll a gridview you have a layout consisting of progressbar attached with loading.. – user1340801 Nov 28 '12 at 11:27
  • @user1340801: So a _loading..._ with the spinning progress when you scroll to the bottom right? – Siddharth Lele Nov 28 '12 at 11:29
  • Are you using an `AsyncTask` to perform the functions? – Siddharth Lele Nov 28 '12 at 11:31
  • Sure. But how are you running the functions to download the data? Is it a conventional `() Method` or in an `AsyncTask`? And also, are you calling a different method to get the next 5 data set when you click the button? – Siddharth Lele Nov 28 '12 at 11:35
  • its a conventional ()Method where iam parsing the xml after executin a url ..nope im calling the same method when for the next 5 data but change in url.. – user1340801 Nov 28 '12 at 11:42
  • @user1340801: Check my edit. I have linked to a complete solution using `AsyncTask` from one of my answers earlier. It will address all your questions. – Siddharth Lele Nov 28 '12 at 11:53
  • Hi i have a tab host tabwidget for tabs and gridview inside framlayout..now if i want the progressbar then should i need to inclue this linearlayout after frame layout or inside frame laoyout? – user1340801 Dec 04 '12 at 11:16
0

You need to create custom view for that and inflate it and add as bottom view in gridview

<RelativeLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
>

<Button android:text="Load"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
/>          

<ProgressBar 
       android:layout_width="25.0dip"
       android:layout_height="25.0dip"
       android:layout_centerInParent="true"
       android:visibility="gone"
/>

</RelativeLayout>

now inflate this view and show the progressbar at bottom of gridview You can set button as bottom with this when you click just visible the progressbar

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

Just add a ProgressBar in your layout, set its visibility to VISIBLE or GONE whenever you want to show/hide it.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

Add a in the xml of GridView and set its property "alignParentRight = true" and "visibility = invisible" . In your activity , use Async task class and on its preExectue method set set its visibility to visible

pb.setVisibility(View.VISIBLE);

In doInBackground method load your images and finally in onPostExecute method make this progress bar invisible.

pb.setVisibility(View.INVISIBLE);

execute this async task class on click of load button.

This is an easy tutorial for understanding asyncTask

http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

Or threads can be used instead of async task.

Tahreem
  • 205
  • 5
  • 15
  • set adapter in the postExecute method. progressBar visibility and invisibility wont work without async task or threading. You can not make it visible or invisible in a single thread. – Tahreem Nov 28 '12 at 12:07