본문 바로가기
안드로이드 코틀린

[Kotlin][Android] BottomSheetDialogFragment 구현해보기

by teamnova 2022. 6. 22.

gradle

implementation 'com.google.android.material:material:1.1.0'

 

MainActivity

class MainActivity : AppCompatActivity() {


    private lateinit var bottomSheetBtn: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bottomSheetBtn = findViewById(R.id.bottom_sheet_btn)
        val bottomSheetDialogFragment = TestBottomSheetDialogFragment(this)

        bottomSheetBtn.setOnClickListener {
            bottomSheetDialogFragment.show(supportFragmentManager,bottomSheetDialogFragment.tag)
        }


    }

}

 

TestBottomSheetDialogFragment

class TestBottomSheetDialogFragment(context : Context)  : BottomSheetDialogFragment() {

    private val mContext: Context = context

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_test_bottom_sheet_dialog, container, false)
        val btnOK: Button = view.findViewById(R.id.success_btn)
        val btnClose: Button = view.findViewById(R.id.failure_btn)

        btnOK.setOnClickListener {
            Toast.makeText(mContext, "확인", Toast.LENGTH_SHORT).show()
            dismiss()
        }

        btnClose.setOnClickListener {
            Toast.makeText(mContext, "닫기", Toast.LENGTH_SHORT).show()
            dismiss()
        }

        return view
    }
}

activity_main.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="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bottom_sheet_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bottom Sheet Dialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

fragment_test_bottom_sheet_dialog

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/success_btn"
            android:text="확인"
            android:layout_margin="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/failure_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="취소"
            android:layout_margin="10dp"
            app:layout_constraintEnd_toEndOf="parent" />

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>