Reputation: 147
Yesterday I had programmed a little AlertDialog with my Android-Phone via AIDE programmed. On the Internet I've found the source code
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
I have tested this in AIDE and it's working, then I've tested it within AndroidStudio and it didn't work. Why does it work in AIDE and not in Android studio?
Upvotes: 0
Views: 2223
Reputation: 32
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this);
Edit first line as above since you need to set builder before creating alertDialog
Complete code as follows:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialogMain = alertDialog.create();
// Showing Alert Message
alertDialogMain.show();
Upvotes: 1