Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snackbar expands when soft keyboard is showing

I'm seeing a problem where the snackbar expands itself to fit the size of a listview on the screen when the soft keyboard is up.

Snackbar with issue enter image description here

Normal snackbar (keyboard not up) Normal snackbar

I've been able to slightly remedy this by setting the height programatically, however then the text disappears from the snackbar and I have no idea how to add it back.

                ViewGroup.LayoutParams lp = snackbarView.getLayoutParams();
                lp.height = 150;
                snackbarView.setLayoutParams(lp);

enter image description here

The snackbar is added as follows:

        mConnectionLostSnackbar = Snackbar.make(view, mConnectionLostString, Snackbar.LENGTH_INDEFINITE);
        final View snackbarView = mConnectionLostSnackbar.getView();
        TextView textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setLineSpacing(0, SNACKBAR_LINE_SPACING_MULTIPLIER);
        snackbarView.getViewTreeObserver().addOnPreDrawListener(new NotDismissiblePreDrawListener(snackbarView));
        mConnectionLostSnackbar.show();

I would appreciate any thoughts on not having the snackbar expand when the keyboard is up. Thanks in advance.

like image 694
Karen Forde Avatar asked Oct 26 '17 14:10

Karen Forde


2 Answers

I have noticed that this only happens when the Snackbar is attached to a CoordinatorLayout that has ANOTHER CoordinatorLayout parent somewhere up the hierarchy. I guess the behaviour of <add as much bottom padding as needed to display the Snackbar above the keyboard> gets duplicated because of the multiple CoordinatorLayouts, and the Snackbar ends up taking the entire screen.

The solution is to attach the Snackbar to the topmost CoordinatorLayout. In my scenario I have an Activity with a CoordinatorLayout content view, that hosts a fragment that has a CoordinatorLayout as the root view. When the fragment needs to display a Snackbar, it attaches it to the Activity's CoordinatorLayout, and then it behaves correctly when the keyboard is displayed. Note that I am now using the AndroidX library instead of the support library, so the behaviour may be slightly different.

like image 188
Ovidiu Avatar answered Nov 15 '22 11:11

Ovidiu


The same happened to me, but I could not find the reason of the problem.

To avoid expanding the snackbar, I decided keep it behind the keyboard. This was achieved by adding this line to the desired activity on the manifest

android:windowSoftInputMode="adjustNothing"

I would really like to know why it happens, because on this project I use snackbar a lot and it only breaks on one activity

like image 1
Bruno Avatar answered Nov 15 '22 12:11

Bruno