How to Add End User License Agreement to Android App

Save

An end user license agreement, commonly abbreviated EULA, is a contract between the developer and purchaser of a program, such as an Android application. It is used to establish the purchaser's right to use the application, but can also be used to give the developer various rights. Adding an end user license agreement to your Android app is relatively easy. When the user launches the app for the first time, the EULA is displayed and he has two options: accept or decline. He must accept the agreement to start the app.

  • Open your application's "/android/src" folder and create a new file. Name it "Eula.java" and open it in a text editor, such as WordPad.

  • Insert the following code in the Eula.java file:

    //package com.google.android.divideandconquer;
    package org.warmux;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.SharedPreferences;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.Closeable;

    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";
    static interface OnEulaAgreedTo {
    void onEulaAgreedTo();
    }
    static boolean show(final Activity activity) {
    final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_EULA,
    Activity.MODE_PRIVATE);
    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);
    }
    });
    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    public void onCancel(DialogInterface dialog) {
    refuse(activity);
    }
    });
    builder.setMessage(readEula(activity));
    builder.create().show();
    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 InputStreamReader(activity.getAssets().open(ASSET_EULA)));
    String line;
    StringBuilder buffer = new StringBuilder();
    while ((line = in.readLine()) != null) buffer.append(line).append('\n');
    return buffer;
    } catch (IOException e) {
    return "";
    } finally {
    closeStream(in);
    }
    }
    private static void closeStream(Closeable stream) {
    if (stream != null) {
    try {
    stream.close();
    } catch (IOException e) {
    // Ignore
    }
    }
    }

    }

  • Save and close the file. This Java code displays the Eula text and draws two buttons: Accept and Decline. If the user clicks Accept, the application starts. If he clicks Decline, the app closes.

  • Open your application's Assets folder and create a new text file. Name it "EULA" and do not include any extension.

  • Open the EULA file in any text editor and insert your end user license agreement text.

  • Save and close the EULA file.

  • Open your application's MainActivity.java file in any text editor.

  • Locate the "protected void onCreate(Bundle savedInstanceState)" function and insert this code between its "{" and "}" brackets:

    Eula.show(this);

  • Save and close the MainActivity.java file.

  • Open the "res/values" folder and open the strings.xml file in any text editor.

  • Add this code to the end of the file, before the "</resources>" line:

    <string name="eula_title">License</string>
    <string name="eula_accept">Accept</string>
    <string name="eula_refuse">Decline</string>

  • Save and close the strings.xml file.

  • Build the Android app and test it. The EULA is displayed the first time you launch it. If you select "Accept," the app starts. If you select "Decline," the app closes.

References

Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!