I started developing Android apps today using Visual Studio 2015 and I'd like to know why I can't show an AlertDialog, because it throws me an exception about Android.Views.WindowManagerBadTokenException and I don't see where I'm mistaken..
private void Button_Click(object sender, EventArgs e)
{
((Button)sender).Text = string.Format("{0} clicks!", count++);
try
{
AlertDialog.Builder builder = new AlertDialog.Builder(this.ApplicationContext);
builder.SetTitle("This is a title..");
builder.SetMessage("This is a message..");
AlertDialog ad = builder.Create();
ad.Show();
}
catch (Exception ex)
{ ((Button)sender).Text = ex.Message; }
}
First of all, if you just started developing for Android, USE ANDROID STUDIOS unless you have a really really good reason to develop in Visual Studios. Android Studios is the best IDE I've ever used and I highly recommend it.
But, if you would like to stick with Visual Studios, try this code:
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
Pulled from this question. Maybe it's because your .Create() and .Show() should be all lower case instead of camel case.
Related
I am a fresh in Appium. I use this code to show permission alert and I want to test it.
new AlertDialog.Builder(activity).setTitle("ALERT")
.setMessage(reason)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
ActivityCompat.requestPermissions(activity,new String[]{permissionType},requestCode);
}
}).create().show();
I use appium desktop and python appium-client ,how to wait until this alert dialog show and then catch this NegativeButton and click it?
driver.switch_to_alert().accept() can work.
it is Deprecated use driver.switch_to.alert.
driver.switch_to.alert.accept() can work.
I have a if then statement. If the if then statement is true, I want an Android system to fire a basic alert with one button that says ok.
I have done plenty of research, but nothing works.
this is how you create Alert in android
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Write your message here.");
builder.setNeutralButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show()
I am trying to display a native Alertdialog as soon as the app loads, but an ad is showing before that. I want to make sure this Alertdialog is showing as the first thing when the app loads, and when the Alertdialog is dismissed, the ads and everything else should load next.
Any ideas on how to do that inside Android application?
Here's what I have in code:
public void checkForCookiePolicy()
{
final SharedPreferences settings =
getSharedPreferences("localPreferences", MODE_PRIVATE);
if (settings.getBoolean("isFirstRun", true)) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Cookie Policy");
builder1.setMessage("We use device identifiers to personalise content and ads, to provide social media features and to analyse our traffic. We also share such identifiers and other information from your device with our social media, advertising and analytics partners.");
builder1.setCancelable(true);
builder1.setPositiveButton("See Details",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(AppActivity.this, CookiePolicy.class);
startActivity(intent);
settings.edit().putBoolean("isFirstRun", false).commit();
}
});
builder1.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
settings.edit().putBoolean("isFirstRun", false).commit();
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
}
Here's the ad code. It's above the alert code:
revmob = RevMob.start(this);
revmob.createFullscreen(this, null);
I couldn't find anything usefull on the internet about my problem. So my question is how do you do a good usage of Android's alert dialogs. Here is an example of code creating and showing an alert dialog just with the title "error", the text "you can't do that" and a "Ok" button :
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setMessage("You can't do that");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton(
getResources().getString("ok"),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialogError = alertDialogBuilder.create();
alertDialogError.show();
But now, if I have many of this alert dialogs in my application, what should I do ?
Should I set the alertDialogBuilder as an attribute so each time I want to display an error message I can call his function setMessage() and then create() and then show() ?
Should I keep an already configured alertDialog for every single error message I have so I can just call theRightAlertDialog.show() to display my message ?
Something else ?
What's the good usage/cleanest way to do this for you ?
You could do this one of two ways. The first is to create a static method, which you can place in a final utility class:
public final class AlertUtil {
public static void showErrorDialog(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Error");
builder.setMessage(message);
builder.setCancelable(true);
builder.setPositiveButton(
getResources().getString("ok"),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
}
}
Or you can use a DialogFragment which you can create with:
getSupportFragmentManager().beginTransaction().add(ErrorDialogFragment.newInstance(message), "tag").commit()`
I will say though, as a side note; if you are looking to change more than just a few fields for each of the dialogs (i.e. adding more parameters to the showErrorDialog method), then you probably should just stick to the Builder pattern. Considering that is what the Builder pattern is meant for.
Should I set the alertDialogBuilder as an attribute so each time I want to display an error message I can call his function setMessage() and then create() and then show() ?
If the title and the button functionality are the same for all of your alerts, than this would be the best strategy. Create a variable for the alertDialogBuilder, or even just the alertDialog itself, then change the message and show it each time.
Alternatively, you could create a method that builds the dialog, and takes in a string for the message text.
I have a app which requests for a lot of JSON arrays, if there is no internet signal , JSON is a null pointer reference due to which my app crashes. Instead of writing a function to check if the JSONArray is null, can I change the text of Unfortunately app stopped working to Cannot connect to internet?
Is this possible?
You can't change the text of the NullPointerException unless you develop your own Exception class and a SDK.
But for now this is what you can do.
try
{
// try to parse your json here
}
catch(NullPointerException npe)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Cannot connect to internet")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish(); // Close your app
}
});
AlertDialog alert = builder.create();
alert.show();
}