Thursday, January 22, 2009

How to display a license agreement on your android application.

It is usually good practice to present the user with a license agreement the first time it uses an app. You can show the user a preferably short text with the license agreement. A license agreement gives you some legal protection if your application causes a problem to someone. You can use a checkbox to indicate that the user accepted the license agreement and proceed to the application.

We can use the shared preference mechanism to check if the user has agreed to the license agreement during the activity. If the user has previously agreed to the license then we proceed as normal. Otherwise, we show him the EULA. If the user checks the "I accept the license agreement" checkbox and presses OK, then we write to the preferences that the user accepted the license. If the user presses Cancel, we exit the app. You can see this in the following code excerpt

private android.widget.CheckBox checkboxAcceptLicense;
private android.widget.Button buttonOK;
private android.widget.Button buttonCancel;
private android.widget.TextView textviewLicense;
public SharedPreferences.Editor prefsEditor;
public SharedPreferences prefs;
public android.view.View licenseView;
private boolean soundInitialized;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
soundInitialized = false;
prefs = this.getPreferences(android.content.Context.MODE_PRIVATE);
prefsEditor = prefs.edit();
if(!prefs.getBoolean("LicenseAccepted", false))
{

//this.licenseView = this.findViewById(R.layout.licenseagreement);
this.licenseView = View.inflate(this, R.layout.licenseagreement, null);
this.setContentView(licenseView);
this.checkboxAcceptLicense = (android.widget.CheckBox)this.findViewById(R.id.checkLicenseAgreement);
this.textviewLicense = (android.widget.TextView)this.findViewById(R.id.textviewLicenseAgreement);
this.buttonOK= (android.widget.Button)this.findViewById(R.id.buttonOK);
this.buttonCancel = (android.widget.Button)this.findViewById(R.id.buttonCancel);
this.buttonCancel.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
exitNotAcceptedApplication();
}
}
);
this.buttonOK.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
if(checkboxAcceptLicense.isChecked())
{
prefsEditor.putBoolean("LicenseAccepted", true);
prefsEditor.commit();
licenseView.setVisibility(View.INVISIBLE);

initActivity();
}

}
}
);
}
else
{
initActivity();
}



}

the initActivity() method contains the actual code that you would normally execute on the onCreate() method of your Activity. The layout file for the license agreement is the following (licenseagreement.xml):

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

<LinearLayout

android:id="@+id/widget24"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffffff"

android:padding="0px"

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

>


<TextView

android:id="@+id/textviewLicenseAgreement"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="0px"

android:text="Monolith Android Licence Agreement. This software package is provided to you, the end user, by the sofware package creator, Tasos Kleisas, as is. The software package is provided free of charge by the developer. However, the means of delivering the software to your device, (mobile phone, computer or other) may induce a service fee. The developer cannot be held responsible for any damage or loss that you may suffer by the use or installation of this application package to your device. Use at your own risk. The code, sound effects, and music are copyrighted by Tasos Kleisas (C) 2007-2009."

android:textSize="14sp"

android:textColor="#000000"

android:typeface="normal"

android:textStyle="normal"

android:layout_weight="0"

android:layout_gravity="left"

>

TextView>


<CheckBox

android:id="@+id/checkLicenseAgreement"

android:layout_width="fill_parent"

android:layout_height="wrap_content"


android:text="I accept the license agreement"

android:textColor="#000000"

android:textSize="12sp"

android:typeface="normal"

android:textStyle="normal"

android:checked="false"



android:orientation="horizontal"

>

CheckBox>

<TableRow

android:id="@+id/widget32"

android:layout_width="fill_parent"


android:layout_height="wrap_content"

android:orientation="horizontal"

>

<Button

android:id="@+id/buttonOK"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10px"

android:text="OK"

android:textSize="14sp"

android:typeface="normal"

android:textStyle="normal"

android:layout_weight="0"

android:layout_gravity="left"

>

Button>

<Button

android:id="@+id/buttonCancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="10px"

android:text="Cancel"

android:textSize="14sp"

android:typeface="normal"

android:textStyle="normal"

android:layout_weight="0"

android:layout_gravity="left"

>


Button>

TableRow>

LinearLayout>

Unfortunately, as you can see the server messed up the xml layout a bit. But you can find the full code at http://code.google.com/p/monolithandroid/source/browse/

No comments:

Search the web