Reputation: 3
First of all please bear with me because I'm new in Android.
I would like to write an application that can pop up a dialog box if a condition is met
example :
public class TestMax {
public void main(String[] args) {
int i = 5;
int j = 5;
int sum = i + j;
if (sum == 10) {
// alert dialog box will appear and show the message - "Answer is 10"
}
}
Appreciate your help. Thank you.
Upvotes: 0
Views: 4139
Reputation: 6326
do this. if you want to show only a alert dialog.
public void main(String[] args)
{
int i = 5;
int j = 5;
int sum = i + j;
if (sum == 10) {
new AlertDialog.Builder(yourclass.this)
.setTitle("Your answer is")
.setMessage(i)
.setNeutralButton("ok", null)
.setIcon(android.R.drawable.stat_sys_warning).show();
}
}
Upvotes: 0
Reputation: 4425
public void main(String[] args) {
int i = 5;
int j = 5;
int sum = i + j;
if (sum == 10) {
showAlertDialog(Activityname.this, "Internet Connection",
"You have internet connection", true);
// alert dialog box will appear and show the message - "Answer is 10"
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
Upvotes: 0
Reputation: 990
Please try this code....
Class to Pop Up the Dialog
if (condition) {
showAlertDialog(Activityname.this, "Internet Connection",
"You have internet connection", true);
} else {
showAlertDialog(Activityname.this, "No Internet Connection",
"You don't have internet connection.", false);
}
methods declaration for showdailog
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}
Upvotes: 0
Reputation: 780
Add this when your condition is met:
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage("Message");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here. }
});
// Set the Icon for the Dialog
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
Refer http://developer.android.com/reference/android/app/AlertDialog.html. http://developer.android.com/guide/topics/ui/dialogs.html.
Upvotes: 1