0

I am trying to display a toast message on top of the screen but it keep showing on the bottom.

This the code I am using

val toast = Toast.makeText(context, message, Toast.LENGTH_LONG)
toast.setGravity(Gravity.TOP or Gravity.LEFT, 0, 0)
toast.show()

I tried as well to change bkg and color but it seems difficult now even getView become deprecated

any idea who to force the toast on top of the screen ?

Thanks

Seb
  • 2,929
  • 4
  • 30
  • 73
  • Your approach is correct. Does anything happen if you let's say change (Gravity.TOP or Gravity.LEFT, 0, 0) to (Gravity.TOP, 0, 250) – Asparuh Gavrailov Jun 29 '21 at 18:02
  • Have you checked this: https://stackoverflow.com/questions/65004242/toast-setgravity-does-not-work-in-my-avd-nexus-6-api-30 – Zain Jun 29 '21 at 18:07
  • 4
    You can't set gravity since SDK 30, the default position is on the bottom and there is nothing we can do but using external libraries or build your own – javdromero Jun 29 '21 at 18:11

3 Answers3

0

Try with the following code its working just set gravity from TOP.

Toast.makeText(this, "hello world", Toast.LENGTH_LONG).apply {
 //If you want some margin from top to toast you can set yOffSet value
        setGravity(Gravity.TOP, 0, 50)
        show()
    }
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
0

Try this I have tried and it's working fine.

Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
rNkL
  • 376
  • 3
  • 11
-1

I moved to Snackbar instead of Toast message. It allow more easy customization

Seb
  • 2,929
  • 4
  • 30
  • 73