Android: Show a legal agreement dialog before Program launches

person Jason Huangfolder_openAndroid, Javalocal_offer, , , , access_time January 17, 2011

You can easily show a legal liability agreement on the screen when the user first run your app.

The following code will show a legal notice and a “Agree”, No selection before launching the main app activity.

In the main activity’s OnCreate event, just call the showDialog(0). That would called the override onCreateDialog event defined below.

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        showDialog(0);
    }

  protected Dialog onCreateDialog(int id){
    	// show disclaimer....
        // for example, you can show a dialog box... 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("LEGAL DISCLAIMER: ... ")
               .setCancelable(false)
               .setPositiveButton("Agree", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // and, if the user accept, you can execute something like this:
                       // We need an Editor object to make preference changes.
                       // All objects are from android.context.Context
                       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                       SharedPreferences.Editor editor = settings.edit();
                       editor.putBoolean("accepted", true);
                       // Commit the edits!
                       editor.commit();                    
                   }
               })
               .setNegativeButton("Disagree", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //nm.cancel(R.notification.running); // cancel the NotificationManager (icon)
                        System.exit(0);
                   }
               });
        AlertDialog alert = builder.create();
        return alert;
    }

Comments

  1. Pingback: A very clean way to add a disclaimer/EULA to your Android application « Derek Reynolds – Brain Dump-O-Matic

  2. i used this in my two free android apps. it works great! thanks so much!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>