Custom Dialog

Soomin Kim·2023년 3월 28일

Android

목록 보기
11/14

Dialog

Toast vs Snack Bar
Toast:

  • API 1부터 있었음.
  • 액티비티 종료해도 Toast실행.
  • 사용자 입력값을 사용한 실행이 불가능.
  • 스와이프 불가능
  • 사용자에게 보여주고, 메세지를 전달하는데 좋음.

Snack Bar

  • API 23 부터 추가.
  • 액티비티에서만 보임.
  • 뭔가를 설정할 수 있음.
  • 스와이프 가능.
  • 경고 메세지나 주의 사항 메세지로 활용하기 좋음.

Snack Bar

image_btn.setOnClickListener { view ->
	Snackbar.make(view, "You have clicked image button.", Snackbar.LENGTH_LONG).show()
}

Alert Dialog

alert_btn.setOnClickListener { view ->
	alertDialogFunction()
}

private fun alertDialogFunction() {
        val builder = AlertDialog.Builder(this)
        //set title for alert dialog
        builder.setTitle("Alert")
        //set message for alert dialog
        builder.setMessage("This is Alert Dialog. Which is used to show alerts in our app.")
        builder.setIcon(android.R.drawable.ic_dialog_alert)

        //performing positive action
        builder.setPositiveButton("Yes") { dialogInterface, which ->
            Toast.makeText(applicationContext, "clicked yes", Toast.LENGTH_LONG).show()
            dialogInterface.dismiss() // Dialog will be dismissed
        }
        //performing cancel action
        builder.setNeutralButton("Cancel") { dialogInterface, which ->
            Toast.makeText(
                applicationContext,
                "clicked cancel\n operation cancel",
                Toast.LENGTH_LONG
            ).show()
            dialogInterface.dismiss() // Dialog will be dismissed
        }
        //performing negative action
        builder.setNegativeButton("No") { dialogInterface, which ->
            Toast.makeText(applicationContext, "clicked No", Toast.LENGTH_LONG).show()
            dialogInterface.dismiss() // Dialog will be dismissed
        }
        // Create the AlertDialog
        val alertDialog: AlertDialog = builder.create()
        // Set other dialog properties
        alertDialog.setCancelable(false) // Will not allow user to cancel after clicking on remaining screen area.
        alertDialog.show()  // show the dialog to UI
    }

Custom Dialog

btnCustomDialog.setOnClickListener { view ->
	customDialogFunction()
}

private fun customDialogFunction() {
        val customDialog = Dialog(this)
        /*Set the screen content from a layout resource.
    The resource will be inflated, adding all top-level views to the screen.*/
        customDialog.setContentView(R.layout.dialog_custom)
        customDialog.findViewById<TextView>(R.id.tv_submit).setOnClickListener {
            Toast.makeText(applicationContext, "clicked submit", Toast.LENGTH_LONG).show()
            customDialog.dismiss() // Dialog will be dismissed
        }
        customDialog.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
            Toast.makeText(applicationContext, "clicked cancel", Toast.LENGTH_LONG).show()
            customDialog.dismiss()
        }
        //Start the dialog and display it on screen.
        customDialog.show()
    }

dialog_custom.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:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Dialog Title"
        android:textColor="@android:color/black"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Custom Dialog Content Details. Which you can set your own as per requirement."
        android:textColor="@android:color/darker_gray"
        android:textSize="14sp" />

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

        <TextView
            android:id="@+id/tv_submit"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:foreground="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="10dp"
            android:text="SUBMIT"
            android:textColor="@android:color/holo_red_dark"/>

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:foreground="?attr/selectableItemBackground"
            android:gravity="center"
            android:padding="10dp"
            android:text="CANCEL"
            android:textColor="@android:color/black"/>

    </LinearLayout>
</LinearLayout>

Custom Progress Dialog

MainActivity.kt
btnCustomProgressDialog.setOnClickListener { view ->
	customProgressDialogFunction()
}

private fun customProgressDialogFunction() {
        val customProgressDialog = Dialog(this)

        /*Set the screen content from a layout resource.
        The resource will be inflated, adding all top-level views to the screen.*/
        customProgressDialog.setContentView(R.layout.dialog_custom_progress)

        //Start the dialog and display it on screen.
        customProgressDialog.show()
    }

dialog_custom_progress.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:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp">

    <ProgressBar
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginEnd="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please Wait..."
        android:textColor="@android:color/black"
        android:textSize="16sp" />

</LinearLayout>
profile
개발자지망생

0개의 댓글