0

I am currently implementing the MaterialComponents Snackbar in my app and as I started using

(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

the Snackbar started drawing behind the navigation bar. Usually this shouldn't be of a problem as I would just use code to define a margin on the Snackbar including the navigation bar height, but whyever, it does not get applied and stays like so:

enter image description here

The code I used to define the margin is as follows:

final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) snackBarView.getLayoutParams();
            params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin + ThemeUtils.getNavigationBarHeight(this));

            snackBarView.setLayoutParams(params);

Does anybody see an error I am making? Any help on fixing this would be massively appreciated! Thanks in advance!

Edric
  • 24,639
  • 13
  • 81
  • 91
Made by FA
  • 710
  • 2
  • 7
  • 27

2 Answers2

0

Here is my implementation of Snackbar with FLAG_LAYOUT_NO_LIMITS is, and it works fine.

Snackbar snackbar = Snackbar.make(coordinatorLayout, "Doing good...", Snackbar.LENGTH_LONG);
                View snackBarView = snackbar.getView();
                final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) snackBarView.getLayoutParams();
                params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin + getNavigationBarHeight());

                snackBarView.setLayoutParams(params);
                TextView tv = snackBarView.findViewById(android.support.design.R.id.snackbar_text);
                tv.setTextColor(Color.YELLOW);
                snackBarView.setBackgroundColor(Color.BLACK);
                snackbar.show();

Where my getNavigationBarHeight() is:

public int getNavigationBarHeight() {
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            Toast.makeText(this, "nav bar height: " + getResources().getDimensionPixelSize(resourceId), Toast.LENGTH_SHORT).show();
            return getResources().getDimensionPixelSize(resourceId);
        }
        return 0;
    }
Abdullah Riaz
  • 536
  • 5
  • 19
  • You are using a CoordinatorLayout, I am using a LinearLayout, maybe that's the problem. Trying your code, give me a sec. – Made by FA May 15 '19 at 09:34
  • Yes, it's not working for me. Might be caused by the base layout I would guess – Made by FA May 15 '19 at 09:40
  • Android itself emphasizes to use coordinator-layout as a suitable ancestor viewgroup in order to use snackbar. https://material.io/develop/android/components/snackbar/ – Abdullah Riaz May 15 '19 at 10:02
0

My solution has now been to add a ConstraintLayout in my layout on which I set margins that fit the navigation bar. I then set the Snackbar into this new ConstraintLayout. Quite hacky, but well, better than not working at all I guess...

Made by FA
  • 710
  • 2
  • 7
  • 27