New smart game. iQBloxing
 
 
 

Adding EULA to Android app

EULA ( End User License Agreement ) is not required to publish your Android app on Market, but it’s a good practice to protect yourself by concluding an agreement with your user. So in this post I will describe simple and quick method of implementing EULA in Android application.

Our EULA implementation is based on class available here. We are using Eclipse 3.3 with ADT plugin installed. So we need to do the following steps to add EULA to Android application:

  1. Download or copy Eula class source code from here.
  2. Add Eula.java file containing Eula class source code to Android application.
  3. Add necessary string resources.
  4. Add text file, containing EULA text into ‘asset’ application folder.
  5. Call Eula.show method in Create event handler of the main Activity.

Now let us study each step in detail. The first step doesn’t demand any explanations, that is why let us go to the second one.

EULA Class Content

Method Description
static boolean show(final Activity activity) Is responsible for a display of “dialogue window” with EULA text and processing of user actions
private static void accept(SharedPreferences preferences) Records a value, which confirms that the user has accepted a license agreement, into application state
private static void refuse(Activity activity) Closes the main Activity
private static CharSequence readEula(Activity activity) Reads EULA text from the file
private static void closeStream(Closeable stream) Closes a thread passed as a parameter, in particular a thread of reading EULA text

Adding EULA Class To Your Project

In order to add EULA class source code into application it is necessary to copy Eula.java file into the folder with source codes of your application (e.g. for BrainTwister the path will be as follows: BrainTwister\src\AndroidDevStudio\Games\BrainTwister). It is also possible to drag Eula.java file in Eclipse into BrainTwister\src\AndroidDevStudio.Games.BrainTwister folder. In any case the result should look as the one presented on the following image:

EULA class in your Android application

EULA class in your Android application

It is also possible to add the class using Eclipse tool “New Java Class” in the following way: add an empty class, then copy Eula class code copied from the site into the created file. In both cases, it may be needed to add a link to the package of the application into which EULA is added.

Adding String Resources

Now it is necessary to add missing resources into the application, in particular, the following string resources:

  1. eula_title it isdialogue windowheader text, which allows a user to get familiarized with EULA text and accept or refuse from it.
  2. “eula_accept” – it is a text, which is displayed on the button implementing “Accept” action.
  3. “eula_refuse” it is a text, which is displayed on the button implementing “Refuse” action.

String resources are added to res\values\strings.xml file and possibly could be as follows:

  1. <string name=“eula_title”>License</string>
  2. <string name=“eula_accept”>Accept</string>
  3. <string name=“eula_refuse”>Refuse</string>

Adding EULA File To Project

Now we should add file with EULA text to your project. Please note that file should not have any extension and must be placed into “assets” folder. First create *.txt file, add it to project and then remove extension. It should look like that:

EULA File In Your Project

EULA File In Your Project

Call Eula.show() Method

And at the last step you should call show() method from Eula class in the very beginning of Create events handler in the main Activity. You should also pass Activity which should be closed if user refuses EULA. Code will look like this:

@Override
public void onCreate(Bundle savedInstanceState)
{
Eula.show(this);

}

And that’s it – we are ready to see the result! Start your application and you will see something like that:

EULA in Android App Example

EULA in Android App Example

If user clicks “Refuse” button then application will be closed. If user accepts EULA, then it will never be shown again.

Testing EULA

Every application must be tested. So we should test EULA few times. We can write indicator to application state which says that user did not accepted EULA yet. The following code should be inserted at the beggining of show() method in Eula class:
preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, false).commit();

Just do not forget to remove it after testing ;)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Related Posts

How To Prepare Android Application For Market, Pseudo 3D Graphics for Android Game. Isometric basics., How to install non-Market app on G1?
 

Comments - 22

  1. Dharmin says:

    Great time saver. Thanks a lot…:)

  2. Rose says:

    Thanks! I used this for my first app.

  3. AA says:

    great work guys. thanks for sharing =)

  4. Yene says:

    Just Want to say thank you. you made my day simple

  5. SA says:

    You did not mention about how to implement the callback: OnEulaAgreedTo.

    The problem with the above is- EULA is displayed while other async. theads continue, which creates problem. Expample, license check and EULA display both start in parallel, while we want one to start after the other one is completed.

  6. Tobi says:

    works great ( and explained great too ;)

  7. Hikmat Khan says:

    Great Work buddy .
    Keeep it up.
    Thanks

  8. Raphael says:

    Now the Eula.java file is licensed under Apache License 2.0. How can you enforce a different EULA with Eula.java when using that file restricts you to the Apache License 2.0 anyway?

  9. padmavathi says:

    For some reason once accepted, it does not show the dialog next time even if this line is added for testing purposes:

    preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, false).commit();

  10. Joseph says:

    I am getting “eula cannot be resolved” in my mainactivity and I have recreated your steps exactly about 10 times. Not sure what I am doing wrong.

  11. Joseph says:

    ok, there is one difference…I dont have public void, I have protected void, does that make a difference?

  12. Lars Vogel says:

    Great! Thanks lot for this example. Adding an EULA is really easy now. Thanks also for the Apache License.

  13. Robin says:

    Slight Improvement – Full Justification of EULA Text using a WebView

    Replace:
    //Default LinearLayout
    // builder.setMessage(readEula(activity));
    // builder.create().show();

    With:
    //WebView layout
    WebView myWebView = new WebView(activity.getApplicationContext());
    String htmlSkeletonText = activity.getResources().getString(R.string.webview_html_skeleton);
    CharSequence eulaText = readEula(activity);
    htmlSkeletonText = htmlSkeletonText.replace((CharSequence)”TEXT_TO_REPLACE”, eulaText);
    myWebView.loadData(htmlSkeletonText, “text/html”, “utf-8″);
    builder.setView(myWebView);
    builder.create().show();

    Add the following string to strings.xml:
    <![CDATA[

    TEXT_TO_REPLACE

    ]]>

  14. Robin says:

    Hmmm, the HTML contents of my CDATA tag were removed. Just add a basic HTML outline, and add a body tag, setting the style attribute to: text-align:justify;color:gray;background-color:black;

  15. Robin says:

    strings.xml

    <string name=”webview_html_skeleton”> <![CDATA[
    <html>
    <head></head>
    <body style="text-align:justify;color:gray;background-color:black;">
    TEXT_TO_REPLACE
    </body>
    </html>
    ]]>
    </string>

  16. Robin says:

    Also, handy for testing:

    /**
    * Reset the acceptance to false
    * For development purposes and testing only.
    * @param activity The Activity used to retrieve preferences.
    */
    public static void resetAcceptance(final Activity activity){
    final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_EULA,
    Activity.MODE_PRIVATE);
    preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, false).commit();

    }

  17. Sujith says:

    hai,
    while i am displaying eula, copyright symbol is not showing. can anyone say how to use the copyright symbol “©” while we read eula from asset?

  18. makos says:

    Hi,

    Great Work and very nice instructions.
    Keep on..

    Thanks
    Makos (from Greece)

  19. [...] in my application which reads the contents form a text file. For that i had done as shown in http://bees4honey.com/blog/tutorial/adding-eula-to-android-app/It really helped me and is a nice tutorial for those who needs help…But i need the text [...]

  20. Felix says:

    I think the link’s broken, but the tool is lokking very useful! Thank you for your work.

  21. Felix says:

    Very useful tool!

    But the links are broken!

  22. I constantly emailed this weblog post page to all my associates, for the
    reason that if like to read it after that my contacts will too.

 
Leave a comment