Android: A Simple Eula for your Android Apps

There comes a time when you need to show a EULA to your users of your Android apps. Doing this is pretty easy if you decide to ALWAYS show it when the user uses your app. However, that’s beyond annoying. The best case scenario for displaying a EULA to a user would go like this…

  • User installs your app
  • User opens your app
  • User is presented with a EULA
  • Users Accepts EULA?
    • No –> Close app
    • Yes –> Continue
  • User uses app, closes it.
  • User re-opens app for a subsequent use. EULA IS NOT displayed.
  • An update for the app is available.
  • User installs App Update
  • User opens the app.
  • User is presented with a new updated EULA again.

As you can see, this can become a royal PITA for managing. So how do you do you it?

Very simple. Use the version code and the shared preferences.

How To Implement the SimpleEula.java File

I’ve created a file by the name of SimpleEula.java that will display a Dialog with the Eula and two buttons (Ok, Cancel … You can rename them anything you want). Upon initialization of the first activity of the app, SimpleEula will grab the version code of the app, and then use that version code to create a unique key for the version. Such as “eula_1″ or “eula_2″, etc. SimpleEula uses this key to check shared preferences if this key has been set. If it HAS then it means that the user has accepted the EULA and nothing happens, the user is greeted with the first activity of the app. If the key has NOT been seen before, SimpleEula will display a Dialog with a Eula in it (along with app updates – which both from from the string resources of the app)  and the user can accept or decline the Eula (Ok or Cancel buttons). If the user selects OK, the key is set in shared preferences so upon susequent app starts, the Eula WILL not be displayed.

Therefore, the only time the user will see a new Eula display to them is when the version code changes. Since new features are introduced in new versions, this is the perfect time to show the Eula again.

Whats the code look like?

To use SimpleEula, add the SimpleEula.java file (download below from the sample project on GitHub) to your app. Add the required string resources:

  • eula
  • updates

Then in the first activity of your app, add a call to SimpleEula’s show method:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new SimpleEula(this).show();
    }
}

This will create Eula that looks like this:

The user clicks “OK” and the eula key will be saved in shared preferences and the user will not be bugged about the Eula again until an app update is made.

Here’s the full code for SimpleEula.java. You can also download the sample project on GitHub via the link at the end of this post.

public class SimpleEula {

	private String EULA_PREFIX = "eula_";
	private Activity mActivity;

	public SimpleEula(Activity context) {
		mActivity = context;
	}

	private PackageInfo getPackageInfo() {
        PackageInfo pi = null;
        try {
             pi = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi;
    }

     public void show() {
        PackageInfo versionInfo = getPackageInfo();

        // the eulaKey changes every time you increment the version number in the AndroidManifest.xml
		final String eulaKey = EULA_PREFIX + versionInfo.versionCode;
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
        boolean hasBeenShown = prefs.getBoolean(eulaKey, false);
        if(hasBeenShown == false){

        	// Show the Eula
            String title = mActivity.getString(R.string.app_name) + " v" + versionInfo.versionName;

            //Includes the updates as well so users know what changed.
            String message = mActivity.getString(R.string.updates) + "\n\n" + mActivity.getString(R.string.eula);

            AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                    .setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            // Mark this version as read.
                            SharedPreferences.Editor editor = prefs.edit();
                            editor.putBoolean(eulaKey, true);
                            editor.commit();
                            dialogInterface.dismiss();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new Dialog.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// Close the activity as they have declined the EULA
							mActivity.finish();
						}

                    });
            builder.create().show();
        }
    }

}

Download Sample Project

Android Dev Digest

Get the best Android Developer posts delivered weekly to your inbox.

Don't worry, I wont spam (I hate that stuff too).