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:
- Download or copy Eula class source code from here.
- Add Eula.java file containing Eula class source code to Android application.
- Add necessary string resources.
- Add text file, containing EULA text into ‘asset’ application folder.
- 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:
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:
- “eula_title” – it is “dialogue window” header text, which allows a user to get familiarized with EULA text and accept or refuse from it.
- “eula_accept” – it is a text, which is displayed on the button implementing “Accept” action.
- “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:
- <string name=“eula_title”>License</string>
- <string name=“eula_accept”>Accept</string>
- <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:
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:
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 ;)





Great time saver. Thanks a lot…:)
Thanks! I used this for my first app.
great work guys. thanks for sharing =)
Just Want to say thank you. you made my day simple
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.
works great ( and explained great too ;)
Great Work buddy .
Keeep it up.
Thanks
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?
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();
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.
ok, there is one difference…I dont have public void, I have protected void, does that make a difference?
Great! Thanks lot for this example. Adding an EULA is really easy now. Thanks also for the Apache License.
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
]]>
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;
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>
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();
}
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?
Hi,
Great Work and very nice instructions.
Keep on..
Thanks
Makos (from Greece)
[...] 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 [...]
I think the link’s broken, but the tool is lokking very useful! Thank you for your work.