Search code examples
androiddialogandroid-permissions

What is the alternative way of show message in dialog?


I want to generate a alert dialog through which a user can grant permission. But showMessage method has a problem in android studio.

Error code:

"Cannot resolve method ' showMessage(Java.lang.String, anonymous android.content.DialogInterface.OnClickListener)'

Are they any alternative code for doing this?

Here is the current code:

if (!shouldShowRequestPermissionRationale(Manifest.permission.WRITE_CONTACTS)){

     showMessage("bi sahab ro allow kon ",
     new DialogInterface.OnClickListener() {

     @Override
              public void onClick(DialogInterface dialog, int which) {

                  requestPermissions(new String[ 
                  {Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                 REQUEST_CODE_ASK_PERMISSIONS);
               }
  });
 }

Solution

  • Try this code for alert dialog. It should work.

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(_context);
    
                AlertDialog.Builder builder = alertDialogBuilder
                        .setTitle("title goes here")
                        .setMessage("message goes here")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                {
                                    // code body
                                }
                            }
                        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // code body
                            }
                        });
    
    
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.setCancelable(true);
                alertDialog.setCanceledOnTouchOutside(false);
    
                alertDialog.show();
    

    Hope this helps!!!