본문 바로가기

안드로이드/안드로이드

[안드로이드] BottomSheetDialog

 

BottomSheetDialog는 어떠한 이벤트 발생 시 위와 같이 하단에서 내가 설정한 Layout을 보여주는 기능이다.

 

사용 방법

thmes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    
    ...
    
    <style name="BottomSheetTheme" parent="Theme.Design.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheetStyle</item>
    </style>
    <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@android:color/transparent</item>
    </style>
</resources>

 

@android:color/transparent : 배경 투명하게

 

 

interview_bs_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/bottom_sheet"
    android:padding="10dp"
    android:background="@drawable/bottomsheet_bg">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_horizontal_rule"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dp"/>

    <TextView
        android:id="@+id/interview_bs_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_margin="10dp"
        android:text="추가"
        android:textStyle="bold"
        android:textColor="@color/black"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/interview_rv"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>

 

BottomSheetDialog에서 보여주고 싶은 layout을 위와 같이 셋팅한다.

 

 

drawable/bottomsheet_bs.xml(상단 테두리 둥글게)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"/>
    <corners android:topRightRadius="15dp"
        android:topLeftRadius="15dp"/>
</shape>

 

 

MainActivity.kt

class MainActivity : AppCompatActivity() {
	private lateinit var bottomSheetDialog: BottomSheetDialog

	override fun onCreate(savedInstanceState: Bundle?) {
		...
        
        
        binding.button.setOnClickListener{
            // BottomSheetDialog init
            bottomSheetDialog = BottomSheetDialog(context, R.style.BottomSheetTheme)
            
            // BottomsheetDialog layout Event
            val bsBinding = InterviewBsLayoutBinding.inflate(LayoutInflater.from(context))
            bsBinding.interviewBsAdd.setOnClickListener {...}
            
            bottomSheetDialog.setContentView(bsBinding.root)
            bottomSheetDialog.show()
        }
    }
}