Recipe Adding a EULA

As discussed in Chapter 1,"Overview of Android," it is often useful to have an End User License Agreement (EULA) display when a user first installs and runs an app. If the user does not accept it, the downloaded application does not run. After a user does accept it, the EULA is never shown again.

This EULA functionality is already implemented and available publicly under the Apache License as the Eula class shown in Listing 9.7. It uses SharedPreferences with the boolean preference_eula_accepted to determine whether the EULA was previously accepted or not accepted.

Listing 9.7 src/com/cookbook/eula_example/

* Copyright (C) 2008 The Android Open Source Project

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at


* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,


* See the License for the specific language governing permissions and

* limitations under the License. */

package com.cookbook.eula_example;

import; import; import android.content.DialogInterface; import android.content.SharedPreferences;

import; import; import;

import; /**

* Displays an EULA ("End User License Agreement") that the user has to accept before

* using the application. */

class Eula {

private static final String ASSET_EULA = "EULA";

private static final String PREFERENCE_EULA_ACCEPTED = "eula.accepted";

private static final String PREFERENCES_EULA = "eula";

* callback to let the activity know when the user accepts the EULA. */

static interface OnEulaAgreedTo { void onEulaAgreedTo();

* Displays the EULA if necessary. */

static boolean show(final Activity activity) {

final SharedPreferences preferences =



// preferences.edit()

// .putBoolean(PREFERENCE_EULA_ACCEPTED, false).commit();

if (!preferences.getBoolean(PREFERENCE_EULA_ACCEPTED, false)) { final AlertDialog.Builder builder =

new AlertDialog.Builder(activity); builder.setTitle(R.string.eula_title); builder.setCancelable(true);

builder.setPositiveButton(R.string.eula_accept, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { accept(preferences);

if (activity instanceof OnEulaAgreedTo) {

((OnEulaAgreedTo) activity).onEulaAgreedTo();

builder.setNegativeButton(R.string.eula_refuse, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { refuse(activity);


new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { refuse(activity);



return false;

return true;

private static void accept(SharedPreferences preferences) { preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit();

private static void refuse(Activity activity) { activity.finish();

private static CharSequence readEula(Activity activity) { BufferedReader in = null; try {

in = new BufferedReader(new


String line;

StringBuilder buffer = new StringBuilder(); while ((line = in.readLine()) != null)

buffer.append(line).append('\n'); return buffer; } catch (IOException e) {


* Closes the specified stream. */

private static void closeStream(Closeable stream) { if (stream != null) { try {

stream.close(); } catch (IOException e) { // Ignore

The Eula class needs to be customized as follows:

1. The actual text of the EULA needs to be put in a text file called EULA (as specified by the asset_eula variable in Listing 9.7) and placed in the assets/ directory of the Android project.This is loaded by the readEula() method of the Eula class.

2. There are few strings that need to be specified for the Acceptance dialog box.These can be collected in the string's resource file. An example wording is shown in Listing 9.8.

Listing 9.8 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="hello">Welcome to MyApp</string> <string name="app_name">MyApp</string> <string name="eula_title">License Agreement</string> <string name="eula_accept">Accept</string> <string name="eula_refuse">Don\'t Accept</string> </resources>

Then, any application can automatically have the EULA functionality by simply putting the following line in the onCreate() method of the main activity of the application:;

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


  • brandon
    How to start new activity after eula android?
    6 years ago

Post a comment