material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[BottomNavigationView] return incorrect view height after measured

Open Sanlorng opened this issue 4 years ago • 8 comments

Description: BottomNavigationView will return incorrect view height after measured

Expected behavior: BottomNavigationView return the correct view height afater measured

Source code: bottomNavigationView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) view.updatePadding(bottom=bottomNavigationView.measuredHeight)

Android API version: Android API 30

Material Library version: 1.5.0-alpha01

Device: OnePlus 7 Pro

Sanlorng avatar Jul 11 '21 07:07 Sanlorng

Can you provide more details?

Like, is it returning 0, or larger/smaller than the actual value?

drchen avatar Jul 12 '21 14:07 drchen

Can you provide more details?

Like, is it returning 0, or larger/smaller than the actual value?

the return value is 0 and BottomNavigationView can't preview in the Android Studio layout editor

Sanlorng avatar Jul 12 '21 15:07 Sanlorng

The same in 1.6.0, but no problem with 1.4.0 It happens when measure() called with MeasureSpec.UNSPECIFIED:

 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        bottomNavigation.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED)
        // ...
}

AT_MOST can be used as workaround :

bottomNavigation.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(heightMeasureSpec, MeasureSpec.AT_MOST))

illuzor avatar Dec 02 '21 18:12 illuzor

Hi @Sanlorng,

When are you trying to get the height of the view? Are you able to wait until the view has been laid out until trying to get it's height?

hunterstich avatar Dec 03 '21 22:12 hunterstich

@Sanlorng any more info on this? Is this still an issue?

hunterstich avatar Feb 23 '22 19:02 hunterstich

@Sanlorng any more info on this? Is this still an issue?

I need confirm again,I make a workaround code,which work fine at these months.

Sanlorng avatar Feb 24 '22 03:02 Sanlorng

I'll close this issue for now due to no further info available. Please feel free to reopen it if you confirm this is still happening.

drchen avatar Mar 18 '22 21:03 drchen

@drchen 👋 I think I faced the same issue, when trying to upgrade from 1.4.0 to the latest stable (1.6.1 at this point)

This change: https://github.com/material-components/material-components-android/commit/7eea2b25bc47553c9b11b7ec5268257588601c76 changed the behavior how onMeasure works.

When trying to manually measure the view with makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), the requested measure spec gets overridden with 0, MeasureSpec.EXACTLY which then results in unwanted behavior ("Unwanted" meaning the measured height is 0). The expected behavior is to respect the minHeight attribute.

Answering one of the previous questions: my use case is to manually measure the view to then capture it with https://github.com/facebook/screenshot-tests-for-android and store as a screenshot for later comparison.

When are you trying to get the height of the view? Are you able to wait until the view has been laid out until trying to get it's height?

I call measure(x, makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)) and then immediately layout(0, 0, measuredWidth, measuredHeight)

mateuszkwiecinski avatar Jun 10 '22 14:06 mateuszkwiecinski