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

Volley for client and server interaction (Android)

Volley provides client and server interaction with QUEUE basis, each and every request process separately, we can cancel our request from QUEUE. HERE is a code sample for Volley with json request format. GenerateRequest.java -------------------------------------------------------------------- package com.entrata.maintenance.network_communication.networkutils; import android.content.Context; import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.entrata.maintenance.application.ERPUserDefaults; import com.entrata.maintenance.network_communication.networkconstant.NetworkConstant; import com.entrata.maintenance.network_communication.networkexception.Error; import com.entrata.maintenance.network_communication.networkutil.SubdomainChange; import com.entrata.maintenance.utils.Logger; import com.google.gson.Gson; import org.json.JSONException; import org.js...
Java Code Examples for javax.net.ssl.HttpsURLConnection The following code examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks for your votes! Code Example 1:   9  From project ADFS , under directory /adfs-hdfs-project/adfs-hdfs/src/main/java/org/apache/hadoop/hdfs/ . Source HsftpFileSystem.java @Override protected HttpURLConnection openConnection ( String path , String query ) throws IOException { query = addDelegationTokenParam ( query ); final URL url = new URL ( "https" , nnAddr . getHostName (), nnAddr . getPort (), path + '?' + query ); HttpsURLConnection conn =( HttpsURLConnection ) url . openConnection (); conn . setHostnameVerifier ( new DummyHostnameVerifier ()); return ( HttpURLConnection ) conn ; } Code Example 2:  ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...