Tuesday, 1 September 2015

Android Programming Tutorial 48 : Progress Bar In Android

Progress Bar In Android:

Has name implies Progress Bar means to progress the work done.In General if you are downloading the .jpge or .png or any video file file from the Network there you want to know the work progress like was its work is getting made.

This work can be display in terms of Bar, is Called Progress Bar, once the Progress starts our application value will get changed.

Progress Bar will show like 10,20,30,40,50,60,70,80,90,100 the default value is 100
its can not be 10,20,......100  it can be 10, 15, 21, 30, 44, 57,....81,97,100 based on the programmer choice that we will see in details with example...

Progress Bar Example in Android:

File >> New >> Android Application
Enter Project name: ProgressBarExample
Package: listviewwithdb.com.progressivebarapp
Keep other default selections, click Next until you reach Finish

                                     


Copy and Past the following code in the MainActivity.java
package listviewwithdb.com.progressivebarapp;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    ProgressDialog progressDialog;
    private int progressBarStatus = 0;
    private long fileSize = 0;
    private Handler progressBarHandler = new Handler();
    Button btn_ProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_ProgressBar = (Button) findViewById(R.id.btn_ProgressBar);
        btn_ProgressBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressDialog = new ProgressDialog(view.getContext());
                progressDialog.setCancelable(true);
                progressDialog.setMessage("Downloading Started..");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setProgress(0);
                progressDialog.setMax(100);
                progressDialog.show();

                progressBarStatus = 0;
                fileSize = 0;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (progressBarStatus < 100) {
                            progressBarStatus = doSomeTask();
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            progressBarHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    progressDialog.setProgress(progressBarStatus);
                                }
                            });
                        }
                        if (progressBarStatus >= 100) {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            progressDialog.dismiss();
                        }
                    }
                }).start();
            }
        });
    }
    public int doSomeTask() {
        while (fileSize <= 1000000) {
            fileSize++;
            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 35;
            }else if (fileSize == 400000) {
                return 43;
            } else if (fileSize == 500000) {
                return 52;
            } else if (fileSize == 600000) {
                return 59;
            }else if (fileSize == 700000) {
                return 71;
            } else if (fileSize == 800000) {
                return 89;
            } else if (fileSize == 900000) {
                return 95;
            }
        }
        return 100;
    }
}


 Copy and Past the following code in the layout file called activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Progressive Bar Example"
        android:layout_marginTop="80dp"
        android:gravity="center_horizontal"
        android:id="@+id/textView"
        android:textColor="#FF0000"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Downloading......"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:background="#B0C4DE"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/btn_ProgressBar"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

 Copy and Past the following code in the Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="listviewwithdb.com.progressivebarapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Run Your Project as the below by clicking on shift+10 or like the below-->Android Application and observe the output
    
 OutPut:









Please Send Your Comments To ambilpura.sunil@gmail.com





Stay Tune For Next Tutorial...WebView In AndroidIn Android:

No comments:

Post a Comment