show a license agreement in android application - android

We have to show a license agreement dialog when user use the application at the first time, now I have two questions about this:
1 Where to put this dialog?
Add another activity or put the dialog just at the MainActivity which is the launch acitivty?
2 How to close the app if user hit "Reject"
Once user hit the "Reject" button which means that he/she does not agree our license, then we have to exit the application completely. How to make it?
According to the answer of "Ahmad", I will decide to open a dialog or not at the beginning of the activity(the onCreate method):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
this.setupLicense();
this.setupViews();
this.initSomeJob();
}
private void setupLicense() {
SharedPreferences setting = getSharedPreferences(IConstant.Map_License, 0);
boolean mapLicenseAccept = setting.getBoolean(IConstant.Map_License, false);
if (!mapLicenseAccept) {
//user does not accept the license yet, we will open the dialog
showDialog(Dialog_Map_License);
}
}
#Override
protected Dialog onCreateDialog(int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
switch (id) {
case Dialog_Map_License:
builder.setIconAttribute(android.R.attr.alertDialogIcon)
.setTitle(R.string.map_license_title)
.setMessage(R.string.map_license_content)
.setPositiveButton(R.string.map_license_accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//License accepted, persisted it.
SharedPreferences settings = getSharedPreferences(IConstant.Map_License, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IConstant.Map_License, true);
editor.commit();
}
})
.setNegativeButton(R.string.map_license_reject, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//do nothing and exit
Process.killProcess(Process.myPid());
System.exit(0);
}
});
Dialog target = builder.create();
target.setCanceledOnTouchOutside(false);
return target;
}
return null;
}
But now I have meet two problem:
1 Event I choose the "Accept" button, once I open my app the second time, the dialog will show.
It seems that the following code does not work:
editor.putBoolean(IConstant.Map_License, true);
editor.commit();
2 When I show the dialog, the code:
this.setupViews();
this.initSomeJob();
will still run , they are not blocked which is out of expected, since nothing should be done before user hit the "Accept" button.
Any idea to fix it?

onCreateDialog has been deprecated. Use dialog fragment instead. The advantage will be that the code for displaying dialog will be moved from activity and you can then display dialog from any activity. Also move
SharedPreferences setting = getSharedPreferences(IConstant.Map_License, 0);
boolean mapLicenseAccept = setting.getBoolean(IConstant.Map_License, false);
to a utility method like isLicenceAccepted and similarly for storing the data
SharedPreferences settings = getSharedPreferences(IConstant.Map_License, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(IConstant.Map_License, true);
editor.commit();
to method like acceptLicence in utility.
You can find how to make communication between dialog Fragment and your activity here. In your interface instead of onArticleSelected you will have to implement two methods onLicence accepted and onLicenceRejected. Implement the interface in you activity override these two methods and take appropriate action.

Where to put this dialog?
Right at the beginning, when the user opens the app for the first time. Keep track of that by saving it in your shared preferences, if this dialog has been already shown or not. You don't have to create a separate activity for this. You could, but most apps I've seen out there don't.
How to close the app if user hit "Reject"
Just finish the Activity and also save that in your shared preferences as well. So every time the user opens your app you check weather the boolean value for "hasUserAcceptedOurAgreement" is true or not and proceed depending on that.
I'm only answering from a technical standpoint on how this could be done reliably. I'm not a lawyer, but as far as I know, it's perfectly valid to just submit your license agreement to the play store, so it's available from the applications application page (Why else would there be this option?).

Related

how do i change the landing page of the app after the user has signed in

i have created android app that require users to enter their phone numbers when they first use the app, now i am storing that info using SQL lite.the problem is every time they open the app it requires their phone number, and I want the app to just automatically login without asking for the phone number again, kind of like whats app.
Its not about landing page change. The activity which has a category "LAUNCHER" in manifest file always opens first. In that Activity .java file , You can make a check , whether the value for user is available in sqlite or not. if available perform intent to next page .. Check this link too ......Android check user logged in before, else start login activity
Either set a splash activity or another blank activity as your initial activity.
Then store a boolean in your app's shared preference to identify whether the application
is loading first time or not. Based on that boolean value, move to phone number entry screen or
your desired screen.
In your "login" activity you should look for the number in the db, if it is there, you open a new activity, if not, you ask the user. You can use setVisibility(int) in your "ask" views to not show them while looking in the db, and then, if you don't find the number, you show them.
You have to use sharedPrefrences where you can store whether user has stored his number.
When user open the app for the first time and enter his number, then store value in sharedprefrences.
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("prefrence", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("value", "selected"); editor.commit();
When user open the app, Splash screen will check the value in sharedprefrences. If user already had put his number then he will be re-directed to home screen rather than phone number screen.
Splash Screen:
Context mContext;
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mContext = SplashScreen.this;
new Handler().postDelayed(new Runnable() {
/*Showing splash screen with a timer. This will be useful when you
want to show case your app logo / company*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String value = (shared.getString("value", ""));
if(value!=null && !value.equals("")){
/*Re-Direct to Home Screen after Login*/
Intent intent = new Intent(mContext,MainActivity.class);
startActivity(intent);
}
else{
Intent intent = new Intent(mContext, LoginActivity.class);
startActivity(intent);
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}

How exactly to act on a confirmation dialog?

New to Android... I understand Dialogs are asynchronous. But I really can't get my head around the flow for confirming an action. Can someone please explain the flow?
I want to save a file on the sdcard. The Activity prompts the used for the filename. Then it checks to see if the file exists. If it exists, it needs to prompt the user to confirm if they want to overwrite it. Then it proceeds to erase and write the file.
I know you can't hold execution waiting for the response. How then would this common flow work in Android?
Thanks
I am not 100% it is what you are looking for, but here is a link to the Android documentation explaining how we should display Confirmation and Acknowledgement popups using the "Android standard way":
http://developer.android.com/design/patterns/confirming-acknowledging.html
I do not know the exact flow, I suppose it would depend on how the application was written. I would check for the file if it existed call the dialog windows then if the Ok/Yes/Confirm is pressed overwrite the file.
Dialogs | Android Developers - Has an excellent code example
public class FireMissilesDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES! AKA Overwrite your file.
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog AKA do nothing
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
I know it's slightly silly example but basically check for the file (if exist) > Call Dialog (if yes)> Overwrite.

Displaying alert dialog only after a specific activity page

I have a login page.When the user logs in correctly,I want to show an alert dialog saying that your login details have been verified.Click continue to proceed.Now I want this alert dialog to be shown on the next activity page after login.
Here is the alert dialog code-:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean dialogShown = settings.getBoolean("dialogShown", false);
if (!dialogShown) {
AlertDialog.Builder builder = new Builder(Myperwallet.this);
builder.setTitle("Fast Cashier!");
builder.setMessage("Logging In");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
//continue activity here....
}
});
builder.create().show();
// AlertDialog code here
}
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dialogShown", true);
editor.commit();
I tried this but now alert dialog won't display after I have logged out.I need to know how can I set the flag to true when I have pressed the logout button.Here is my logout button functionality-:
logout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Myperwallet.this).create();
alertDialog.setTitle("FastCashier:");
alertDialog.setMessage("Are you sure you want to exit?");
alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent logout = new Intent(Myperwallet.this,Login.class);
startActivity(logout);
}
});
alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
You need to add some implementation to your current code.
You can keep a flag in sharedPrefs and initially it would be true. After showing it for first time make it false.
This is what you must have implemented right now.
So to show it again after logout reset the flag to true when user has logged out. So again when u reach to the activity the dialog would be shown.
actually could not get Under stood more issue, but you want show your AlertDialog in some Optionnale Requirement such as when Login then it show and in next page is Required but due to SharedPreferences displayed only once at the start of the application. When I logged out and then logged in I did not get any alert dialog
SharedPreferences what do do is store your specific Sting with key and you need to match with it. after loing suceessufully you need to Update SharedPreferences with some new String and need to match with it.
also You can use File to check isExit() or not and write require Sting it and Read it and match with you require condition to show alert dialog. this may Helps you.
keep track of the activity flow ,i.e when u start your application it is doing what so ever is in onCreate method ,for ur problem set flag in the on create method ,and when u switch from any other activity to this one implement the method on Restart() and clear ur previous data ....
like this...
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
//ur code
}
hope it will be helpful,

