Android Kotlin - AlertDialog(알림창) 기본 및 커스텀

Notepad96

·

2021. 8. 22. 10:03

300x250

 

 


1. 결과

 

 

 

 

 


2. Custom Dialog Layout (layout/custom_dialog.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"
    style="@style/myDialog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingEnd="30dp"
        android:text="타이틀 타이틀"
        android:textSize="30sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:text="내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 내용 "
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancelBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" />

        <Button
            android:id="@+id/okBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK" />
    </LinearLayout>
</LinearLayout>

 

 

 

 

 


3. MainActivity.kt

 

package com.example.alertdialog

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import android.app.AlertDialog
import kotlinx.android.synthetic.main.custom_dialog.view.*

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

    fun dialog(view: View) {
        AlertDialog.Builder(view.context).apply {
            setTitle("알림창 제목")
            setMessage("메세지입니다. 메세지입니다. 메세지입니다. 메세지입니다.")
            setPositiveButton("OK", DialogInterface.OnClickListener { dialog, which ->
                Toast.makeText(view.context, "OK Button Click", Toast.LENGTH_SHORT).show()
            })
            setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, which ->
                Toast.makeText(view.context, "Cancel Button Click", Toast.LENGTH_SHORT).show()
            })
            show()
        }
    }

    fun cuDialog(view: View) {
        val myLayout = layoutInflater.inflate(R.layout.custom_dialog, null)
        val build = AlertDialog.Builder(view.context).apply {
            setView(myLayout)
        }
        val dialog = build.create()
        dialog.show()

        myLayout.okBtn.setOnClickListener {
            Toast.makeText(view.context, "OK Button Click", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }
        myLayout.cancelBtn.setOnClickListener {
            Toast.makeText(view.context, "Cancel Button Click", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }

    }
}

 

fun dialog - 기본 Dialog.

- AlertDialog.Builder() 로 생성을 한다. 생성 후 제공되는 메소드를 이용하여 제목이나 설명같은 기본적인 구성요소를 변경 가능하다.

*
setTitle: 알림창 제목
setMessage: 알림창 내용
setPositiveButton: 알림창 OK 버튼 클릭 이벤트
setNagativeButton: 알림창 Cancel 버튼 클릭 이벤트
*
fun cuDialog - 커스텀 Dialog

- LayoutInflater의 inflate를 사용하여 미리 만들어 놓은 커스텀한 Layout 파일을 연결한다.
- AlertDialog의 setView를 사용하여서 연결한 Layout을 설정한다.

- myLayout을 통하여 커스텀 Layout의 포함된 Button의 접근이 가능하며 코드의 보이는 것처럼 OK, Cancel 버튼의 클릭 이벤트를 구현할 수 있다.

 

 

 


4. 전 체 코 드

 

 

GitHub - Notepad96/Android: Kotlin

Kotlin. Contribute to Notepad96/Android development by creating an account on GitHub.

github.com

 

300x250