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.
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.
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: Apart from these Methods, there are other methods defined in the ProgressBar class , that you can use to handle spinner more effectively.
Let's try to run our Loading Spinner application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Eclipse 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: Now click on the load spinner button to turn on the loading spinner.
<ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" />
private ProgressBar spinner;
spinner = (ProgressBar)findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
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 |
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 Eclipse IDE to create an Android application and name it as Spinner under a package com.example.spinner. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Modify the res/values/string.xml to add necessary string components |
5 | Run the application and choose a running android device and install the application on it and verify the results |
package com.example.spinner; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.ProgressBar; public class MainActivity extends Activity { private ProgressBar spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (ProgressBar)findViewById(R.id.progressBar1); spinner.setVisibility(View.GONE); } public void load(View view){ spinner.setVisibility(View.VISIBLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="103dp" android:onClick="load" android:text="@string/hello_world" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" /> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Spinner</string> <string name="action_settings">Settings</string> <string name="hello_world">load spinner</string> </resources> Following is the content of AndroidManifest.xml file. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.spinner" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.spinner.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>
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: Apart from these Methods, there are other methods defined in the ProgressBar class , that you can use to handle spinner more effectively.
- Following is the content of the modified main activity filesrc/com.example.spinner/MainActivity.java.
- Following is the modified content of the xml res/layout/activity_main.xml.
- Following is the content of the res/values/string.xml.
Let's try to run our Loading Spinner application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Eclipse 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: Now click on the load spinner button to turn on the loading spinner.
Post A Comment:
0 comments: