logo logo

How to Show “What’s New” Dialog in Android App

Home » Information Technology » Programming » Android » How to Show “What’s New” Dialog in Android App




As Android application developer  sometimes when we release a new version of our application, we want to provide information to user about what changes have been made or what features have been added into the new version. The best way is to display a ‘What’s Newdialog that shows information about new changes and features when the application starts after updated. This dialog should only be displayed once  when application starts. If user want to open the dialog manually, we can provide it  via a menu.

To display a What’s New dialog, we can compare  current application version number with the previous version number. If the current version number is greater then previous, that means the application is newer or recently updated then we should display the dialog and save the current version number .

Code implementation in Android:

public class MainActivity extends Activity {
	private static final String PRIVATE_PREF = "myapp";
	private static final String VERSION_KEY = "version_number";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        init();
    }

    private void init() {
    	SharedPreferences sharedPref  	= getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE);
    	int currentVersionNumber		= 0;

		int savedVersionNumber			= sharedPref.getInt(VERSION_KEY, 0);

		try {
   	 		PackageInfo pi 			= getPackageManager().getPackageInfo(getPackageName(), 0);
    	 	currentVersionNumber	= pi.versionCode;
   	 	} catch (Exception e) {}

   	 	if (currentVersionNumber > savedVersionNumber) {
   	 		showWhatsNewDialog();

   	 		Editor editor	= sharedPref.edit();

   	 		editor.putInt(VERSION_KEY, currentVersionNumber);
   	 		editor.commit();
   	 	}
	}

    private void showWhatsNewDialog() {
    	LayoutInflater inflater = LayoutInflater.from(this);

        View view				= inflater.inflate(R.layout.dialog_whatsnew, null);

  	  	Builder builder			= new AlertDialog.Builder(this);

	  	builder.setView(view).setTitle("Whats New")
	  	.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	  		@Override
	  		public void onClick(DialogInterface dialog, int which) {
	  			dialog.dismiss();
	  		}
	    });

	  	builder.create().show();
    }
}

Look at init() method (line 14):

  • currentVersionNumber is the current application version number which is obtained from PackageInfo (line 21,22). This version number is set in AndroidManifest.xml on android:versionCode attribute.
  • try {
       	 		PackageInfo pi 	= getPackageManager().getPackageInfo(getPackageName(), 0);
        	 	currentVersionNumber = pi.versionCode;
       	 	} catch (Exception e) {}
    
  • savedVersionNumber is used to save latest version number and saved in SharedPreferences with default value is 0 (line 18).
  • int savedVersionNumber	= sharedPref.getInt(VERSION_KEY, 0);
    
  • When application starts for the first time, it compares currentVersionNumber with savedVersionNumber. Because there was no value set for savedVersionNumber before, the value is set to 0 (line 18) so currentVersionNumber is greater then savedVersionNumber then the What’s New dialog displayed (line 26). The value of currentVersionNumber then saved into SharedPreferences for later use (28-31) so the dialog will not be displayed anymore until the application is updated. Next time when the application updated, currentVersionNumber value will be greater than savedVersionNumber so the What’s New dialog is displayed again.
  • if (currentVersionNumber > savedVersionNumber) {
       	 		showWhatsNewDialog();
    
       	 		Editor editor	= sharedPref.edit();
    
       	 		editor.putInt(VERSION_KEY, currentVersionNumber);
       	 		editor.commit();
       	 	}
    

Download source code

Share
Related post:
bottom

Leave a Reply

 
bottom