Sunday, February 22, 2015

Crating Circle Android Custom Progress Bar

In this post we are going to create Circle Progress bar by customizing horizontal default android progress bar.


Step 01
You should create an xml file on drawable file for configure the appearance of progress bar . So Im creating my xml file as  circular_progress_bar.xml.

<?xml version="1.0" encoding="UTF-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="120"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="140">
  <item android:id="@android:id/background">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:useLevel="false"
         android:angle="0"
         android:type="sweep"
        android:thicknessRatio="50.0">
        <solid android:color="#000000"/>
    </shape>
  </item>
  <item android:id="@android:id/progress">
       <rotate
        android:fromDegrees="120"
        android:toDegrees="120">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
         android:angle="0"
         android:type="sweep"
        android:thicknessRatio="50.0">
        <solid android:color="#ffffff"/>
    </shape>
    </rotate>
  </item>
</layer-list>


Step 02
Then create progress bar on your xml file
Then give the name of xml file on your drawable folder as the parth of android:progressDrawable

<ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_marginLeft="0dp"
                android:layout_centerHorizontal="true"
                android:indeterminate="false"
                android:max="100"
                android:progressDrawable="@drawable/circular_progress_bar" />

Step 03
Visual the progress bar using thread

package com.example.progress;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

      private ProgressBar progBar;
      private TextView text;
    private Handler mHandler = new Handler();
    private int mProgressStatus=0;
   
   
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progBar= (ProgressBar)findViewById(R.id.progressBar);
        text = (TextView)findViewById(R.id.textView1);
       
        dosomething();
    }



      public void dosomething() {
     
      new Thread(new Runnable() {
            public void run() {
            final int presentage=0;
                while (mProgressStatus < 63) {
                    mProgressStatus += 1;
                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            progBar.setProgress(mProgressStatus);
                            text.setText(""+mProgressStatus+"%");
                        
                        }
                    });
                    try {
                       
                       
                       
                        Thread.sleep(50);
                       
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
      }

   
}








No comments:

Post a Comment