1

I have the following code:

startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Toast.makeText(MainActivity.this, "Please, login to start.",
            Toast.LENGTH_SHORT).show();
        checkPermissionAndStartLogin();
    }
});

On some devices (e.g. Samsung with API level 23) this code leads to Screen Overlay issue, because permissions dialog and Toast are showing at the same time.

How can I fix this issue without removing Toast? Because I need Toast and permissions both. Thank you.

Yamashiro Rion
  • 1,778
  • 1
  • 20
  • 31

2 Answers2

1

That is correct behavior since Toast and the permission's Dialog are two different kind of View.. Toast is visible even outside of application view but dialog is bound to application. If you need to show a message before permission check use a AlertDialog like the one used inside Android Tutorials:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
} else {
    // Permission has already been granted
}
Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
1

The behavior is expected. Since Toast is kind of overlay, it will be appearing along with the pop-up. You can switch to AlertDialog, which could be cleaner in approach.

However if you still want to proceed, since its your requirement, the only way out would be to display toast and the permission sequentially. Since toast message doesn't provide any listeners to notify when it has been dis-appeared you can use following approach:

private static final int SHORT_DELAY = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            displayToastAndStartLogin();
        }    
    });
}

protected void displayToastAndStartLogin() {
    Toast.makeText(MainActivity.this, "Please, login to start.",
            Toast.LENGTH_SHORT).show();

    new android.os.Handler(getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
           checkPermissionAndStartLogin();
        }
    }, SHORT_DELAY );
}
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I have tried this solution, but I found that 2000 ms are not enough, because after Toast closing there's still animation of it. And increasing 2000 ms delay and let user to wait, probably, is not a good idea. I'll try to use AlertDialog instead of Toast, thank you. – Yamashiro Rion Apr 22 '18 at 09:22
  • 1
    Then you have to display the pop or change position of toast. You can refer to this [SO](https://stackoverflow.com/questions/2506876/how-to-change-position-of-toast-in-android) – Sagar Apr 22 '18 at 09:25