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.