ProgressBar is a dialog box which gives the current status of Activity on Android such as downloading and uploading of files. Here, we are using ProgressDialog box, which is a sub-class of AlertDialog box which helps us to show the progress bar.
It has following method setCancelable(bool), SetMessage(string), setProgressStyle(style), setProgress(int) , setMax(int) and finally use show() to show the controls.
Attributes
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsample.msclient009.progressbar">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Main_Activity.xml
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mapsample.msclient009.progressbar.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:layout_marginTop="20dp"
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/btn"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Progress-Status" />
</LinearLayout>
</LinearLayout>
Main_Activity.java
package com.mapsample.msclient009.progressbar;
import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
ProgressDialog pdbar;
Button btn;
int status = 0;
Handler progresHandler = new Handler ( );
long FSize = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
progressChanged();
}
public void progressChanged()
{ btn =(Button)findViewById ( R.id.btn );
btn.setOnClickListener ( new View.OnClickListener ( ) {
@Override
public void onClick(View v) {
pdbar = new ProgressDialog ( v.getContext () );
pdbar.setCancelable (true );
pdbar.setMessage ("Downloading progress..." );
pdbar.setProgressStyle (pdbar.STYLE_HORIZONTAL );
pdbar.setProgress ( 0 );
pdbar.setMax (100);
pdbar.show ();
status = 0;
FSize = 0;
new Thread ( new Runnable ( ) {
@Override
public void run() {
while (status<100)
{
status = checkStatus();
try{Thread.sleep ( 1000 );}catch (Exception e){e.printStackTrace ();}
progresHandler.post ( new Runnable ( ) {
@Override
public void run() {
pdbar.setProgress ( status );
}
} );
}
if(status>=100)
{ try{Thread.sleep ( 1000 );}catch(Exception e){e.printStackTrace ();}
pdbar.dismiss ();
}
}
} ).start ();
}
} );
}
public int checkStatus() {
while (FSize <= 5000) {
FSize++;
if (FSize == 500)
return 15;
else if (FSize == 1000)
return 30;
else if (FSize == 2000)
return 45;
else if (FSize==3000)
return 100;
}
return 100;
}
}
Launch Your APP

Leave Comment