Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Progress Dialog Android

I have followed following links to customize the progress dialog :

How to center progress indicator in ProgressDialog easily (when no title/text passed along)

custom Progress Dialog in android?

I want to create custom progress dialog like in this image enter image description here but that code does not show a progress dialog in my application.Please guide me how to do this ? Also guide me how can i change the color of progress dialog according to my projects theme ?

like image 210
Shruti Avatar asked Nov 28 '11 11:11

Shruti


People also ask

What is progress dialog in Android?

Android ProgressDialog is a dialog box/dialog window which shows the progress of a task. Android Progress Dialog is almost same as ProgressBar with the exception that this is displayed as a dialog box. In order to create a ProgressDialog to display a ProgressBar we need to instantiate it like this.

How do I change font size in progress dialog android?

You can use a spannable string and set the color and size for the spannable string. Use the spannable string to set text color and size.

How do I change the progress bar icon on Android?

Go to the activity_main. xml file and refer to the following code. Open the activity_main. xml file and in the ProgressBar tag and set the drawable in indeterminateDrawable attribute.


1 Answers

I have done this way:

Use this class for Custom Progress Dialog:

public class CustomProgressbar extends Dialog {
    private static CustomProgressbar mCustomProgressbar;
    private CustomProgressbar mProgressbar;
    private OnDismissListener mOnDissmissListener;

    private CustomProgressbar(Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_progressbar);
        this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public CustomProgressbar(Context context, Boolean instance) {
        super(context);
        mProgressbar = new CustomProgressbar(context);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mOnDissmissListener != null) {
            mOnDissmissListener.onDismiss(this);
        }
    }

    public static void showProgressBar(Context context, boolean cancelable) {
        showProgressBar(context, cancelable, null);
    }

    public static void showProgressBar(Context context, boolean cancelable, String message) {
        if (mCustomProgressbar != null && mCustomProgressbar.isShowing()) {
            mCustomProgressbar.cancel();
        }
        mCustomProgressbar = new CustomProgressbar(context);
        mCustomProgressbar.setCancelable(cancelable);
        mCustomProgressbar.show();

    }

    public static void showProgressBar(Context context, OnDismissListener listener) {

        if (mCustomProgressbar != null && mCustomProgressbar.isShowing()) {
            mCustomProgressbar.cancel();
        }
        mCustomProgressbar = new CustomProgressbar(context);
        mCustomProgressbar.setListener(listener);
        mCustomProgressbar.setCancelable(Boolean.TRUE);
        mCustomProgressbar.show();
    }

    public static void hideProgressBar() {
        if (mCustomProgressbar != null) {
            mCustomProgressbar.dismiss();
        }
    }

    private void setListener(OnDismissListener listener) {
        mOnDissmissListener = listener;

    }

    public static void showListViewBottomProgressBar(View view) {
        if (mCustomProgressbar != null) {
            mCustomProgressbar.dismiss();
        }

        view.setVisibility(View.VISIBLE);
    }

    public static void hideListViewBottomProgressBar(View view) {
        if (mCustomProgressbar != null) {
            mCustomProgressbar.dismiss();
        }

        view.setVisibility(View.GONE);
    }

    public void showProgress(Context context, boolean cancelable, String message) {

        if (mProgressbar != null && mProgressbar.isShowing()) {
            mProgressbar.cancel();
        }
        mProgressbar.setCancelable(cancelable);
        mProgressbar.show();
    }

}

dialog_progressbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_progressbar"
    android:gravity="center"
    android:padding="10dp" >

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Add drawable file named with bg_progressbar.xml in drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#7F000000" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

Now whenever you want to start Progress Dialog, just write 1 line code:

CustomProgressbar.showProgressBar(MainActivity.this, false);

Whenever you want to dismiss Progress Dialog, just write 1 line code:

CustomProgressbar.hideProgressBar();

Hope this will help you.

like image 168
Hiren Patel Avatar answered Oct 11 '22 18:10

Hiren Patel