1215

I want to display a dialog/popup window with a message to the user that shows "Are you sure you want to delete this entry?" with one button that says 'Delete'. When Delete is touched, it should delete that entry, otherwise nothing.

I have written a click listener for those buttons, but how do I invoke a dialog or popup and its functionality?

4

38 Answers 38

1
2
2
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("This is Title"); builder.setMessage("This is message for Alert Dialog"); builder.setPositiveButton("Positive Button", (dialog, which) -> onBackPressed()); builder.setNegativeButton("Negative Button", (dialog, which) -> dialog.cancel()); builder.show(); 

This is a way which alike to create the Alert dialog with some line of code.

Sign up to request clarification or add additional context in comments.

Comments

2

Code to delete an entry from the list

 /*--dialog for delete entry--*/ private void cancelBookingAlert() { AlertDialog dialog; final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom); alertDialog.setTitle("Delete Entry"); alertDialog.setMessage("Are you sure you want to delete this entry?"); alertDialog.setCancelable(false); alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //code to delete entry } }); alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog = alertDialog.create(); dialog.show(); } 

Call above method on delete button click

Comments

2

Kotlin Custom dialog: In Case if you want to create custom dialog

Dialog(activity!!, R.style.LoadingIndicatorDialogStyle) .apply { // requestWindowFeature(Window.FEATURE_NO_TITLE) setCancelable(true) setContentView(R.layout.define_your_custom_view_id_here) //access your custom view buttons/editText like below.z val createBt = findViewById<TextView>(R.id.clipboard_create_project) val cancelBt = findViewById<TextView>(R.id.clipboard_cancel_project) val clipboard_et = findViewById<TextView>(R.id.clipboard_et) val manualOption = findViewById<TextView>(R.id.clipboard_manual_add_project_option) //if you want to perform any operation on the button do like this createBt.setOnClickListener { //handle your button click here val enteredData = clipboard_et.text.toString() if (enteredData.isEmpty()) { Utils.toast("Enter project details") } else { navigateToAddProject(enteredData, true) dismiss() } } cancelBt.setOnClickListener { dismiss() } manualOption.setOnClickListener { navigateToAddProject("", false) dismiss() } show() } 

Create LoadingIndicatorDialogStyle in style.xml

<style name="LoadingIndicatorDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:statusBarColor">@color/black_transperant</item> <item name="android:layout_gravity">center</item> <item name="android:background">@android:color/transparent</item> <!--<item name="android:windowAnimationStyle">@style/MaterialDialogSheetAnimation</item>--> 

Comments

1

With Anko (official library from developers of Kotlin), you can simple use

alert("Alert title").show() 

or more complex one:

alert("Hi, I'm Roy", "Have you tried turning it off and on again?") { yesButton { toast("Oh…") } noButton {} }.show() 

To import Anko:

implementation "org.jetbrains.anko:anko:0.10.8" 

Comments

1
 LayoutInflater inflater = LayoutInflater.from(HistoryActivity.this); final View vv = inflater.inflate(R.layout.dialog_processing_tts, null); final AlertDialog.Builder alert = new AlertDialog.Builder( HistoryActivity.this); alert.setTitle("Delete"); alert.setView(vv); alert.setCancelable(false) .setPositiveButton("Delete", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { databaseHelperClass.deleteHistory(list.get(position).getID()); list.clear(); setAdapterForList(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); final AlertDialog dialog = alert.create(); dialog.show(); 

Comments

1

Now with Jetpack Compose introduced in android , you can simply create alert dialog using below code

if (viewModel.shouldDialogOpen.value) { AlertDialog(onDismissRequest = { viewModel.shouldDialogOpen.value = false }, title = { Text("Delete Entry?") }, text = { Text("Are you sure you want to delete this entry?") }, dismissButton = { Button(modifier = Modifier.fillMaxWidth(), onClick = { viewModel.shouldDialogOpen.value = false }) { Text(text = "Cancel") } }, confirmButton = { Button(modifier = Modifier.fillMaxWidth(), onClick = { viewModel.shouldDialogOpen.value = false viewModel.beginDelete(recipe) }) { Text(text = "Okay") } }) } 

Here In viewModel.shouldDialogOpen, shouldDialogOpen is a mutablestate field inside viewmodel whose value we change when we need to show or dismiss dialog.

For more code samples for Jetpack Compose:- https://androidlearnersite.wordpress.com/2021/08/03/jetpack-compose-1-0-0-sample-codes/

Comments

0

You can create Activity and extends AppCompatActivity. Then in the Manifest put next style:

<activity android:name=".YourCustomDialog" android:theme="Theme.AppCompat.Light.Dialog"> </activity> 

Inflate it by Buttons and TextViews

Then use this like a dialog.

For example, in the linearLayout I fill next parameters:

android:layout_width="300dp" android:layout_height="wrap_content" 

Comments

0

In past few days my co-workers keep asking me about using AlertDialog in Xamarin.Android and almost all of them sent this question as the ref which they read before asking me (and didn't find the answer), so here is Xamarin.Android (C#) version:

var alertDialog = new AlertDialog.Builder(this) // this: Activity .SetTitle("Hello!") .SetMessage("Are you sure?") .SetPositiveButton("Ok", (sender, e) => { /* ok things */ }) .SetNegativeButton("Cancel", (sender, e) => { /* cancel things */ }) .Create(); alertDialog.Show(); // you can customize your AlertDialog, like so var tvMessage = alertDialog.FindViewById<TextView>(Android.Resource.Id.Message); tvMessage.TextSize = 13; // ... 

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.