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();
Please login or Register to submit your answer

Log In

Not the answer you are looking for? Browse other questions tagged or ask your own question.