Show Android SnackBar above keyboard?

21,071

Solution 1

Set

android:windowSoftInputMode="adjustResize"

in the AndroidManifest.xml for the activity containing your snackbar

Solution 2

If you nest your layout in a ScrollView, the snackbar will appear on top of the keyboard. This is because the view will resize to take up only the available space above the keyboard. And, of course, your View will also be scroll-able if necessary at any time, while the keyboard is shown or not.

Solution 3

You can hide the keyboard when the Snackbar is shown.

InputMethodManager imm = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

Solution 4

I have solved the issue like this:

Create a class:

public class ShowSnackBar {
    public static void show(Context context, String message, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
    }
}

You can access this class from any activity in the application

Usage:

ShowSnackBar.show(LoginOtpActivity.this,"Please enter Email ID /Mobile",linear_container);

Solution 5

The following works:

  1. Ensure the activity/fragment from which you are calling the Snackbar contains a ScrolView.
  2. Ensure you set the snackbar view to findViewById(android.R.id.content):

Snackbar.make(getActivity().findViewById(android.R.id.content), "hello world").show();

  1. OR, if you want to be able to swipe away the Snackbar, ensure the ScrolView is nested within a CoordinatorLayout.

Snackbar.make(getActivity().findViewById(android.R.id.my_coordinator_layout), "hello world").show();

Share:
21,071
fobbymaster
Author by

fobbymaster

Updated on July 05, 2022

Comments

  • fobbymaster
    fobbymaster almost 2 years

    Is it possible to show the Android Snackbar above the keyboard (as in Y coordinate, not layering)? The Snackbar currently gets hidden if the keyboard is shown, which is an undesirable behavior.

  • Menasheh
    Menasheh almost 8 years
    How do you put that in an activity?
  • Philip Stratford
    Philip Stratford over 7 years
    It goes in the AndroidManifest.xml file as an attribute of the Activity in question.
  • fobbymaster
    fobbymaster over 7 years
    The issue with this solution is that it also adjusts the entire screen, which isn't something that we want. We only want the snackbar to be adjusted, not the entire screen.
  • dimsuz
    dimsuz almost 7 years
    I don't know if this is helpful, but I have found that using a DrawerLayuot results in snackbar not showing above the keyboard. If I remove DrawerLayout from the activity, it shows as it should.
  • Awah Teh
    Awah Teh over 6 years
    @fobbymaster, why did you mark this as the answer if as you say "isn't something that we want"?
  • Ivan
    Ivan over 6 years
    in case your snackbar is a response to a typing to a textedit this is very unfriendly to user as it could end up with clicking on anything else and unpleasant to see (flickering). better is to nest with scrollviewer
  • FallasB
    FallasB over 6 years
    Using this with android:fillViewport="true" to make sure ScrollView fill the entire screen should be the accepted answer
  • soshial
    soshial about 5 years
    I loved the 2nd solution. Short, clear and no hacks.
  • Marcello B.
    Marcello B. over 4 years
    Please provide some context on why your code is a valid answer. Please see stackoverflow.com/help/how-to-answer
  • Admin
    Admin about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.