본문 바로가기
안드로이드 자바

[JAVA][Android] BottomSheetDialogFragment 배경색 및 테두리 둥글게 변경

by teamnova 2024. 7. 18.
728x90

오늘은 BottomSheetDialogFragment을 커스텀하여 배경색과 상단 테두리를 둥글게 변경해보겠습니다. 

 

레이아웃 xml 파일 코드(main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <Button
        android:id="@+id/main_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="올리기"
        app:layout_constraintTop_toBottomOf="@+id/main_textView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:id="@+id/main_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textSize="20dp"
        android:text="메인 화면"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

레이아웃 xml 파일 코드(mybottomsheet_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="bottomSheet"
        android:textColor="@color/black"
        android:textSize="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bottomSheet_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:text="내리기"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

메인 액티비티 자바 코드

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView main_textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=findViewById(R.id.main_button);
        main_textView=findViewById(R.id.main_textView);
        MybottomSheet dialog = new MybottomSheet(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //MybottomSheet 라는 태그를 가진 프레그먼트가 없는 경우에만 MybottomSheet dialog 띄우기
                if(getSupportFragmentManager().findFragmentByTag("MybottomSheet")==null){
                    dialog.show(getSupportFragmentManager(), "MybottomSheet");
                }
            }
        });

    }
}

 

BottomSheetDialogFragment 자바 코드

public class MybottomSheet  extends BottomSheetDialogFragment {
    Context context;
    Button bottomSheet_button;
    TextView bottomSheet_textView;

    BottomSheetBehavior bottomSheetBehavior;
    public MybottomSheet(Context context) {
        this.context = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL,  R.style.BottomSheetDialogTheme);
        //이 BottomSheetDialogFragment에 직접 작성한 스타일을 적용함.
        // R.style.BottomSheetDialogThem 은 project>App>res>values>themes>themes.xml에 있음(자세한 사항은 하단 내용 확인)
    }

    @Nullable
    @Override
    public View onCreateView(@Nullable LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mybottomsheet_layout, container, false);
        bottomSheet_button=  view.findViewById(R.id.bottomSheet_button);
        bottomSheet_textView=  view.findViewById(R.id.textView);

        bottomSheet_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

            }
        });
        return view;
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bottomSheetBehavior = BottomSheetBehavior.from((View) (view.getParent()));
        bottomSheetBehavior.setMaxWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

}

 

project > App > res > values > themes > themes.xml 파일

<resources xmlns:tools="http://schemas.android.com/tools">
.....
//기존 내용 하단에 추가

    <style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
    </style>

    <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/bottomsheet_background</item>
    </style>

</resources>

 

 

project > App > res > drawable > bottomsheet_background.xml 파일

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid android:color="#CFBCF3"/>
    <corners
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp"/>
    <stroke android:width="0dp" android:color="#000fff"/>
</shape>