Reputation: 12947
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();
Upvotes: 0
Views: 1013
Reputation: 1746
You can ask the user to enter their phone when the user is running for the first time, then you can save the details in Shared Preferences and for the next time if the user run the app you can use the shared Preferences values to know if they have entered their contact info or not, if they did, let them go through this activity and forward it to the next activity.
Please have a glance link
Upvotes: 0
Reputation: 1007349
I'm trying to make an alert box that appears only when the app is installed
This is not possible. You do not get control at install time.
I need the user to input their phone number before they are allowed to use the app.
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).
I have an alert dialog in my onCreate() method, but if the user taps outside of the alert box, it goes away.
It will also go away if they press the BACK button.
I've tried adding alert.setFinishOnTouchOutside(true) as you can see, but I get an error (Add cast to 'Alert')
setFinishOnTouchOutside()
is a method on Activity
. It is not a method on AlertDialog.Builder
.
What do I need to do to stop the alert from cancelling if the user taps outside of the window?
Call setCanceledOnTouchOutside()
on the AlertDialog
built by the Builder
. This is not available on the Builder
itself.
Of course, it would be better if you did not use an AlertDialog
in 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 original onResume()
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.
Upvotes: 2