Changing "Unfortunately app has stopped message" to some other text - android

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();
}

Related

Android alert dialog good usage

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.

Video call got disconnect due to do not keep activities check in android device

ongoing video call got disconnected when it goes to on pause because of do not keep activities checked under developer option in android device how can a user stay in video instead of that option is checked
You can check "Is the do not keep activities is checked or not" by using below line,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
If it returns one, create a AlertDialog and ask the user to disable it first, if user clicks on positive button then create an intent for the Settings like below,
int value = Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0);
if (value == 1)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("");
// set dialog message
alertDialogBuilder
.setMessage("You need to disable `do not keep activities` option, press YES")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.dismiss();
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
I am entirely not sure if I got you correctly.
But does this: https://developer.android.com/training/scheduling/wakelock.html
answer your question?
It prevents your app from entering onPause, when a voice-chat is ongoing.

Can't show a simple message in an Android App

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.

How to show messages in alertdialog randomly

I am using AlertDialog to show any message and links, I use this code. But I want to show a different message (link) in every action randomly. Is that possible? and if it is, can you give me sample codes for this. thanks.
final AlertDialog d = new AlertDialog.Builder(this)
.setPositiveButton(android.R.string.ok, null)
.setIcon(R.drawable.icon)
.setMessage(Html.fromHtml("Check this link out"))
.create();
d.show();
// Make the textview clickable. Must be called after show()
((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
what i want is: when user opens my application, my alertbox shows a link but i want to use many links and show them randomly, I will use it for like text ads. I mean when user open my app google.com will be shown and another time yahoo.com and another time a different link. Hope i am clear
You can use this:
public static void showAlertDialog(final String title, String message,
final Context context, final boolean redirectToPreviousScreen) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
alertbox.setMessage(message);
alertbox.setTitle(title);
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
try{
alertbox.show();
}catch (Exception b) {
}
}

DialogResult in normal java class and return result

I am working on an android project where I am trying to show a AlertDialog in a separate normal java class and return the result that the user enters. I can display the dialog fine but the problem I am having is it always returns the value before the dialog has had one of the buttons pressed.
Below is the code that calls the function in the standard java class to show the dialog
private void showDiagreeError()
{
Common common = new Common(this);
boolean dialogResult = common.showYesNoDialog();
Toast.makeText(getApplicationContext(), "Result: " + dialogResult, Toast.LENGTH_LONG).show();
}
And below is the code that shows the actual dialogue
public boolean showYesNoDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you do not want to agree to the terms, if you choose not to, you cannot use Boardies Password Manager")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = true;
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialogResult = false;
}
});
AlertDialog alert = builder.create();
alert.show();
return dialogResult;
}
dialogResult is a global variable visible throughout the class and being set to false. As soon as the dialog is shown the toast message is shown showing the result is false, but I was expecting the return statement to block until the user has pressed one of the buttons too set the variable to the correct value.
How can I get this to work.
After many hours hunting through the inner depths of google pages, I found this Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style).
It does exactly the job I was after and tested to make sure there are no ANR errors, which there isn't

Categories

Resources