2

I'm working with Snackbar and fab support library. I want to get height of Snackbar that gives correct height value with single line and multi line message.

var=mysnackbar.gethight();

but this code does not work because snackbar does have gethight().

Tunaki
  • 132,869
  • 46
  • 340
  • 423
farzad ahmadi
  • 21
  • 1
  • 3
  • What are you trying to acheive in your case? – PunitD Nov 05 '15 at 12:52
  • i'm using `CoordinatorLayout` and when `snackbar` appear, `fab` move up and great work this part. but if before appearing `snackbar` i invisible the `fab`, and then `snackba`r appear, and after appearing `snackbar` i visible my `fab`, `fab` visible wrong location(behind `snackbar`),i want to get height of `snackbar` and `setTranslationY` .i hope understand with my bad english :D @Droidwala – farzad ahmadi Nov 05 '15 at 13:17
  • There is no need to do anything like that.i have written answer for this problem few weeks back..that should work in your case...[http://stackoverflow.com/a/32777587/2819262] – PunitD Nov 05 '15 at 17:23
  • No need to calculate snackbar height and change fab position according to that..all that is automatically handled by coordinator layout... check the answer pasted in above comment.. – PunitD Nov 05 '15 at 17:25
  • yes i know that, but when you invisible your fab before appearing snackbar, and visible fab again you see that your fab moved behind of snackbar. `fab.setVisibility(View.INVISIBLE)` is problem. i almost solved my problem with fade in and fade out animation with `fadein.setFillAfter(true)`.@Droidwala – farzad ahmadi Nov 05 '15 at 17:51

3 Answers3

5

Snackbar will only has height after the show() method called. Try call:

mSnackbar.show();
int height = mSnackbar.getView.getHeight();

Or you can use the callback to listen for Snackbar is shown event and return its height as shown below:

mSnackbar.setCallback(new Snackbar.Callback {
    @Override
    Public void onDismissed(Snackbar snackbar, int event) {
    }

    @Override
    Public void onShown(Snackbar snackbar) {
        int height = snackbar.getView.getHeight();
    }
)
mSnackbar.show();
Grace Coder
  • 804
  • 11
  • 19
0

This should work possibly. Will give the height of your Snackbar in pixels.

int height = yourSnackbar.getView().getHeight();

The reference documentation : https://developer.android.com/reference/android/support/design/widget/Snackbar.html#getView()

https://developer.android.com/reference/android/view/View.html#getHeight()

Narayan Acharya
  • 1,459
  • 1
  • 18
  • 33
0

Adding to answer by @Grace Coder

Snackbar.setCallback() has been deprecated.

Now you need to use Snackbar.addCallback()

    View parentView = findViewById(R.id.activity_main);

    Snackbar snackbar = Snackbar.make(parentView, "Hello there.", Snackbar.LENGTH_SHORT);
    snackbar.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
        @Override
        public void onShown(Snackbar transientBottomBar) {
            int height = transientBottomBar.getView().getHeight();
            parent.setPadding(0, 0, 0, height);

            super.onShown(transientBottomBar);
        }

        @Override
        public void onDismissed(Snackbar transientBottomBar, int event) {
            parent.setPadding(0, 0, 0, 0);

            super.onDismissed(transientBottomBar, event);
        }
    });
    snackbar.show();
Vinay Vissh
  • 457
  • 2
  • 9
  • 12