Wednesday, January 7, 2015

custom progress dialog make, custom linear progress bar drawing by custom design attributes

loading.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:whatever="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">
    <com.myapp.components.LoadingProgressBar
        android:id="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="5dp"
        android:height="5dp"
        whatever:strokeWidth="5dp"
        whatever:strokeColor="@color/red"
    />
</LinearLayout>

themes.xml

<style name="dialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="StrokeStyleable">
        <attr name="strokeWidth" format="dimension" />
        <attr name="strokeColor" format="color" />
    </declare-styleable>
</resources>

LoadingDialog.java custom dialog

public class LoadingDialog extends Dialog {

    public LoadingDialog(Context context) {
        super(context, R.style.dialogTheme);
    }
   
    public void setProgress(int value) {
        LoadingProgressBar linearProgressBar = (LoadingProgressBar) findViewById(R.id.progressBar);
        if (linearProgressBar != null) {
            linearProgressBar.setPercent(value);
        }
    }
}

LoadingProgressBar.java custom progressbar

public class LoadingProgressBar extends LinearLayout {

    private final int mStrokeColor;
    private final float mStrokeWidth;
    private int percent;
   
    Paint paint = new Paint();

    public LoadingProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.StrokeStyleable, 0, 0);
        mStrokeWidth = a.getDimension(R.styleable.StrokeStyleable_strokeWidth, 10);
        mStrokeColor = a.getColor(R.styleable.StrokeStyleable_strokeColor, Color.WHITE);
    }

    public int getPercent() {
        return percent;
    }

    public void setPercent(int percent) {
        this.percent = (percent > 100 ? 100 : (percent < 0 ? 0 : percent));
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(mStrokeColor);
        paint.setStrokeWidth(mStrokeWidth);
       
        float w = getWidth();
        float h = getHeight();
        float pw = (percent < 10 ? 10 : percent) * w / 100;
       
        canvas.drawLine(0, h - mStrokeWidth, pw, h - mStrokeWidth, paint);
    }
}

activity code usage

private LoadingDialog loading;

protected void doLoading(int percent) {
        if (loading == null) {
            loading = new LoadingDialog(this);
            loading.requestWindowFeature(Window.FEATURE_NO_TITLE);
            loading.setContentView(R.layout.loading);
            Window window = loading.getWindow();
            window.setGravity(Gravity.TOP);
            window.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            loading.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface di) {
                    cancelTasks();
                }
            });
            loading.show();
        }
        if (loading != null) {
            loading.setProgress(percent);
        }
    }

    protected void doLoaded() {
        if (loading != null) {
            loading.dismiss();
            loading = null;
        }
    }


No comments: