본문 바로가기

Android/공부

커스텀 보틈시트(BottomSheet) 만들기

1. 레이아웃 파일 만들기

자유롭게 작성해주시면 됩니다. 저는 아래처럼 작성했습니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent"
    app:layout_behavior="@string/bottom_sheet_behavior"
    app:behavior_hideable="true"
    app:behavior_peekHeight="329dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/bottom_sheet_give_sticker_first_main_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="79dp"
        android:background="@drawable/shape_bottom_sheet_logout"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:paddingBottom="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/bottom_sheet_give_sticker_first_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="81dp"
            android:fontFamily="@font/noto_sans_kr_bold"
            android:gravity="center"
            android:includeFontPadding="false"
            android:text="@string/naeogkku_first_visit"
            android:textColor="@color/mackduck_yellow"
            android:textSize="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/bottom_sheet_give_sticker_first_title2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:fontFamily="@font/noto_sans_kr_bold"
            android:gravity="center"
            android:includeFontPadding="false"
            android:text="@string/write_review_give_sticker"
            android:textColor="@color/white"
            android:textSize="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/bottom_sheet_give_sticker_first_title" />

        <TextView
            android:id="@+id/bottom_sheet_give_sticker_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:fontFamily="@font/noto_sans_kr_regular"
            android:gravity="center"
            android:includeFontPadding="false"
            android:text="@string/write_reveiw_check_sticker"
            android:textColor="@color/main_gray"
            android:textSize="12dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bottom_sheet_give_sticker_first_title2" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="30dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/bottom_sheet_give_sticker_content">
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/bottom_sheet_give_sticker_first_okay"
                android:layout_width="0dp"
                android:layout_height="56dp"
                android:layout_weight="1"
                android:background="@drawable/shape_round_btn"
                android:elevation="0dp"
                android:fontFamily="@font/noto_sans_kr_medium"
                android:text="스티커 보기"
                android:textColor="@color/kakao_black"
                android:textSize="16dp" />
            <View
                android:layout_width="16dp"
                android:layout_height="0dp"/>
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/bottom_sheet_give_sticker_first_cancel"
                android:layout_width="0dp"
                android:layout_height="56dp"
                android:layout_weight="1"
                android:background="@drawable/shape_round_btn"
                android:backgroundTint="@color/white"
                android:elevation="0dp"
                android:fontFamily="@font/noto_sans_kr_medium"
                android:text="나중에 보기"
                android:textColor="@color/kakao_black"
                android:textSize="16dp" />

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <ImageView
        android:id="@+id/bottom_sheet_give_sticker_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/first_popup_sticker_first_visit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

2. 클래스 생성

BottomSheetDialogFragment를 상속한 클래스를 생성해야 합니다. 저는 아래처럼 작성했습니다.

class FirstStickerBottomSheet(val itemClick: (Int) -> Unit) : BottomSheetDialogFragment() {
    lateinit var firstOkay : AppCompatButton
    lateinit var firstCancel : AppCompatButton

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.bottom_sheet_give_sticker_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // 스티커 보기
        firstOkay = view.findViewById(R.id.bottom_sheet_give_sticker_first_okay)
        firstOkay.setOnClickListener {
            itemClick(0)
            dialog?.dismiss()
        }
        // 나중에 보기
        firstCancel = view.findViewById(R.id.bottom_sheet_give_sticker_first_cancel)
        firstCancel.setOnClickListener {
            itemClick(1)
            dialog?.dismiss()
        }
    }
    override fun getTheme(): Int {
        return R.style.AppBottomSheetDialogTheme
    }
}

이때 전 보틈시트의 윗부분이 투명하도록 구현하고 싶었습니다. 그래서 getTheme() 메서드를 통해 스타일을 커스텀해서 지정해주었습니다. 해당 스타일 코드는 다음과 같습니다.

    <style name="AppBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:navigationBarColor">@color/bottom_sheet_black</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:screenOrientation">portrait</item>
    </style>
    <style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@android:color/transparent</item>
    </style>

3. 액티비티에서 사용

    private fun showFirstSticker() {
        val giveFirstStickerBottomSheet = FirstStickerBottomSheet {
            when (it) {
                0 -> { // 스티커 보기
                    binding.mainBottomNavi.selectedItemId = R.id.main_bottom_navi_refrigerator
                    binding.mainViewpager.setCurrentItem(1, false)
                    val fragment = fragmentList[binding.mainViewpager.currentItem]
                    if (fragment is RefrigeratorFragment) {
                        if (fragment == null) {
                            showCustomToast("null")
                        }
                        else {
                            fragment.gotoSticker()
                        }
                    }
                }
                1 -> { // 나중에 보기
                }
            }
        }
        giveFirstStickerBottomSheet.show(supportFragmentManager, giveFirstStickerBottomSheet.tag)
    }

이제 알맞게 파라미터로 전달된 숫자에 따라서 처리할 함수를 작성하고, show() 메서드를 통해 화면에 띄우면 됩니다.

그럼 위 사진처럼 보틈시트가 화면에 나옵니다.

728x90
반응형