This is a very nice post which we are using in development for display alerts to users.
A progress dialog is not deprecated from the android SDK. So I have created an alternative to show progress dialog in the application.
Kotlin code snippet:
styles.xml: Add below style.
<!--LOADING DIALOG STYLE-->
    <style name="styleLoadingDialog" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
    </style>layout_loading_dialog.xml: you can modify the file at your convenience.
<?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="match_parent"
    android:fitsSystemWindows="true"
    >
    <ImageView
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_centerInParent="true"
        android:clickable="false"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_icon" />
    <ProgressBar
        android:id="@+id/imgLoader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminateTint="@color/colorLoaderProgress"
        android:indeterminateTintMode="src_in"
        android:layout_centerInParent="true"
        />
</RelativeLayout> DialogUtils.kt:  Create a kt class as an object.
object DialogUtils {
    private val TAG = DialogUtils::class.java.name
    private var progressDialog: Dialog? = null
    private var alertDialog: Dialog? = null
    /*
     * showLoadingDialog
     */
    fun showLoadingDialog(mContext: Activity) {
        try {
            progressDialog = Dialog(mContext, R.style.styleLoadingDialog)
            progressDialog!!.setCancelable(false)
            val mProgressView = View.inflate(mContext, R.layout.layout_loading_dialog, null)
            progressDialog!!.setContentView(mProgressView)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                progressDialog!!.window!!.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                progressDialog!!.window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            } else {
                progressDialog!!.window!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            }
            // Set properties
            mProgressView.fitsSystemWindows = false
            if (progressDialog != null && progressDialog!!.isShowing) {
                progressDialog!!.dismiss()
            }
            if (progressDialog != null) {
                progressDialog!!.show()
            }
        }catch (e:Exception) {
            e.printStackTrace()
        }
    }
    /**
     * dismissDialog
     */
    fun dismissDialog() {
        try {
            if (progressDialog != null && progressDialog!!.isShowing) {
                progressDialog!!.dismiss()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    fun dialogOK(title: String, message: String, activity: Activity) {
        try {
            val builder = AlertDialog.Builder(activity,R.style.styleAlertDialog)
            builder.setTitle(title)
            builder.setMessage(message)
            builder.setPositiveButton(R.string.str_ok) { dialog, which -> dialog.dismiss() }
            builder.show()
        } catch (e: Exception) {
            Log.e(TAG, e.toString())
        }
    }
    /**
     * Display message according to your parameters
     *
     * @param mContext Context
     * @param title Title of alert dialog
     * @param message Message of alert dialog
     * @param positiveTitle Positive button text to display. It should not null or blank
     * @param negativeTitle Negative button text to display. It should not null or blank
     * @param positiveClick Positive button click listener
     * @param negativeClick Negative button click listener
     */
    fun showAlertDialog(mContext: Context?, title:String, message: String?,positiveTitle: String?, negativeTitle: String?, positiveClick: DialogInterface.OnClickListener?, negativeClick: DialogInterface.OnClickListener?)
    {
        try
        {
            val builder = AlertDialog.Builder(mContext!!)
            builder.setCancelable(false)
            builder.setTitle(title)
            builder.setMessage(message)
            if (positiveClick != null)
            {
                builder.setPositiveButton(positiveTitle, positiveClick)
            }
            if (negativeClick != null)
            {
                builder.setNegativeButton(negativeTitle, negativeClick)
            }
            if (alertDialog != null && alertDialog!!.isShowing) {
                alertDialog!!.dismiss()
                alertDialog = null
            }
            alertDialog = builder.create()
            (alertDialog as AlertDialog).show()
        }
        catch (e: java.lang.Exception)
        {
            AndroidLog.e(TAG, "Display Alert Dialog Error" + e.message)
        }
    }
}
How to use:
Progress Dialog:
DialogUtils.showLoadingDialog(this@CheckEmailActivity)
DialogUtils.dismissDialog
Alert Dialog:
DialogUtils.dialogOK("title","message",context)
DialogUtils.showAlertDialog(context,"title","message","Ok","Cancel",
                        { dialog, _ ->
                            dialog.dismiss()//Ok event
                        },
                        {dialog, which ->
                            dialog.dismiss()//Ok Cancel
                        })
JAVA code snippet:
1. Simple Alert Dialog
Here is the Function in which there is an implementation code for creating dialog.
Instead of creating dialog for multiple screens or different options, here I have created a method for implement alert dialog based on its Dialog Interface.
This will return the click of the button as it's class itself via DialogInterface.OnClickListener
Method:
public void showSettingsAlert(Context contex,DialogInterface.OnClickListener positive, DialogInterface.OnClickListener negative){try{AlertDialog.Builder builder = new AlertDialog.Builder(contex);builder.setCancelable(false);builder.setTitle(R.string.GPSAlertDialogTitle);builder.setMessage(R.string.GPSAlertDialogMessage);builder.setPositiveButton(R.string.settings, positive);builder.setNegativeButton(R.string.cancel, negative);if(alertdialog != null && alertdialog.isShowing()){alertdialog.dismiss();alertdialog = null;}alertdialog = builder.create();alertdialog.show();}catch (Exception e){AndroidLog.e("Utils", "Display Alert Dialog Error" + e.getMessage());}}
Implementation :
YourclassName.showSettingsAlert(mActivity, new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){//Do stuff here OK button}},new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which){//Do stuff here CANCLE button}} );
2. Single dialog with multiple usage: 
- This dialog is not used while you are using Date and Time dialog because it has same method (onCreateDialog). As a result below 3rd option is feasible for custom dialog.
-Dialog id:
final static int DIALOG_Measurement=100;
     -Call dialog like on Button Tap Event:
Method:
- This dialog is not used while you are using Date and Time dialog because it has same method (onCreateDialog). As a result below 3rd option is feasible for custom dialog.
-Dialog id:
final static int DIALOG_Measurement=100;
                     final static int DIALOG_NumberFormate=101;
                     removeDialog(DIALOG_Measurement);
                     showDialog(DIALOG_Measurement);
                     removeDialog(DIALOG_NumberFormate);
                     showDialog(DIALOG_NumberFormate);
Method:
protected Dialog onCreateDialog(int id) {AlertDialog dialog = null;AlertDialog.Builder builder = null;switch(id){case DIALOG_Measurement:final CharSequence[] items = {"Imperial","Metric"};builder = new AlertDialog.Builder(this);builder.setTitle("Select Options");builder.setItems(items, new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int item) {String name=items[item].toString();TextView txt=(TextView) findViewById(R.id.txt_meas_name);txt.setText(name);}});builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});dialog = builder.create();break;case DIALOG_NumberFormate:final CharSequence[] items1 = {"1000","1,000","1000.00"};builder = new AlertDialog.Builder(this);builder.setTitle("Select Options");builder.setItems(items1, new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int item) {String name=items1[item].toString();TextView txt=(TextView) findViewById(R.id.txt_NumFormate_Value);txt.setText(name);}});builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});dialog = builder.create();break;}return dialog;}
private static final int PAIDWITH_CODE=101String[] temp = null;String Paidwith[]={"Visa","MC"};temp=Paidwith;showSimplePopUp(PAIDWITH_CODE,"Select Paid with");Method:private void showSimplePopUp(final int id,String title){CharSequence[] itemsPhoto = temp;AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);helpBuilder.setTitle(title);//helpBuilder.setMessage("This is a Simple Pop Up");helpBuilder.setItems(itemsPhoto, new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int item){if (id == PAIDWITH_CODE){String paidwith=temp[item].toString();txt_paidwith.setText(paidwith);}}});// Remember, create doesn't show the dialogAlertDialog helpDialog = helpBuilder.create();helpDialog.show();}
You may like
Custom dialog with animation | Dialog effect Android here
More Ref Links:
1. Alert Dialog:
2.Dialog Animation using window Animations:
3.PopUpWindow in Android
4.Progress Dialog in Android:
5.Custom Dialog
                   1. Alert Dialog:
2.Dialog Animation using window Animations:
3.PopUpWindow in Android
4.Progress Dialog in Android:
5.Custom Dialog
I
will be happy if you will provide your feedback or follow this blog. Any suggestion
and help will be appreciated.
Thank
you :)
 
 
No comments:
Post a Comment