Skip to main content

Custom Progress Bar Source code Android




Custom Dialog Class
/**
 *
 */
package com.exam.customprogress;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author Tatyabhau chavan
 *
 */
public class MyDialog extends Dialog {

    public static final int STATUS_UPDATE = 101;
    public static final int STATUS_COMPLETE = 100;
    public static ProgressBar progressBar;
    TextView textView;
    TextView percent;
    int increment;
    int progress;

    public MyDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

        setContentView(R.layout.my_progress);

        setDialog();
    }

    private void setDialog() {
        setTitle("Downloading Files....");
        textView = (TextView) findViewById(R.id.textProgress);
        progressBar = (ProgressBar) findViewById(R.id.progress_horizontal);
        percent = (TextView) findViewById(R.id.textPercentage);

        percent.setTextColor(Color.BLACK);
        textView.setTextColor(Color.BLACK);

        progressBar.setProgressDrawable(getContext().getResources()
                .getDrawable(R.drawable.greenbackground));
        progressBar.setIndeterminate(false);

        // set the maximum value
        progressBar.setMax(1315);

        LauncherThread();

    }

    // handler for the background updating
    Handler progressHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {

            case STATUS_UPDATE:
                progressBar.setProgress(increment);
                float value = increment / 1315F;
                percent.setText(" " + ((int) (value * 100)) + "%");
                System.out.println(value * 100);
                textView.setText(String.valueOf(progressBar.getProgress())
                        .concat(" / " + progressBar.getMax()));
                break;

            // case STATUS_COMPLETE:
            // if (progressBar != null && progressBar.isShown()) {
            // dismiss();
            // progressBar = null;
            // }
            // dismiss();
            }
        }
    };

    private class LoaderThread extends Thread {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (progressBar.getProgress() < progressBar.getMax()) {
                try {
                    Thread.sleep(100);
                    increment++;
                    progressHandler.sendEmptyMessage(STATUS_UPDATE);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            progressHandler.sendEmptyMessage(STATUS_COMPLETE);
        }

    }

    private class LauncherThread extends Thread {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            progressHandler.sendMessage(progressHandler.obtainMessage());
            progressHandler.sendEmptyMessage(0);
        }
    }

    private void LauncherThread() {
        LoaderThread loaderThread = new LoaderThread();
        // if (!loaderThread.isAlive())
        loaderThread.start();
        LauncherThread launcehrThread = new LauncherThread();
        // if (!launcehrThread.isAlive())
        launcehrThread.start();
    }
}


Main Activity :-
package com.exam.customprogress;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.tomdignan.nifty.dialogs.NiftyBaseDialog;

public class MainActivity extends Activity {

    NiftyBaseDialog mDialog;

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

        MyDialog dialog = new MyDialog(this);
        dialog.show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip" >

        <TextView
            android:id="@+id/textProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/textPercentage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textColor="@android:color/black" />
    </RelativeLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:text="* Parkeon and Telepark records will be added." />

</LinearLayout>


Comments

Popular posts from this blog

Bitmap scalling and cropping from center

How to Bitmap scalling and cropping from center? public class ScalingUtilities {     /**      * Utility function for decoding an image resource. The decoded bitmap will      * be optimized for further scaling to the requested destination dimensions      * and scaling logic.      *      * @param res      *            The resources object containing the image data      * @param resId      *            The resource id of the image data      * @param dstWidth      *            Width of destination area      * @param dstHeight      *     ...

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more /**  * @author Tatyabhau Chavan  *  */ public class Preview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public Camera.Parameters mParameters;     private byte[] mBuffer;     private Activity mActivity;     // this constructor used when requested as an XML resource     public Preview(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public Preview(Context context) {         super(context);         init();     }     public Camera getCamera() {        ...

Recycle view adapter in android

Recycle view adapter             The   RecyclerView   widget is a more advanced and flexible version of   ListView . This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the   RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events. The   RecyclerView   class simplifies the display and handling of large data sets by providing: ·          Layout managers for positioning items ·          Default animations for common item operations, such as removal or addition of items You also have the flexibility to define custom layout managers and animations for   RecyclerView   widgets. RecyclerViewFragment.class public class RecyclerViewFragment...