Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a ProgressBar on a View while doing some work?

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?

like image 338
azizbekian Avatar asked Dec 04 '12 18:12

azizbekian


People also ask

Which mode determine the percentage of work done in the ProgressBar view?

Android ProgressBar with Determinate Mode Generally, we use the Determinate progress mode in progress bar when we want to show the quantity of progress has occurred. For example, the percentage of file downloaded, number of records inserted into a database, etc.

What is progress bar in android?

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.

What are the different types of progress bar?

Progress bar supports two modes to represent progress: determinate, and indeterminate.


1 Answers

There's no need to manipulate your ProgressBar's visibility. ListView has a feature called "empty view" which is shown only if your ListView is empty. Here's what you need to do to use it:

  1. If your activity extends ListActivity, use android:id="@android:id/list" for your ListView and android:id="@android:id/empty" for your ProgressBar.
  2. If you don't extend ListActivity, there's a setEmptyView() 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 use ViewSwitcher, which also has great advantage - it lets you apply transition animation. For details on ViewSwitcher, take a look at this.

like image 83
Andrii Chernenko Avatar answered Sep 24 '22 08:09

Andrii Chernenko