tags:

views:

303

answers:

2

Hi,

A lot of Android apps now display a modal dialog with the changelog (text) of the application (what's new in the latest version) the first time after it is installed or updated.

Does any one have example code on:

  1. How to display a nice modal dialog suitable for scrollable text
  2. How to make sure the dialog is only shown once

For 2. I assume SharedPreferences can be used for defining a preference "hasChangeLogBeenShownForVersionX" that is updated after the dialog has been shown. (though there might be a better way

Regards

+2  A: 

I think you're probably on the right track. For #1, I have a class that inherits off of dialog, and its layout simply has a ScrollView containing a TextView, and then a "dismiss" button after that.

Regarding #2, what I've done in mine is to store a setting for LastVersionNotesSeen that holds the ... well, the version number for the notes last displayed. :) If that number is less than the current version, I display the latest version notes and update the value to the current version.

Blumer
A: 

Ended up with the following code Part 1. Checking if the changelog should be viewed

        //evaluate if we will show changelog
    try {
        //current version
        PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        int versionCode = packageInfo.versionCode; 

        //version where changelog has been viewed
        SharedPreferences settings = getSharedPreferences(SAPNotePreferences.PREFS_NAME, 0);
        int viewedChangelogVersion = settings.getInt(SAPNotePreferences.KEY_CHANGELOG_VERSION_VIEWED, 0);

        if(viewedChangelogVersion<versionCode) {
            Editor editor=settings.edit();
            editor.putInt(SAPNotePreferences.KEY_CHANGELOG_VERSION_VIEWED, versionCode);
            editor.commit();
            displayChangeLog();
        }
    } catch (NameNotFoundException e) {
        Log.w("Unable to get version code. Will not show changelog", e);
    }

Part 2 displaying the changelog dialog

        //load some kind of a view
    LayoutInflater li = LayoutInflater.from(this);
    View view = li.inflate(R.layout.changelog_view, null);

    new AlertDialog.Builder(this)
    .setTitle("Changelog")
    .setIcon(android.R.drawable.ic_menu_info_details)
    .setView(view)
    .setNegativeButton("Close", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          //
      }
    }).show();

Part 3 the layout with the changelog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <ScrollView android:id="@+id/aboutscrollview" 
            android:orientation="vertical"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                    <TextView android:text="Thanks for installing ..."
                            android:layout_width="fill_parent" 
                            android:layout_height="wrap_content"
                            android:paddingLeft="15px"              
                            android:layout_gravity="center_vertical"
                            android:textColor="#ffffff" />  
                    <TextView android:text="Changes: "
                            android:layout_width="fill_parent" 
                            android:layout_height="wrap_content"            
                            android:paddingTop="15px"
                            android:paddingLeft="15px"
                            android:textStyle="bold"
                            android:textColor="#ffffff" />
                    <TextView android:text="v2.0:changes..."
                            android:layout_width="fill_parent" 
                            android:layout_height="wrap_content"
                            android:paddingLeft="15px"      
                            android:paddingBottom="10px"            
                            android:textColor="#ffffff" />
                    </LinearLayout>
            </ScrollView>                   
</LinearLayout>
dparnas