I’m trying to make an alert box that appears only when the app is installed; I need the user to input their phone number before they are allowed to use the app. I have an alert dialog in my onCreate() method, but if the user taps outside of the alert box, it goes away. I’ve tried adding alert.setFinishOnTouchOutside(true) as you can see, but I get an error (Add cast to ‘Alert’). What do I need to do to stop the alert from cancelling if the user taps outside of the window? Thanks
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Please Enter Your Phone Number");
alert.setMessage("You must enter your phone number in order to use this application");
alert.setFinishOnTouchOutside(true);
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
}
});
alert.show();
This is not possible. You do not get control at install time.
Then ask them for the value on the first run of your app, or if your desired data is not there (e.g., user cleared your app’s data).
It will also go away if they press the BACK button.
setFinishOnTouchOutside()is a method onActivity. It is not a method onAlertDialog.Builder.Call
setCanceledOnTouchOutside()on theAlertDialogbuilt by theBuilder. This is not available on theBuilderitself.Of course, it would be better if you did not use an
AlertDialogin the first place, as your proposed UX is user-hostile.The user downloads and installs your app, opens it, and the first thing they encounter is a dialog box that they may not understand. And, your goal is that they cannot get to anything in your app that would actually explain:
Instead of the dialog, if you detect, in
onResume(), that you do not have the phone number, start up an activity to allow them to provide the number. Make use of the screen to explain a bit about why you need the number, and allow them to get to online help to explain matters further. Even if they BACK out of that activity, your originalonResume()will fire again, so they cannot proceed further. An even better UX would be to allow them into the app, but disable things that require the phone number for use, just as you can open up Microsoft Word without being forced at gunpoint to load an existing Word doc.