Skip to content

Instantly share code, notes, and snippets.

@dorianpavetic
Last active February 19, 2021 10:49
Show Gist options
  • Save dorianpavetic/7f8c0df32987f5e6aab3260749179e8f to your computer and use it in GitHub Desktop.
Save dorianpavetic/7f8c0df32987f5e6aab3260749179e8f to your computer and use it in GitHub Desktop.
private void setupRatio(BottomSheetDialog bottomSheetDialog) {
FrameLayout bottomSheet = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
if(bottomSheet == null)
return;
//Retrieve button parameters
buttonLayoutParams = (ConstraintLayout.LayoutParams) binding.sheetButton.getLayoutParams();
//Retrieve bottom sheet parameters
BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_COLLAPSED);
ViewGroup.LayoutParams bottomSheetLayoutParams = bottomSheet.getLayoutParams();
bottomSheetLayoutParams.height = getBottomSheetDialogDefaultHeight();
expandedHeight = bottomSheetLayoutParams.height;
int peekHeight = (int) (expandedHeight / 1.3); //Peek height to 70% of expanded height (Change based on your view)
//Setup bottom sheet
bottomSheet.setLayoutParams(bottomSheetLayoutParams);
BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(false);
BottomSheetBehavior.from(bottomSheet).setPeekHeight(peekHeight);
BottomSheetBehavior.from(bottomSheet).setHideable(true);
//Calculate button margin from top
buttonHeight = binding.sheetButton.getHeight() + 40; //How tall is the button + experimental distance from bottom (Change based on your view)
collapsedMargin = peekHeight - buttonHeight; //Button margin in bottom sheet collapsed state
buttonLayoutParams.topMargin = collapsedMargin;
binding.sheetButton.setLayoutParams(buttonLayoutParams);
//OPTIONAL - Setting up recyclerview margins
ConstraintLayout.LayoutParams recyclerLayoutParams = (ConstraintLayout.LayoutParams) binding.sheetRecyclerview.getLayoutParams();
float k = (buttonHeight - 60) / (float) buttonHeight; //60 is amount that you want to be hidden behind button
recyclerLayoutParams.bottomMargin = (int) (k*buttonHeight); //Recyclerview bottom margin (from button)
binding.sheetRecyclerview.setLayoutParams(recyclerLayoutParams);
}
//Calculates height for 90% of fullscreen
private int getBottomSheetDialogDefaultHeight() {
return getWindowHeight() * 90 / 100;
}
//Calculates window height for fullscreen use
private int getWindowHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) requireContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment