Simple message box with no buttons
AlertDialog alertDialog; alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Packing List"); alertDialog.setMessage("Could not find the file."); alertDialog.show();
Message box with buttons
AlertDialog deleteAlert = new AlertDialog.Builder(this).create(); deleteAlert.setTitle("Delete List"); deleteAlert.setMessage("Are you sure you want to delete this list?"); deleteAlert.setButton("Yes", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { //... } }); deleteAlert.setButton2("No", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { //... } }); deleteAlert.show();
Is there any way to do this without an Activity object? (I want to use it for debugging in classes that don't extend Activity)
ReplyDeleteYou can pass the Activity context to your class to replace "this" in code above.
ReplyDeleteyou can use getContext() method out of activity to replace "this" in code above.
ReplyDeleteTake a look here , this is my solution for it with source code:
ReplyDeletehttp://www.erhancetin.com.tr/2013/10/how-to-display-alert-dialog-in-android.html
its showing error in setbutton and setbutton2 line
ReplyDeletecan tell me the solution for it?
Those methods are deprecated. You can use setButton(AlertDialog.BUTTON_POSITIVE, ...) and setButton(AlertDialog.BUTTON_NEGATIVE, ...) instead
DeleteThis comment has been removed by the author.
ReplyDeleteNice
ReplyDelete