How do I change the default height of a BottomSheetDialog?
You can set a bottomSheetDialogTheme
in your Activity, overriding the bottomSheetStyle
attribute's behavior_peekHeight
:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">@dimen/custom_peek_height</item>
</style>
This same technique can be used for other attributes as well, such as adding <item name="behavior_hideable">true</item>
to the AppModalStyle
to change whether the bottom sheet is hideable.
How to change the default height of bottomsheet dialog fragment?
Try this following code.
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.activity_device_nfclocation, null);
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
int maxHeight = (int) (height*0.88);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) contentView.getParent());
mBehavior.setPeekHeight(maxHeight);
dialog.show();
How to increase the height of a BottomSheetDialog to follow a vertical translation
I found an alternative solution to solve this problem. I just added a empty view with 1dp height at the Bottom of my layout. I'm increasing it's size like this
val anim = ValueAnimator.ofInt(AnimationView.measuredHeight, 100)
anim.addUpdateListener()
{
valueAnimator ->
val value = valueAnimator.animatedValue as Int
val layoutParams: ViewGroup.LayoutParams = AnimationView.layoutParams
layoutParams.height = value
AnimationView.layoutParams = layoutParams
}
anim.duration = 500
anim.start()
Unable to set BottomSheetDialog initial height
The way that successfully works for me:
- Add
viewTreeObserver.addOnGlobalLayoutListener
inonCreate
to setup your BottomSheet behavior:
binding.root.viewTreeObserver.addOnGlobalLayoutListener {
setupBottomSheetBehaviorForView(binding.bottomFragmentContainer)
}
- Setup behavior:
private fun setupBottomSheetBehaviorForView(view: FragmentContainerView) {
val behavior = BottomSheetBehavior.from(view)
val screenHeight =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowManager.currentWindowMetrics.bounds.height()
} else {
val metrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(metrics)
metrics.heightPixels
}
val toolbarLocation = IntArray(COORDINATES_ARRAY_SIZE) // was added to avoid overlapping the toolbar
binding.toolbar.getLocationOnScreen(toolbarLocation) // with the bottom sheet (in case of full screen activity)
behavior.apply {
peekHeight = (screenHeight * BOTTOM_SHEET_PEEK_PERCENT).toInt()
isFitToContents = false
halfExpandedRatio = BOTTOM_SHEET_PEEK_PERCENT
expandedOffset = toolbarLocation[1] // it's optional
}
}
where BOTTOM_SHEET_PEEK_PERCENT
is Float const, for 40% initial peek height:
const val BOTTOM_SHEET_PEEK_PERCENT = 0.40f
Related Topics
How to Show a Video Thumbnail from a Video Path
Resolving Problems With Android Studio Freezing Constantly
Missing Android Platform(S) Detected
How to Dynamically Hide a Menu Item in Bottomnavigationview
Retrofit @Body With @Multipart Having Issue
Where Is Adb.Exe in Windows 10 Located
How to Show List of String in Textview on Android
How to Connect SQL Server to Android Application
How to Retrieve Data from Sqlite Database in Android and Display It in Textview
How to Create Files to a Specific Folder in Android Application
How to Stop Refreshing Recyclerview Data Scroll to Top Position Android Everytime
How to Synchronously Load Data from Firebase
Prevent Other Applications Form Capturing/Recording Screen
Remove Scroll Bar Track from Scrollview in Android
Constraint Layout Scroll to Bottom With Keypad Open
Could Not Identify Launch Activity: Default Activity Not Found