Reputation: 242
When I'm sliding the persistent bottom sheet inside my app, I want to know the current height of the bottom sheet at that moment. I tried with calling BottomSheetCallback's onSlide method but it didn't work.
bottomSheetView = findViewById(R.id.bottom_sheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView);
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if(BottomSheetBehavior.STATE_DRAGGING == newState)
Log.i("MainActivity", "onStateChanged >> " + bottomSheet.getHeight());
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
Log.i("MainActivity", "onSlide bottomSheet>> " + bottomSheet.getHeight());
Log.i("MainActivity", "onSlide bottomSheetView>> " + bottomSheetView.getHeight());
}
});
Upvotes: 3
Views: 5882
Reputation: 12666
What do you mean "didn't work"? This is how I compute the current height while sliding:
private val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
drawerHeight = bottomSheet.height * slideOffset // Current height of bottom sheet
}
override fun onStateChanged(bottomSheet: View, newState: Int) = Unit
}
Upvotes: 3
Reputation: 1252
I think the height won't change when sliding since the actual view of the bottom sheet is not changed. The bottom sheet itself just sliding down.
But you still can get the height by a little calculation using locationOnScreen of the bottomSheetView.
For example:
bottomSheetBehavior.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, slideOffset: Float) {
var locationOnScreen: IntArray = intArrayOf(0, 0)
bottomSheet.getLocationOnScreen(locationOnScreen)
Log.d(
"BottomSheet",
"slideOffset: $slideOffset - view height: ${locationOnScreen[0]},${locationOnScreen[1]}"
)
}
override fun onStateChanged(bottomSheet: View, newState: Int) {
Log.d("BottomSheet", "State: $newState")
}
})
locationOnScreen's value keeps changing when the BottomSheet is sliding up/down.
Upvotes: 1
Reputation: 3353
give id to your parent layout of bottom sheet then get your height , for example if your parent is
RelativeLayout relative = findviewbyid (R.id.rel);
relative.getheight();
or depend your condition get height in another ways
you can always get your height or width of layouts in onWindowFocusChanged
method
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int height = relative.getHeight();
Toast.makeText(this, String.valueOf(height), Toast.LENGTH_SHORT).show();
}
Upvotes: 0
Reputation: 887
This is my option to get shift down.
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
int currentHeight = rootContainer.getHeight() - bottomSheet.getHeight();
int bottomSheetShiftDown = currentHeight - bottomSheet.getTop();
rootContainer.setPadding(
0,
0,
0,
(mBottomSheet.getPeekHeight() + bottomSheetShiftDown));
}
Upvotes: 0