Search code examples
androidandroid-toastandroid-api-levels

Toast appearing on Bottom even though Gravity is set to TOP|LEFT


Here is all the code related to the Toast (it's within the mainActivity). The main (and only) layout is a LinearLayout although I don't think that's relevant since it's not a custom toast.

Toast taux = Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT);
taux.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
taux.show();

Thing is, doesn't matter if I set gravity to any other option (combining two like in the code or with just a single one) it will still appear only on te bottom centered (default position).


Solution

  • documentation

    Warning: Starting from Android Build.VERSION_CODES#R, for apps targeting API level Build.VERSION_CODES#R or higher, this method is a no-op when called on text toasts.

    Snackbar is recommended

    Note that Snackbars are preferred for brief messages while the app is in the foreground.

    Toast.setGravity() can no longer do the work in API 30+ and it is recommended to use Snackbar instead to replace Toast if you're app is in the foreground.