. f Android Custom Horizontal progress bar | Custom Progress bar ~ Android Developers Blog

Monday, 19 January 2015

Android Custom Horizontal progress bar | Custom Progress bar

Hello Friends,
                  Today,I am going to share my another blog which helps you in customizing
Horizontal Progress Bar.With the help of this Android tutorial we can also implement
the Android PI Chart view with multiple color.

Actually my need to show some PI Chart like view with multiple different color which fills
dynamically base on the percentage,  without using any 3rd party library. So , I first go
with Horizontal progress bar but in horizontal progress bar only show up-to two level 
progress with different color.

Finally, I have written a custom progress bar code using android seek bar. Using this we
can add "N level" of different color with different percentage.And Its very simple and
easily understandable.

Horizontal Progress bar


Here is my code :

1. MainActivity.java

  1. package com.android.customprogressbar;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.Menu;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.  private CustomProgressBar seekbar;  
  13.  private ArrayList<progressitem> progressItemList;  
  14.  private ProgressItem mProgressItem;  
  15.   
  16.  @Override  
  17.  protected void onCreate(Bundle savedInstanceState) {  
  18.   super.onCreate(savedInstanceState);  
  19.   setContentView(R.layout.activity_main);  
  20.   seekbar = ((CustomProgressBar) findViewById(R.id.seekBar0));  
  21.   seekbar.getThumb().mutate().setAlpha(0);  
  22.   initDataToSeekbar();  
  23.  }  
  24.   
  25.  private void initDataToSeekbar() {  
  26.   progressItemList = new ArrayList<progressitem>();  
  27.   // red span  
  28.   mProgressItem = new ProgressItem();  
  29.   mProgressItem.progressItemPercentage = 20;  
  30.   Log.i("Mainactivity", mProgressItem.progressItemPercentage + "");  
  31.   mProgressItem.color = R.color.red;  
  32.   progressItemList.add(mProgressItem);  
  33.   // blue span  
  34.   mProgressItem = new ProgressItem();  
  35.   mProgressItem.progressItemPercentage = 25;  
  36.   mProgressItem.color = R.color.blue;  
  37.   progressItemList.add(mProgressItem);  
  38.   // green span  
  39.   mProgressItem = new ProgressItem();  
  40.   mProgressItem.progressItemPercentage = 35;  
  41.   mProgressItem.color = R.color.green;  
  42.   progressItemList.add(mProgressItem);  
  43.     
  44.   //white span  
  45.   mProgressItem = new ProgressItem();  
  46.   mProgressItem.progressItemPercentage = 20;  
  47.   mProgressItem.color =  R.color.white;  
  48.   progressItemList.add(mProgressItem);  
  49.   
  50.     
  51.   seekbar.initData(progressItemList);  
  52.   seekbar.invalidate();  
  53.  }  
  54.   
  55.  @Override  
  56.  public boolean onCreateOptionsMenu(Menu menu) {  
  57.   getMenuInflater().inflate(R.menu.main, menu);  
  58.   return true;  
  59.  }  
  60. }  
  61.   
  62. </progressitem></progressitem>  
2. CustomProgressBar.java


  1. package com.android.customprogressbar;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Rect;  
  9. import android.util.AttributeSet;  
  10. import android.widget.SeekBar;  
  11.   
  12. public class CustomProgressBar extends SeekBar {  
  13.   
  14.  private ArrayList<progressitem> mProgressItemsList;  
  15.   
  16.  public CustomProgressBar(Context context) {  
  17.   super(context);  
  18.   mProgressItemsList = new ArrayList<progressitem>();  
  19.  }  
  20.   
  21.  public CustomProgressBar(Context context, AttributeSet attrs) {  
  22.   super(context, attrs);  
  23.  }  
  24.   
  25.  public CustomProgressBar(Context context, AttributeSet attrs, int defStyle) {  
  26.   super(context, attrs, defStyle);  
  27.  }  
  28.   
  29.  public void initData(ArrayList<progressitem> progressItemsList) {  
  30.   this.mProgressItemsList = progressItemsList;  
  31.  }  
  32.   
  33.  @Override  
  34.  protected synchronized void onMeasure(int widthMeasureSpec,  
  35.    int heightMeasureSpec) {  
  36.   // TODO Auto-generated method stub  
  37.   super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  38.  }  
  39.   
  40.  protected void onDraw(Canvas canvas) {  
  41.   if (mProgressItemsList.size() > 0) {  
  42.    int progressBarWidth = getWidth();  
  43.    int progressBarHeight = getHeight();  
  44.    int thumboffset = getThumbOffset();  
  45.    int lastProgressX = 0;  
  46.    int progressItemWidth, progressItemRight;  
  47.    for (int i = 0; i < mProgressItemsList.size(); i++) {  
  48.     ProgressItem progressItem = mProgressItemsList.get(i);  
  49.     Paint progressPaint = new Paint();  
  50.     progressPaint.setColor(getResources().getColor(  
  51.       progressItem.color));  
  52.   
  53.     progressItemWidth = (int) (progressItem.progressItemPercentage  
  54.       * progressBarWidth / 100);  
  55.   
  56.     progressItemRight = lastProgressX + progressItemWidth;  
  57.   
  58.     // for last item give right to progress item to the width  
  59.     if (i == mProgressItemsList.size() - 1  
  60.       && progressItemRight != progressBarWidth) {  
  61.      progressItemRight = progressBarWidth;  
  62.     }  
  63.     Rect progressRect = new Rect();  
  64.     progressRect.set(lastProgressX, thumboffset / 2,  
  65.       progressItemRight, progressBarHeight - thumboffset / 2);  
  66.     canvas.drawRect(progressRect, progressPaint);  
  67.     lastProgressX = progressItemRight;  
  68.    }  
  69.    super.onDraw(canvas);  
  70.   }  
  71.   
  72.  }  
  73.   
  74. }  
  75.   
  76. </progressitem></progressitem></progressitem>  

2. ProgressItem.java
  1. package com.android.customprogressbar;  
  2.   
  3. public class ProgressItem {  
  4.  public int color;  
  5.  public float progressItemPercentage;  
  6. }  

Download complete Source code : CustomProgress Bar

Hope , this will helps some one .
Enjoy Coding...   :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

2 comments:

  1. Hello Mukesh,

    You have done a good job with it. But i have tried to implement with oval shape like pie chart but it could not be a proper. So can you able to post for circular shape?

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.