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
|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:
public void onCreate(Bundle savedInstanceState)
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.
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:
Just do not forget to remove it after testing ;)