본문 바로가기

Android/Kotlin

Model Bottom Sheet

1. activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_open_bottom_sheet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Model Bottom Sheet"/>
    <TextView
        android:id="@+id/text_view_button_clicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button X clicked"
        android:textSize="25sp"/>

</LinearLayout>

2. bottom_sheet_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a BottomSheet"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 1"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button 2"/>
</LinearLayout>

3. ExampleBottomSheetDialog.kt

class ExampleBottomSheetDialog : BottomSheetDialogFragment() {

    private var mListener: BottomSheetListener? = null

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

        val v = inflater.inflate(R.layout.bottom_sheet_layout, container, false)

        val button1 = v.findViewById<Button>(R.id.button1)
        val button2 = v.findViewById<Button>(R.id.button2)

        button1.setOnClickListener {
            mListener!!.onButtonClicked("Button 1 clicked")
            dismiss()
        }

        button2.setOnClickListener {
            mListener!!.onButtonClicked("Button 2 clicked")
            dismiss()
        }

        return v

    }

    interface BottomSheetListener {
        fun onButtonClicked(text: String?)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mListener = try {
            context as BottomSheetListener
        } catch (e: ClassCastException) {
            throw ClassCastException(context.toString() + "must implement BottomSheetListener")
        }
    }
}

4. MainActivity.kt

class MainActivity : AppCompatActivity(), ExampleBottomSheetDialog.BottomSheetListener {

    private var mTextView: TextView? = null

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

        mTextView = findViewById(R.id.text_view_button_clicked)

        val buttonOpenButtonSheet = findViewById<Button>(R.id.button_open_bottom_sheet)

        buttonOpenButtonSheet.setOnClickListener {
            val bottomSheet = ExampleBottomSheetDialog()
            bottomSheet.show(supportFragmentManager, "exampleBottomSheet")
        }
    }

    override fun onButtonClicked(text: String?) {
        mTextView!!.text = text
    }


}

 

참고: codinginflow.com/tutorials/android/modal-bottom-sheet

반응형

'Android > Kotlin' 카테고리의 다른 글

안드로이드 앱위젯 홈화면 자동추가  (0) 2021.04.22
Custom Outline Textview in Kotlin  (0) 2021.04.20
Android BottomSheet Example in Kotlin  (0) 2021.04.17
Switch Button and Toggle Button  (0) 2021.04.10
widget  (0) 2021.03.26