12

I create a bottom navigation view. I try to get height of bottom navigation view. Material design says that the height should be 56dp. I don't want to use hard coded value, because I am not sure that this value won't change. How can I get the dimension of the view programmatically like getting status bar's height.

int resourceId =getResources().getIdentifier("status_bar_height","dimen","android");
if (resourceId > 0) {
     height = getResources().getDimensionPixelSize(resourceId);
} 
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
  • 3
    It's name is `"design_bottom_navigation_height"`, and it would be in your package, so replace `"android"` with `getPackageName()`. – Mike M. May 29 '17 at 08:20
  • 1
    @MikeM. `activity.getResources().getIdentifier("design_bottom_navigation_height", "dimen", "android");` returns 0. it is invalid resource id. –  May 29 '17 at 08:30
  • 1
    "...it would be in your package, so replace `"android"` with `getPackageName()`." – Mike M. May 29 '17 at 08:32
  • You are right. I convert it to dp. The value is 168. it is weight, not heigh. –  May 29 '17 at 08:40
  • 1
    Huh? There's no weight involved here. It's a definite measure, in pixels. If you want dp, you need to take the display density into account. You shouldn't really need the dp measure in code, though. Pretty much everything deals in pixels. – Mike M. May 29 '17 at 08:46

3 Answers3

8

You can do this by this in the onCreate method:

   bottomNavigationView.post(new Runnable() {
        @Override
        public void run() {
            int height = (int) bottomNavigationView.getMeasuredHeight();
        }
    });

This will give you height in pixels. Hope this helps

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
3

This is basic code for achieve the height of BottomNavigationView,

int resourceId = getResources().getIdentifier("design_bottom_navigation_height", "dimen", this.getPackageName());
        int height = 0;
        if (resourceId > 0) {
            height = getResources().getDimensionPixelSize(resourceId);
        }
        //height in pixels
        Toast.makeText(this, height + "", Toast.LENGTH_SHORT).show();
        // if you want the height in dp
        float density = getResources().getDisplayMetrics().density;
        float dp = height / density;
        Toast.makeText(this, dp + "", Toast.LENGTH_SHORT).show();
Aman Gupta - ΔMΔN
  • 2,971
  • 2
  • 19
  • 39
3

Here is another approach. To get the height of BottomNavigationView programatically, I use ViewTreeObserver. This allows me to get the right value of the view after its drawn. Below is the sample code:

BottomNavigationView mBottomNavigation= findViewById(R.id.navigation);
    ViewTreeObserver viewTreeObserver = mBottomNavigation.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int viewHeight = mBottomNavigation.getHeight();
                        if (viewHeight != 0)
                            mBottomNavigation.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        //viewWidth = mBottomNavigation.getWidth(); //to get view's width
                    }
                });
            }

It is very important to remove viewObserver listener once we get the view's height, I am removing viewObserver using:

removeOnGlobalLayoutListener(this)

So that it stops listening to every change in that view.

Vijay E
  • 829
  • 10
  • 14