Loading Spinner in android

The progress bar comes in two shapes. Loading bar and Loading Spinner. In this chapter we will discuss spinner.

Spinner is used to display progress of those tasks whose total time of completion is unknown. In order to use that, you just need to define it in the xml like this.

<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);	

Apart from these Methods, there are other methods defined in the ProgressBar class , that you can use to handle spinner more effectively.

Sr.No Method & description
1

isIndeterminate()

Indicate whether this progress bar is in indeterminate mode

2

postInvalidate()

Cause an invalidate to happen on a subsequent cycle through the event loop

3

setIndeterminate(boolean indeterminate)

Change the indeterminate mode for this progress bar

4

invalidateDrawable(Drawable dr)

Invalidates the specified Drawable

5

incrementSecondaryProgressBy(int diff)

Increase the progress bar's secondary progress by the specified amount

6

getProgressDrawable()

Get the drawable used to draw the progress bar in progress mode