Open ashtanko opened 3 years ago
I just came across with this also. That's because during rotation somehow the width and height are 0. I haven't tried any solution yet, but just in case did you already?
Hi I Have modified the extension functions to use view tree observer to ensure the layout is ready before operating on it try, it out.
fun BottomNavigationView.show() {
if (visibility == VISIBLE) return
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
val parent = parent as ViewGroup
// View needs to be laid out to create a snapshot & know position to animate. If view isn't
// laid out yet, need to do this manually.
if (!isLaidOut) {
measure(
MeasureSpec.makeMeasureSpec(parent.width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(parent.height, MeasureSpec.AT_MOST)
)
layout(parent.left, parent.height - measuredHeight, parent.right, parent.height)
}
val drawable = BitmapDrawable(context.resources, drawToBitmap())
drawable.setBounds(left, parent.height, right, parent.height + height)
parent.overlay.add(drawable)
ValueAnimator.ofInt(parent.height, top).apply {
startDelay = 100L
duration = 300L
interpolator = AnimationUtils.loadInterpolator(
context,
android.R.interpolator.linear_out_slow_in
)
addUpdateListener {
val newTop = it.animatedValue as Int
drawable.setBounds(left, newTop, right, newTop + height)
}
doOnEnd {
parent.overlay.remove(drawable)
visibility = VISIBLE
}
start()
}
}
})
}
fun BottomNavigationView.hide() {
if (visibility == GONE) return
viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
val drawable = BitmapDrawable(context.resources, drawToBitmap())
val parent = parent as ViewGroup
drawable.setBounds(left, top, right, bottom)
parent.overlay.add(drawable)
visibility = GONE
ValueAnimator.ofInt(top, parent.height).apply {
startDelay = 100L
duration = 200L
interpolator = AnimationUtils.loadInterpolator(
context,
android.R.interpolator.fast_out_linear_in
)
addUpdateListener {
val newTop = it.animatedValue as Int
drawable.setBounds(left, newTop, right, newTop + height)
}
doOnEnd {
parent.overlay.remove(drawable)
}
start()
}
}
})
}
Hi, When the smartphone is rotated, the app crash came with backtrace:
Run on: Google Pixel and Android 10. App crashes only when the BottomNavigationView is visible.