Thursday, December 4, 2014

Android Using custom progress Bar Horizontal


Source Code:
==========

Xml File:
========
 <Button
          android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/textView1"
          android:layout_marginTop="31dp"
          android:background="@drawable/button"
          android:onClick="startProgressDialog"
          android:text="Start Downloading File" />

Java file:
========

package com.example.customizedprogressbar;



import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends ActionBarActivity
{

                     ProgressDialog progressBar;
                     private int progressBarStatus = 0;
                     private Handler progressBarHandler = new Handler();
                     Button click_circle;
                     private long fileSize = 0;
 
                    @Override
                    public void onCreate(Bundle savedInstanceState)
                    {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                                click_circle = (Button)findViewById(R.id.button2);
                                click_circle.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent fadein = new Intent(MainActivity.this, CircleProgress.class);
startActivity(fadein);

}
});
                               
                     }

                    public void startProgressDialog(View V)
                  {
       

                 
           
                                  // prepare for a progress bar dialog
           
                                  progressBar = new ProgressDialog(V.getContext());
                                  progressBar.setCancelable(true);
                                  progressBar.show();
                                  progressBar.setMessage("Downloading File...");
                                  progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                  progressBar.setProgress(0);
                                  progressBar.setMax(100);
         
                                  // Get the Drawable custom_progressbar
                                 
                           
                                // Drawable customDrawable= getResources().getDrawable(R.drawable.progressbar2);
                         

                                  // set the drawable as progress drawable

                                  progressBar.setProgressDrawable(customDrawable);

                                  progressBar.show();

                                 //reset progress bar status
                                  progressBarStatus = 0;

                                 //reset filesize
                                  fileSize = 0;

                                  new Thread(new Runnable() {
                                  public void run() {
                                         while (progressBarStatus < 100) {

                                    // process some tasks
                                progressBarStatus = fileDownloadStatus();

                                //  sleep 1 second to show the progress
                                try {
                                        Thread.sleep(1000);
                                    }
                                catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                // Update the progress bar
                                progressBarHandler.post(new Runnable() {
                                    public void run() {
                                        progressBar.setProgress(progressBarStatus);
                                    }
                                });
                            }

                            // when, file is downloaded 100%,
                            if (progressBarStatus >= 100) {

                                // sleep for  2 seconds, so that you can see the 100% of file download
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                // close the progress bar dialog
                                progressBar.dismiss();
                             
                            }
                        }
                    }).start();

      }


     
//method returns the % of file downloaded
    public int fileDownloadStatus()
    {

        while (fileSize <= 10000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            }
            else if (fileSize == 400000) {
                return 40;
            } else if (fileSize == 500000) {
                return 50;
            } else if (fileSize == 600000) {
                return 60;
            }
            else if (fileSize == 700000) {
                return 70;
            }
            else if (fileSize == 800000) {
                return 80;
            }
            else if (fileSize == 900000) {
                return 90;
            }
            else if (fileSize == 1000000) {
           
                return 100;
            }
         
            // write your code here

        }

        return 100;

    }
   
    /*protected void onProgressUpdate(Integer... progress) {
    progressBar.setProgress(progress[0]);
     
   }*/
}

Drawable file :
===========
this xml file include to  res folder

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:angle="270"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.5"
                android:endColor="#ff747674"
                android:startColor="#ff9d9e9d" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="0"
                    android:endColor="#ff009900"
                    android:startColor="#ff000099" />
            </shape>
        </clip>
    </item>
</layer-list>


Screenshot:
============






No comments:

Post a Comment