6

Is it possible to show a Toast message in Android above the keyboard instead of on it when the keyboard is on the screen?

dumazy
  • 11,355
  • 12
  • 54
  • 100
  • 2
    Possible duplicate of [How to change position of Toast in Android?](http://stackoverflow.com/questions/2506876/how-to-change-position-of-toast-in-android) – Bhargav Rao Oct 23 '15 at 10:47

4 Answers4

9

You can change toast postion by following code.

Toast toast= Toast.makeText(getApplicationContext(), 
"Your string here", Toast.LENGTH_SHORT);  
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Chirag
  • 55,270
  • 29
  • 150
  • 194
5

From the documentation,

Positioning your Toast

A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.

Bob
  • 143
  • 1
  • 9
Raghav Sood
  • 79,170
  • 20
  • 177
  • 186
3

1.) Check if Keyboard is visible:

How to check visibility of software keyboard in Android?

2.) If Keyboard is visible show Toast in a different position:

How to change position of Toast in Android?

Community
  • 1
  • 1
Chris Conway
  • 3,765
  • 4
  • 37
  • 50
1

More than 3 years later... and Android has finally an API

As shown detailed by Chris Banes we now can use the WindowInsets API

The core API to solve a Toast above soft keyboard are:

val insets = ViewCompat.getRootWindowInsets(view) // get insets
val imeVisible = insets.isVisible(Type.ime()) // is keyboard visible?
val imeHeight = insets.getInsets(Type.ime()).bottom // voila, your offset

So I build an extension function to Toast:

/**
 * Shows toast above soft keyboard, if exists
 */
 fun Toast.showAboveKeyboard(containerView: View) {

   // get y offset to let toast appear above soft keyboard
   val insets = ViewCompat.getRootWindowInsets(containerView)
   val imeVisible = insets?.isVisible(WindowInsetsCompat.Type.ime()) ?: false
   val imeHeight = insets?.getInsets(WindowInsetsCompat.Type.ime())?.bottom
   val fallbackYOffset = containerView.resources.getDimensionPixelOffset(R.dimen.thirtytwo_grid_unit)
   val noSoftKeyboardYOffset =
    containerView.resources.getDimensionPixelOffset(R.dimen.three_grid_unit)
   setGravity(
        Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM,
        0,
        if (imeVisible) imeHeight ?: fallbackYOffset else noSoftKeyboardYOffset
   )
   show()
}

Usage in a fragment:

Toast.makeText(requireContext(), "Hello Toast", Toast.LENGTH_SHORT)
     .showAboveKeyboard(requireView())

Happy Toast!

  • Hi Alexander. Thanks for this answer. This looks promising :) I'll try to have a look at it and if it works I might change the correct answer here. Not sure when I'll be able to do this, though. – dumazy Oct 20 '20 at 07:59