Show Android SnackBar above keyboard?
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:
- Ensure the activity/fragment from which you are calling the
Snackbar
contains aScrolView
. - Ensure you set the snackbar view to
findViewById(android.R.id.content)
:
Snackbar.make(getActivity().findViewById(android.R.id.content), "hello world").show();
- OR, if you want to be able to swipe away the
Snackbar
, ensure theScrolView
is nested within aCoordinatorLayout
.
Snackbar.make(getActivity().findViewById(android.R.id.my_coordinator_layout), "hello world").show();
fobbymaster
Updated on July 05, 2022Comments
-
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 almost 8 yearsHow do you put that in an activity?
-
Philip Stratford over 7 yearsIt goes in the AndroidManifest.xml file as an attribute of the Activity in question.
-
fobbymaster over 7 yearsThe 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 almost 7 yearsI 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 over 6 years@fobbymaster, why did you mark this as the answer if as you say "isn't something that we want"?
-
Ivan over 6 yearsin 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 over 6 yearsUsing this with android:fillViewport="true" to make sure ScrollView fill the entire screen should be the accepted answer
-
soshial about 5 yearsI loved the 2nd solution. Short, clear and no hacks.
-
Marcello B. over 4 yearsPlease provide some context on why your code is a valid answer. Please see stackoverflow.com/help/how-to-answer
-
Admin about 2 yearsAs 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.