Last active
August 17, 2020 20:29
-
-
Save dhagz/771b68921b31395f126555305392badd to your computer and use it in GitHub Desktop.
A simple indeterminate progress dialog. Call with MyProgressDialog.show(context) and dismiss with MyProgressDialog.dismiss(myProgressDialog).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Dialog; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Color; | |
import android.graphics.drawable.GradientDrawable; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.widget.ProgressBar; | |
import android.widget.RelativeLayout; | |
/** | |
* @author Dhagz | |
* @since 5/12/2016 | |
*/ | |
public class MyProgressDialog extends Dialog { | |
public static MyProgressDialog show(Context context) { | |
MyProgressDialog dialog = new MyProgressDialog(context); | |
try { | |
dialog.show(); | |
} catch (WindowManager.BadTokenException badTokenException) { | |
badTokenException.printStackTrace(); | |
} | |
return dialog; | |
} | |
public static void dismiss(MyProgressDialog dialog) { | |
if (dialog != null && dialog.isShowing()) { | |
try { | |
dialog.dismiss(); | |
} catch (IllegalArgumentException ex) { | |
// | |
} | |
} | |
} | |
public MyProgressDialog(Context context) { | |
super(context); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
RelativeLayout layout = new RelativeLayout(context); | |
ProgressBar progressBar = new ProgressBar(context); | |
RelativeLayout.LayoutParams params = | |
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT); | |
params.addRule(RelativeLayout.CENTER_IN_PARENT); | |
int dp16 = dpToPx(16); | |
layout.setPadding(dp16, dp16, dp16, dp16); | |
layout.addView(progressBar, params); | |
setContentView(layout); | |
GradientDrawable shape = new GradientDrawable(); | |
shape.setColor(Color.parseColor("#A2FFFFFF")); | |
shape.setCornerRadius(dp16); | |
getWindow().setBackgroundDrawable(shape); | |
setCancelable(false); | |
} | |
@Override public void onBackPressed() { | |
// disable on back pressed | |
// super.onBackPressed(); | |
} | |
/** | |
* Source: http://stackoverflow.com/questions/8295986/how-to-calculate-dp-from-pixels-in-android-programmatically | |
* | |
* @param dp value to be converted to pixels | |
* @return pixel value for dp | |
*/ | |
public static int dpToPx(int dp) { | |
return (int) (dp * Resources.getSystem().getDisplayMetrics().density); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Dialog | |
import android.content.Context | |
import android.content.res.Resources | |
import android.graphics.Color | |
import android.graphics.drawable.GradientDrawable | |
import android.view.ViewGroup | |
import android.view.Window | |
import android.widget.ProgressBar | |
import android.widget.RelativeLayout | |
class MyProgressDialog(context: Context) : Dialog(context) { | |
companion object { | |
fun show(context: Context?): MyProgressDialog? { | |
// Return null if context is null | |
if (context == null) return null | |
val dialog = MyProgressDialog(context) | |
try { | |
dialog.show() | |
} catch (ex: Exception) { | |
ex.printStackTrace() | |
} | |
return dialog | |
} | |
fun dismiss(dialog: MyProgressDialog) { | |
try { | |
dialog.dismiss() | |
} catch (ex: Exception) { | |
ex.printStackTrace() | |
} | |
} | |
} | |
init { | |
requestWindowFeature(Window.FEATURE_NO_TITLE) | |
val layout = RelativeLayout(context) | |
val progressBar = ProgressBar(context) | |
val params = RelativeLayout.LayoutParams( | |
ViewGroup.LayoutParams.WRAP_CONTENT, | |
ViewGroup.LayoutParams.WRAP_CONTENT | |
) | |
params.addRule(RelativeLayout.CENTER_IN_PARENT) | |
val dp16 = dpToPx(16) | |
layout.setPadding(dp16, dp16, dp16, dp16) | |
layout.addView(progressBar, params) | |
setContentView(layout) | |
val shape = GradientDrawable() | |
shape.setColor(Color.parseColor("#A2FFFFFF")) | |
shape.cornerRadius = dp16.toFloat() | |
window?.setBackgroundDrawable(shape) | |
setCancelable(false) | |
} | |
/** | |
* Source: http://stackoverflow.com/questions/8295986/how-to-calculate-dp-from-pixels-in-android-programmatically | |
* | |
* @param dp value to be converted to pixels | |
* @return pixel value for dp | |
*/ | |
fun dpToPx(dp: Int): Int { | |
return (dp * Resources.getSystem().displayMetrics.density).toInt() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment