Skip to content

Instantly share code, notes, and snippets.

@hitesh-dhamshaniya
Created September 23, 2015 09:23
Show Gist options
  • Save hitesh-dhamshaniya/51a9597da013a0515100 to your computer and use it in GitHub Desktop.
Save hitesh-dhamshaniya/51a9597da013a0515100 to your computer and use it in GitHub Desktop.
***************** Java ******************
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.devdigital.ohboy.R;
public class ProgressHUD extends Dialog {
public ProgressHUD(Context context) {
super(context);
}
public ProgressHUD(Context context, int theme) {
super(context, theme);
}
public void onWindowFocusChanged(boolean hasFocus) {
ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
AnimationDrawable spinner = (AnimationDrawable) imageView
.getBackground();
spinner.start();
}
public void setMessage(CharSequence message) {
if (message != null && message.length() > 0) {
findViewById(R.id.message).setVisibility(View.VISIBLE);
TextView txt = (TextView) findViewById(R.id.message);
txt.setText(message);
txt.invalidate();
}
}
public static ProgressHUD show(Context context, CharSequence message,
boolean indeterminate, boolean cancelable,
OnCancelListener cancelListener) {
ProgressHUD dialog = new ProgressHUD(context, R.style.ProgressHUD);
dialog.setTitle("");
dialog.setContentView(R.layout.progress_hud);
if (message == null || message.length() == 0) {
dialog.findViewById(R.id.message).setVisibility(View.GONE);
} else {
TextView txt = (TextView) dialog.findViewById(R.id.message);
txt.setText(message);
}
dialog.setCancelable(cancelable);
dialog.setOnCancelListener(cancelListener);
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.2f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
dialog.show();
return dialog;
}
public static ProgressHUD showWithNoListener(Context context,
CharSequence message, boolean indeterminate, boolean cancelable) {
ProgressHUD dialog = new ProgressHUD(context, R.style.ProgressHUD);
dialog.setTitle("");
dialog.setContentView(R.layout.progress_hud);
if (message == null || message.length() == 0) {
dialog.findViewById(R.id.message).setVisibility(View.GONE);
} else {
TextView txt = (TextView) dialog.findViewById(R.id.message);
txt.setText(message);
}
dialog.setCancelable(cancelable);
dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.2f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
dialog.show();
return dialog;
}
}
**************************************************
******************** 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:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="20dp" >
<ImageView
android:id="@+id/spinnerImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/spinner"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Message"
android:visibility="gone"
android:textColor="#FFFFFF" />
</LinearLayout>
***************************************************
***************** Style *************************
<style name="ProgressHUD" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
********************* Use *****************
private ProgressHUD mProgressHUD;
mProgressHUD = ProgressHUD.showWithNoListener(mActivity, "", true, false);
mProgressHUD.setCancelable(false);
mProgressHUD.setCanceledOnTouchOutside(false);
mProgressHUD.show();
@hitesh-dhamshaniya
Copy link
Author

Try it out for making custom Progress Dialog in Android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment