I need to implement BottomSheetDialogFragment
and face with the problem.
I need that my BottomSheetDialogFragment
has fixed height. Does anyone has an
Try following code
layout_bottom_sheet_dialog_fragment.xml
BottomSheetFragment.java
public class BottomSheetFragment extends BottomSheetDialogFragment{
@BindView(R.id.iv_bottomSheetFrag_closeDialog) AppCompatImageView iv_closeDialog;
@BindView(R.id.nav_view_bottomSheetFrag_salesPerson) NavigationView nav_view;
private Context context;
//public constructor
public BottomSheetFragment() {
}
//create custom theme for your bottom sheet modal
@Override
public int getTheme() {
//return super.getTheme();
return R.style.AppBottomSheetDialogTheme;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//return super.onCreateDialog(savedInstanceState);
return new BottomSheetDialog(requireContext(), getTheme()); //set your created theme here
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void setupDialog(@NonNull Dialog dialog, int style)
{
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.layout_bottom_sheet_dialog_fragment, null);
context = contentView.getContext();
ButterKnife.bind(this, contentView);
dialog.setContentView(contentView);
//tv_title.setText(getString(R.string.app_name)); R.style.AppBottomSheetDialogTheme
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
int maxHeight = (int) (height*0.44); //custom height of bottom sheet
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
((BottomSheetBehavior) behavior).setPeekHeight(maxHeight); //changed default peek height of bottom sheet
if (behavior != null && behavior instanceof BottomSheetBehavior)
{
((BottomSheetBehavior) behavior).setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback()
{
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState)
{
String state = "";
switch (newState)
{
case BottomSheetBehavior.STATE_DRAGGING: {
//imgBtnClose.setVisibility(View.INVISIBLE);
iv_closeDialog.setVisibility(View.GONE);
state = "DRAGGING";
break;
}
case BottomSheetBehavior.STATE_SETTLING: {
// imgBtnClose.setVisibility(View.INVISIBLE);
iv_closeDialog.setVisibility(View.GONE);
state = "SETTLING";
break;
}
case BottomSheetBehavior.STATE_EXPANDED: {
// imgBtnClose.setVisibility(View.VISIBLE);
iv_closeDialog.setVisibility(View.VISIBLE);
state = "EXPANDED";
break;
}
case BottomSheetBehavior.STATE_COLLAPSED: {
//imgBtnClose.setVisibility(View.INVISIBLE);
iv_closeDialog.setVisibility(View.GONE);
state = "COLLAPSED";
break;
}
case BottomSheetBehavior.STATE_HIDDEN: {
// imgBtnClose.setVisibility(View.INVISIBLE);
iv_closeDialog.setVisibility(View.GONE);
dismiss();
state = "HIDDEN";
break;
}
}
Log.i("BottomSheetFrag", "onStateChanged: "+ state);
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
//close dialog
iv_closeDialog.setOnClickListener(view -> dismiss());
}
@Override
public void onDestroyView() {
super.onDestroyView();
}}
rounded_dialog.xml
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = item ->
{
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.bNav_menu:
BottomSheetFragment bf = new BottomSheetFragment();
bf.show(getSupportFragmentManager(), bf.getTag());
//bf.setArguments(bundle);
return true;
}
};