낭만 프로그래머

안드로이드(Android) Custom Dialog 만들기 본문

Android

안드로이드(Android) Custom Dialog 만들기

조영래 2021. 12. 3. 22:50

Android에서 Custom으로 Dialog를 만들어보자
MainActivity.kt 에서 Dialog를 호출하는 소스이다

MainActivity.kt

...

// Setting 클릭시
        binding.ivSetting.setOnClickListener(View.OnClickListener {
            val dlg = SettingDialog(this)
            dlg.setOnOKClickSettingListener{ content ->
                var toast = Toast.makeText(this,content,Toast.LENGTH_SHORT)
                toast.show()
            }
            dlg.start()
        })

...


SettingDialog

import android.app.Dialog
import android.view.Window
import com.ariulsoft.fieldrawmaterial.databinding.DialogSettingBinding

class SettingDialog {
    lateinit var binding : DialogSettingBinding
    lateinit var activity : MainActivity

    private var dlg : Dialog
    private lateinit var listener : SettingDialogOKClickedListener

    constructor(activity : MainActivity) {
        this.activity = activity
        this.dlg = Dialog(activity)
    }

    fun start() {
        binding = DialogSettingBinding.inflate(this.activity.layoutInflater)


        dlg.requestWindowFeature(Window.FEATURE_NO_TITLE)   //타이틀바 제거
        this.dlg.setContentView(binding.root)
        dlg.setCancelable(false)    //다이얼로그의 바깥 화면을 눌렀을 때 다이얼로그가 닫히지 않도록 함

        binding.btnOk.setOnClickListener {
            listener.onOKClickedSetting("설정이 저장 되었습니다")
            dlg.dismiss()
        }

        binding.btnCancel.setOnClickListener {
            dlg.dismiss()
        }

        dlg.show()
    }


    fun setOnOKClickSettingListener(settingListener: (String) -> Unit) {
        this.listener = object: SettingDialogOKClickedListener {
            override fun onOKClickedSetting(content: String) {
                settingListener(content)
            }
        }
    }


    interface SettingDialogOKClickedListener {
        fun onOKClickedSetting(content : String)
    }
}


dialog_setting.xml

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:orientation="vertical">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="URL" />

                <EditText
                    android:id="@+id/editTextTextPersonName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textVisiblePassword"
                    android:privateImeOptions="defaultInputmode=english;" />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="PORT" />

                <EditText
                    android:id="@+id/editTextTextPersonName2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="number" />
            </TableRow>

        </TableLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <android.widget.Button
                android:id="@+id/btnOk"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:background="@drawable/inputoutput_rounded_corner"
                android:backgroundTint="#3F51B5"
                android:paddingTop="13dp"
                android:paddingBottom="13dp"
                android:text="저장"
                android:textColor="@color/white"
                android:textSize="24sp" />

            <android.widget.Button
                android:id="@+id/btnCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:background="@drawable/inputoutput_rounded_corner"
                android:backgroundTint="#3F51B5"
                android:paddingTop="13dp"
                android:paddingBottom="13dp"
                android:text="취소"
                android:textColor="@color/white"
                android:textSize="24sp" />


        </LinearLayout>


    </LinearLayout>

</LinearLayout>