Show Toast Message Above Soft Keyboard

suggest change

By default, Android will display Toast messages at the bottom of the screen even if the keyboard is showing. This places a Toast near the bottom of the visible content view, which is above the keyboard when the window resizes. From Android 11, a text toast ignores setGravity. getYOffset() is the toast's own offset, not the keyboard height.

public void showMessage(final String message, final int length) {
    View root = findViewById(android.R.id.content);
    Toast toast = Toast.makeText(this, message, length);
    int yOffset = Math.max(0, root.getHeight() - toast.getYOffset());
    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, yOffset);
    toast.show();
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents