2

My app has an activity with ViewPager of three Fragment as pages and each pages has some buttons. So my question is, Is it possible to popup a message or a dialog when click on these buttons, which are inside the fragments pages. Thank You .

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
P. Mohanta
  • 130
  • 2
  • 15

1 Answers1

4

Inside your fragment you wish to show a dialog:

fun showDialog() {
    val dialogBuilder = AlertDialog.Builder(context)
    dialogBuilder.setMessage("The message here")
    dialogBuilder.setPositiveButton("Done",
        DialogInterface.OnClickListener { dialog, whichButton -> })
    val b = dialogBuilder.create()
    b.show()
}

OnClick listener for your button:

btn.setOnClickListener{ showDialog() }

If you wish to have a custom dialog box, you can do the following:

 private lateinit var alertDialog: AlertDialog
    fun showCustomDialog() {
        val inflater: LayoutInflater = this.getLayoutInflater()
        val dialogView: View = inflater.inflate(R.layout.dialog_custom_view, null)

        val header_txt = dialogView.findViewById<TextView>(R.id.header)
        header_txt.text = "Header Message"
        val details_txt = dialogView.findViewById<TextView>(R.id.details)
        val custom_button: Button = dialogView.findViewById(R.id.customBtn)
        custom_button.setOnClickListener {
           //perform custom action
        }
        val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context!!)
        dialogBuilder.setOnDismissListener(object : DialogInterface.OnDismissListener {
            override fun onDismiss(arg0: DialogInterface) {

            }
        })
        dialogBuilder.setView(dialogView)

        alertDialog = dialogBuilder.create();
        alertDialog.window!!.getAttributes().windowAnimations = R.style.PauseDialogAnimation
        alertDialog.show()
    }

The dialog_custom_view.xml layout can be anything you want e.g

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
>
    <LinearLayout
            android:id="@+id/content"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

        <TextView
                android:id="@+id/header"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:ellipsize="end"
                android:textColor="@color/black"
                android:textSize="17sp"
                android:text="Header"
                android:maxLines="3"
                android:textAlignment="center"
                android:layout_marginTop="15dp"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp"
                android:textAllCaps="false"/>

        <TextView
                android:id="@+id/details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="10dp"
                android:text="Details here set multiple lines if you want"
                android:paddingEnd="8dp"
                android:paddingStart="8dp"/>

        <Button
                android:id="@+id/customBtn"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginBottom="15dp"
                android:text="Custom Btn" />

    </LinearLayout>

</ScrollView>
</android.support.design.widget.CoordinatorLayout>
tendai
  • 1,172
  • 1
  • 11
  • 22