Professional Documents
Culture Documents
Android Loading Spinner
Android Loading Spinner
Android Loading Spinner
You can show progress of a task in android through loading progress bar. 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
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.
isIndeterminate()
1
Indicate whether this progress bar is in indeterminate mode
postInvalidate()
2
Cause an invalidate to happen on a subsequent cycle through the event loop
setIndeterminate(boolean indeterminate)
3
Change the indeterminate mode for this progress bar
invalidateDrawable(Drawable dr)
4
Invalidates the specified Drawable
incrementSecondaryProgressBy(int diff)
5
Increase the progress bar's secondary progress by the specified amount
getProgressDrawable()
6
Get the drawable used to draw the progress bar in progress mode
Example
Here is an example demonstrating the use of ProgressBar to handle spinner. It creates a basic application that allows you to turn on the
spinner on clicking the button.
To experiment with this example , you can run this on an actual device or in an emulator.
Steps Description
1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
https://www.tutorialspoint.com/android/android_loading_spinner.htm 1/5
31/10/2017 Android Loading Spinner
4 Need to create a xml file in drawable folder.it contains shape and rotate information about the progress bar
5 Run the application and choose a running android device and install the application on it and verify the results
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
b1=(Button)findViewById(R.id.button);
spinner=(ProgressBar)findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinner.setVisibility(View.VISIBLE);
}
});
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="download"
android:id="@+id/button"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
https://www.tutorialspoint.com/android/android_loading_spinner.htm 2/5
31/10/2017 Android Loading Spinner
android:layout_centerHorizontal="true" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:progressDrawable="@drawable/circular_progress_bar"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_alignLeft="@+id/textview"
android:layout_alignStart="@+id/textview"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="7.0">
<gradient
android:centerColor="#007DD6"
android:endColor="#007DD6"
android:startColor="#007DD6"
android:angle="0"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run our application we just modified. I assume you had created your AVD while doing environment setup. To run the app from
Android studio, open one of your project's activity files and click Run icon from the toolbar. Android studio installs the app on your AVD
and starts it and if everything is fine with your setup and application, it will display following Emulator window
https://www.tutorialspoint.com/android/android_loading_spinner.htm 3/5
31/10/2017 Android Loading Spinner
Now click on the load spinner button to turn on the loading spinner. It is shown in the image below
Advertisements
https://www.tutorialspoint.com/android/android_loading_spinner.htm 4/5
31/10/2017 Android Loading Spinner
https://www.tutorialspoint.com/android/android_loading_spinner.htm 5/5