One-time AlertDialog showing on every run

I am trying to have a disclaimer pop up when the app is first run, and after each update. I did a bunch of googling and reviewed some questions here as well, and this is what my code looks like:
SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
String lver = pref.getString("Version", "");
String ver = this.getString(R.string.version);
if(ver != lver)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Disclaimer")
.setMessage(this.getString(R.string.disclaimer))
.setCancelable(false)
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
accepted = true;
dialog.cancel();
}
})
.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainMenu.this.finish();
}
});
AlertDialog disc = builder.create();
disc.show();
if(accepted == true)
{
edit.putString("Version", this.getString(R.string.version));
edit.commit();
}
}
This code actually worked at first, but when I changed my apps starting activity, and moved this code from the original starting activity to the new one, it no longer works. The disclaimer appears on every run.
I want the popup only to show on first run and after updates. What do I need to do to achieve this? I am very frustrated and confused by the fact that it was working, then wasnt.
Comparing Strings with .equals() is the correct way (see: How do I compare strings in Java? for a good explanation) , although because I'm not sure how the android internals work and you said it worked before, that isn't your problem. Your problem is that your check
if (accepted == true) {/** code */}
isn't run on the on click listener. Because it isn't, that thread (I'm assuming it spawns a new thread to show the dialog) keeps running.
I'm also assuming before you moved this code, you had declared a
boolean accepted = true; //or initialized it to true somewhere
But when you moved it you didn't reinitialize it. Now, because the default value of a primitive is false, in your new code it gets to the check before you press a dialog button, and never commit the new version.
My advice would be put what's in the
accepted == true
block simply into your listener for the positive button click.

how to set different message on application startup in android?

I am creating one application for student. I need to set the different message whenever
user open application.I don't understand how to do this or which method use for this.
I search lot of articles but i didn't found anything.
So please provide me some reference or code.
You can store your messages using any storage like sqlite,file or sharedprefrences and retrive message randomly on app opning..
Save your messages in a persistent storage. In android, you could use SharedPreference http://developer.android.com/reference/android/content/SharedPreferences.html, or a Sqlite databse http://developer.android.com/reference/android/database/sqlite/package-summary.html depending on your specific need. Store the messages in either of them and read back a different message each time.
Store some msgs in a SharedPreference at some point in your Activity:
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Editor ed =pref.edit();
ed.putString("0","msg0");
ed.putString("1","msg1");
ed.putString("2","msg2");
ed.putString("3","msg3");
ed.commit();
Then in onCreate(), retrieve a random sg and diplay to the user:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
Random r = new Random();
String msg = pref.getString(r.nextInt(4)+"", "none");
Toast.makeText(this, msg, Toast.LENGTH_LONG ).show();
}
You should read the about the fundamentals of android, this would guide you on how to do this. You're not going to spout garbage at the user, there will be a pattern. Once you find the pattern then you turn that logic into java.
Use AlertDialog
Code example below taken from:
http://www.mkyong.com/android/android-alert-dialog-example/
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder.setMessage("Click yes to exit!").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();

Categories

Resources