๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ| Android/๐Ÿ“˜ | ๊ธฐ๋ก

[Android, Kotlin] android custom dialog ๋งŒ๋“ค๊ธฐ

by immgga 2022. 11. 20.
๋ฐ˜์‘ํ˜•

custom dialog๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ๋ฅผ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ํŒ์—… ์ฐฝ์„ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

ํ•„์ž๋„ ๋งŽ์ด ์ž์ฃผ ์“ฐ์ง€๋งŒ, ๋งŽ์ด ๊นŒ๋จน๊ธฐ(?) ๋•Œ๋ฌธ์— ์ด๋ฒˆ์— ๊ธฐ๋ก์„ ๋‚จ๊ฒจ๋ณด๋ ค ํ•œ๋‹ค.

 


 

custom dialog ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

1. custom dialog๋กœ ์‚ฌ์šฉํ•  xml ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

๋จผ์ € dialog์˜ 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:layout_gravity="center"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/title_image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/addimage"
        android:layout_gravity="center"/>

    <LinearLayout
        android:id="@+id/chat_info_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginStart="30dp"
        android:layout_marginEnd="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_image">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/dalseohealingbold"
            android:text="์ด๋ฆ„: "
            android:textSize="25sp" 
            android:layout_marginEnd="10dp"/>

        <EditText
            android:id="@+id/chatting_room_edit_text"
            android:layout_width="230dp"
            android:layout_height="60dp"
            android:background="@color/white"
            android:fontFamily="@font/dalseohealingbold"
            android:hint="์ฑ„ํŒ…๋ฐฉ ์ด๋ฆ„"
            android:maxLines="1"
            android:textSize="22sp" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="50dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/create_chatting_room"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/greenyellow"
            android:fontFamily="@font/dalseohealingbold"
            android:text="์ƒ์„ฑ" />

        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/cancel_create"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/greenyellow"
            android:fontFamily="@font/dalseohealingbold"
            android:text="์ทจ์†Œ" />
    </LinearLayout>
</LinearLayout>

๋‹ค์ด์–ผ๋กœ๊ทธ ๋ ˆ์ด์•„์›ƒ xml

2. ๋‹ค์ด์–ผ๋กœ๊ทธ ์ƒ์„ฑํ•˜๊ธฐ

์ผ๋‹จ ํ•„์ž๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋ทฐ ๋ฐ”์ธ๋”ฉ์œผ๋กœ ๊ตฌํ˜„ ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ž˜๋“ค์— ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

viewBinding {
    enabled = true
}

๊ทธ ๋‹ค์Œ์— ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.(ํ•„์ž๋Š” fragment์— ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.)

 

activity?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val dialogBinding = DialogCreateRandomChattingBinding.inflate(layoutInflater)
val dialogBuilder = AlertDialog.Builder(context)
    .setView(dialogBinding.root)

val dialog = dialogBuilder.show()
dialogLogic(dialogBinding, dialog)	// ๋‹ค์ด์–ผ๋กœ๊ทธ์˜ ๊ธฐ๋Šฅ์„ ๋‹ด์€ ํ•จ์ˆ˜

 

์‹คํ–‰ ๊ฒฐ๊ณผ

๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ์ž˜ ์ƒ์„ฑ์ด ๋˜์—ˆ๋‹ค.

 


 

์•ฝ๊ฐ„์˜ tmi์„ ๋‚จ๊ธฐ๋ฉด

custom dialog์˜ ๊ธฐ๋ณธ width, height๋Š” wrap_content ์ด๊ธฐ ๋•Œ๋ฌธ์— xml๋กœ ์ง  ๋ชจ์Šต๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.๊ทธ๋ž˜์„œ ํ•„์ž๋Š” ๊ธฐ๋ณธ width, height๋ฅผ ๋ฐ”๊ฟ€ ๋ฐฉ๋ฒ•์„ ๊ฒ€์ƒ‰ํ•ด ๋ณด์•˜์ง€๋งŒ ์ž˜ ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ xml์„ wrap_content๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•