반응형
https://www.crocus.co.kr/1748?category=22652
앞선 코드에서 BottomSheetDialog를 쉽게 만들 수 있다.
보통 BottomSheetDialog를 보면 이렇게 나타나는데 크기를 자유롭게 조절하는 방법을 기술해두고자 한다.
출처는 아래와 같다.
https://stackoverflow.com/questions/58651661/how-to-set-max-height-in-bottomsheetdialogfragment
package com.demo.testing;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
public class MyBottomSheetDialog extends BottomSheetDialogFragment {
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
view.findViewById(R.id.close_btn).setOnClickListener(v -> dismiss());
return view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setOnShowListener(dialogInterface -> {
BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
setupRatio(bottomSheetDialog);
});
return dialog;
}
private void setupRatio(BottomSheetDialog bottomSheetDialog) {
//id = com.google.android.material.R.id.design_bottom_sheet for Material Components
//id = android.support.design.R.id.design_bottom_sheet for support librares
FrameLayout bottomSheet = (FrameLayout)
bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();
layoutParams.height = getBottomSheetDialogDefaultHeight();
bottomSheet.setLayoutParams(layoutParams);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private int getBottomSheetDialogDefaultHeight() {
return getWindowHeight() * 85 / 100;
}
private int getWindowHeight() {
// Calculate window height for fullscreen use
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
반응형
'Basic > Android' 카테고리의 다른 글
Bottom sheet dialog 드래그 방지 방법 (0) | 2021.09.03 |
---|---|
Kotlin 기본 변수 선언 (0) | 2021.07.26 |
Android 빠르게 BottomSheetDialog 만들기 (0) | 2021.07.08 |
post runnable과 layout과의 관계 (0) | 2021.06.28 |
Android에서 chips를 사용하는 방법 (0) | 2021.03.29 |