Connect with us

Display an alert dialog on Android

Display an alert dialog on Android

To create a dialog box first you need to check android version like below:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
    //lollipop and above versions
} else{
    //before lollipop
}

Than need to call builder object and set its title with message:

AlertDialog.Builder anddiabuil;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
	anddiabuil = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
	anddiabuil = new AlertDialog.Builder(context);
}
anddiabuil.setTitle("Add Entry").setMessage("Are you sure you want to add entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
	public void onClick(DialogInterface dialog, int which) { 
		// continue delete
	}
 }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
	public void onClick(DialogInterface dialog, int which) { 
		// do not do anything
	}
 }).setIcon(android.R.drawable.ic_dialog_alert).show();

Your Answer

Please login or Register to submit your answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.

Log In

Subscribe to our newsletter

Enter your email address to subscribe to blog and receive notifications of new articles by email.
100% Privacy. No spam guaranteed