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 .
Asked
Active
Viewed 4,819 times
2

Martin Zeitler
- 1
- 19
- 155
- 216

P. Mohanta
- 130
- 2
- 15
-
Here's code https://codesinandroid.blogspot.com/2014/08/dialog-with-view-pager.html – Sanjay Bhalani Jan 02 '20 at 12:24
1 Answers
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
-
-
-
Is it possible to use a fragment as my dialog box ? Because I need to add more number text lines and some customization to it. – P. Mohanta Jan 03 '20 at 12:09
-
1It doesn't need to be a fragment - but you can use a custom view as a dialog box. I'll edit my answer to show this. – tendai Jan 03 '20 at 12:18
-
-
warning showing in null . can we avoid using null at inflater.inflate(R.layout.dialog_custom_view, null) ? – P. Mohanta Jan 04 '20 at 11:26