안드로이드 앱 개발

Custom Dialog 만들기 본문

안드로이드앱

Custom Dialog 만들기

스텝바이스텝안드로이드 2021. 6. 12. 15:52

1. 먼저 dialog의 xml 을 만들어준다.

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:listitem="@layout/rv_item_message_groups"

        android:orientation="vertical"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/mainPink"
        app:layout_constraintBottom_toTopOf="@+id/dialog_cancel_btn"
        />


    <com.google.android.material.card.MaterialCardView
        android:id="@+id/plus_cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        app:layout_constraintTop_toBottomOf="@+id/rv"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                app:srcCompat="@drawable/white_cross"
                app:tint="@color/mainPink"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>



    </com.google.android.material.card.MaterialCardView>



</androidx.constraintlayout.widget.ConstraintLayout>

 

2. Dialog.kt 클래스 생성

package com.example.today_seyebrowktver

import android.app.Dialog
import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
import android.util.Log
import android.view.Window
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView

class DialogShowMessageGroup(context: Context) {

    private val dlg = Dialog(context)   //부모 액티비티의 context 가 들어감

    private lateinit var addCardview : MaterialCardView
    private lateinit var rv : RecyclerView
    private var adapter: RvMessageGroupAdapter? = null

    private lateinit var listener : MyDialogOKClickedListener
    private lateinit var cancelListener : MyCancelClickedListener

    fun start(context: Context, data: ArrayList<MessageGroupData>){
        dlg.requestWindowFeature(Window.FEATURE_NO_TITLE)   //타이틀바 제거
        dlg.setContentView(R.layout.dialog_show_messagegroup)     //다이얼로그에 사용할 xml 파일을 불러옴
        dlg.setCancelable(true)    //다이얼로그의 바깥 화면을 눌렀을 때 다이얼로그가 닫히지 않도록 함

        rv = dlg.findViewById(R.id.rv)

        adapter = RvMessageGroupAdapter(data)
        rv.layoutManager = LinearLayoutManager(context)
        rv.adapter = adapter


        addCardview = dlg.findViewById(R.id.plus_cardview)
        addCardview.setOnClickListener {

            dlg.dismiss()
        }


        dlg.setOnDismissListener {
            val immhide = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            immhide.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
        }

        dlg.show()
    }

    private fun setData() {

    }

    fun setOnOKClickedListener(listener: (String) -> Unit) {
        this.listener = object: MyDialogOKClickedListener {
            override fun onOKClicked(content: String) {
                listener(content)
            }
        }
    }

    fun setOnCanCelClickedListener(listener: (String) -> Unit){
        this.cancelListener = object :MyCancelClickedListener{
            override fun onCancelClicked(content: String) {
                listener(content)
            }

        }
    }



    interface MyDialogOKClickedListener {
        fun onOKClicked(content: String)

    }

    interface MyCancelClickedListener {
        fun onCancelClicked(content: String)
    }
}

 

3. 띄우고 싶은 액티비티에서 객체를 생성후 호출하기

   val dlg = DialogShowMessageGroup(applicationContext)
            dlg.start(applicationContext, messageGroupList)