Is it possible to show a Toast message in Android above the keyboard instead of on it when the keyboard is on the screen?
4 Answers
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();
![](../../users/profiles/614807.webp)
- 55,270
- 29
- 150
- 194
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: aGravity
constant, anx-position
offset, and ay-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.
![](../../users/profiles/5337149.webp)
- 143
- 1
- 9
![](../../users/profiles/1069068.webp)
- 79,170
- 20
- 177
- 186
-
Hey do you lose rep for legit downvotes? I just downvoted this plagiarised answer and lost 1 rep... – Jack.Ramsden Feb 16 '16 at 10:29
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:
![](../../users/profiles/-1.webp)
- 1
- 1
![](../../users/profiles/674866.webp)
- 3,765
- 4
- 37
- 50
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!
![](../../users/profiles/4741304.webp)
- 73
- 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