public class CustomProgressDialog {
private static CustomProgressDialog customProgressDialog = null;
private Dialog mDialog;
public static CustomProgressDialog getInstance(){
if (customProgressDialog == null){
customProgressDialog = new CustomProgressDialog();
}
return customProgressDialog;
}
public void ShowProgress(Context context, String message,boolean cancelable){
ProgressBar mProgressBar;
TextView textView;
mDialog = new Dialog(context);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
mDialog.setContentView(R.layout.dialog_progressbar);
mProgressBar = mDialog.findViewById(R.id.progress_bar);
textView = mDialog.findViewById(R.id.progress_text);
textView.setText("" + message);
textView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setIndeterminate(true);
mDialog.setCancelable(cancelable);
mDialog.setCanceledOnTouchOutside(cancelable);
mDialog.show();
}
public void hideProgress(){
if (mDialog!=null){
mDialog.dismiss();
mDialog = null;
}
}
dialog_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="24dp"
android:background="@drawable/dialog_background">
<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Your Message"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textSize="16sp"
android:fontFamily="@font/iran_sans"
android:layout_centerInParent="true"
android:visibility="visible" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_alignParentStart="true"/>
</RelativeLayout>
使用
CustomProgressDialog dialog = CustomProgressDialog.getInstance();
dialog.ShowProgress(this,"Your Message",true);
dialog.hide()