JavaForAndroid
JavaForAndroid

Reputation: 1179

AlertDialog not showing in runOnUIThread in Android

I am trying to show an AlertDialog from a background thread. Therefore I am using the runOnUI Method. The function is called without a problem. I also do not get an error. My code:

 public void showingAlert(final String text){
    activity.runOnUiThread(new Runnable() {
        public void run() {
            Log.e("Test","SHOWING DIALOG");
            AlertDialog.Builder builder;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                builder = new AlertDialog.Builder(activity, android.R.style.Theme_Material_Dialog_Alert);
            } else {
                builder = new AlertDialog.Builder(activity);
            }
            builder.create();
            builder.setTitle("Alert title")
                    .setMessage(""+text)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // continue with delete
                        }
                    })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // do nothing
                        }
                    })
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .show();
        }
    });
}

Do you have any idea what could cause the problem? The class is created by the MainActivity this way: BackgroundClass class = new BackgroundClass(this); (this is the reference from the MainActivity). So the activity object is initialized by the constructor.

Upvotes: 0

Views: 983

Answers (1)

Jay Thakkar
Jay Thakkar

Reputation: 1542

Please add this to line to display alert.

 AlertDialog alertDialog = alertDialogBuilder.create();
 // show it
 alertDialog.show();

and remove .show() from builder

Here is full code that you can try, which is working for me.

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
                alertDialogBuilder.setTitle("Alert");
                alertDialogBuilder.setMessage("message")
                        .setCancelable(false)
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                           downloadAPk(mSelectedMovie);
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

Upvotes: 3

Related Questions