1

In my application, when I'm trying to display custom AlertDialog box it works fine in android handset. Now when I install the app on my android tab everything works perfect only problem with custom AlertDialog box. It is not displayed. So I thought, I should check for normal dialog box and it works fine. Following is code for both normal dialog and alert dialog box.

Normal Dialog:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");

// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.delete);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                // Write your code here to invoke YES event
                Toast.makeText(getApplicationContext(),
                        "You clicked on YES", Toast.LENGTH_SHORT)
                        .show();
            }
        });

// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to invoke NO event
                Toast.makeText(getApplicationContext(),
                        "You clicked on NO", Toast.LENGTH_SHORT)
                        .show();
                dialog.cancel();
            }
        });

// Showing Alert Message
alertDialog.show();

Custom Layout Dialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());

// AlertDialogBuilder.setMessage(diadis);
LayoutInflater inflater = (LayoutInflater)getApplicationContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.update_status, null);
alertDialogBuilder.setView(view);

final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
alertDialog.show();
alertDialog.findViewById(R.id.positive_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});

// Close
alertDialog.findViewById(R.id.close_button).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

2 Answers2

0

check out this code snippet

private void openQuitDialog() {
    AlertDialog.Builder quitDialog
            = new AlertDialog.Builder(MainActivity.this);
    quitDialog.setTitle(getResources().getString(R.string.are_you_sure_to_exit));
    quitDialog.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    quitDialog.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });

    quitDialog.show();
}

and call the openQuitDialog() where you want.

Hasan shaikh
  • 738
  • 1
  • 6
  • 16
0

For this You need to add the permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

And Also Thanks to CommonsWare's blog post, for run time with Marshmallow

Assuming your code is in Activity or Fragment, check the overlay permission and make a request for it if necessary:

public static int OVERLAY_PERMISSION_REQ_CODE = 1234;

Call this method before your alertdialog

 checkDrawOverlayPermission();

Method copy to your class

public void checkDrawOverlayPermission() {
    if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
    }
}

Then, re-check the permission for better UX:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
        if (!Settings.canDrawOverlays(this)) {
            // SYSTEM_ALERT_WINDOW permission not granted...
        }
    }
}

pemission on for your project on drawing in marashmallow like gif when you add this code to yourproject enter image description here

Arjun saini
  • 4,223
  • 3
  • 23
  • 51