From PHP/JavaScript to Android, the simple stuff – Yes/No Confirmation Dialog

Featured Image

My apologies for a bit misleading title, as there is no PHP Confirmation dialog as such. More precisely its a analogy of JavaScript confirmation dialog and Android (Java) alternative for the same thing.

Here is a little code snippet that you can use to get the Yes/No dialog in Android and then execute corresponding action based on the users choice.

//...
 
/**
 * START Yes/No Dialog Example
 */
new AlertDialog.Builder(MagentoActivity.this)
	.setMessage("Are you sure you wish to delete customer?")
	.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			//Execute some method call
			Toast.makeText(MagentoActivity.this, "Delete clicked...", Toast.LENGTH_SHORT).show();
		}
	})
	.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			//Execute some method call
			Toast.makeText(MagentoActivity.this, "Cancel clicked...", Toast.LENGTH_SHORT).show();
		}
	})
	.show();
/**
 * END Yes/No Dialog Example
 */ 
 
//...

And here is how this might look in simple JavaScript.

//...
var isConfirmed = confirm("Confirm delete?")
 
if (isConfirmed) {
	alert("Confirmed...")
}
else {
	alert("Not confirmed...")
}
//...

That’s it, plain and simple. Cheers.


1 comment

  1. I’m a PHP developer that is transitioning into some Android work as well. Just a note of encouragement to keep posting these snippets! They’re really helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.