Open top menu

                      Introduction custom progress-bar ;

The custom progress-bar implement as per your need(Best style).Android provided default multiple progress-bar widgets in multiple style ,but you have to change the progress-bar style for example:-
change progress color and progress style via pro-grammatically in android.




Customprogress_bar.java

package com.example.custom_progrss;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ProgressBar;

public class Customprogress_Bar extends ProgressBar {  
    private String text;
    private Paint textPaint;

    public Customprogress_Bar(Context context) {
        super(context);
        text = "External Memory";
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
    }

    public Customprogress_Bar(Context context, AttributeSet attrs) {
        super(context, attrs);
        text = "External Memory";
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
    }

    public Customprogress_Bar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        text = "External Memory";
        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // First draw the regular progress bar, then custom draw our text
        super.onDraw(canvas);
        Rect bounds = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), bounds);
        int x = getWidth()/2  - bounds.centerX();
        int y = getHeight()/2 - bounds.centerY();
        canvas.drawText(text, x, y, textPaint);
    }

    public synchronized void setText(String text) {
        this.text = text;
        drawableStateChanged();
    }

    public void setTextColor(int color) {
        textPaint.setColor(color);
        drawableStateChanged();
    }
}

you have implement progress-bar in .xml  class same name of above java class. show example.


<com.example.custom_progrss.Customprogress_Bar
    android:id="@+id/player_hp_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"                                   
    android:maxHeight="20dip"
    android:minHeight="20dip"
    android:progress="0" />

complete main.xml class.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
<com.example.custom_progrss.Customprogress_Bar
    android:id="@+id/player_hp_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:maxHeight="20dip"
    android:minHeight="20dip"
    android:progress="0" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="82dp"
    android:text="Button" />

</RelativeLayout>
.................................................................................................................................................................
MainActivity.java

package com.example.custom_progrss;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
Customprogress_Bar expBar;
ProgressBar p;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expBar = (Customprogress_Bar) findViewById(R.id.player_hp_bar);
        expBar.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
        //p=(ProgressBar)findViewById(R.id.player_hp);
        btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
download( );
}
});
}

 
public void download( ){
     
     
     final int totalProgressTime = 100;
     final Thread t = new Thread() {
        @Override
        public void run() {
           int jumpTime = 0;
           
           while(jumpTime < totalProgressTime) {
              try {
                 sleep(200);
                 jumpTime += 5;
                 expBar.setProgress(jumpTime);
              }
              catch (InterruptedException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
              }
           }
        }
     };
     t.start();
  }
}


Thank you .....................................................


Tagged

0 comments