Is it possible to show a Toast message in Android above the keyboard instead of on it when the keyboard is on the screen?
Asked
Active
Viewed 9,710 times
2 Answers
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
- 56,621
- 29
- 151
- 198
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!

Alexander Knauf
- 101
- 9
-
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