• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> How to Show “What’s New” Dialog in Android App

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

January 14, 2012 by Lorensius Londa 3 Comments

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 New‘ dialog 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

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Create Android Image Picker
  2. How to Use Facebook SDK to Post Status From Android
  3. How to Create QuickAction Dialog in Android
  4. How to Programmatically Show and Hide Soft Keyboard in Android

Filed Under: Android Tagged With: Android, changes dialog, create dialog, dialog, whats new dialog

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Pol says

    November 13, 2012 at 5:22 am

    Hello and thanks! I’ve a problem: no changelogs show me, I add this:

    Toast.makeText(getApplicationContext(), “version number”+currentVersionNumber, Toast.LENGTH_SHORT).show();

    after:

    } catch (Exception e) {}

    toast show me only 1, I’ve:

    android:versionCode=”1″
    android:versionName=”1.30”

    I change 30 to 31 and I re-check but toast show me always 1

    what’s the problem?

    thanks!

    Reply
    • Evan Richardson says

      August 9, 2013 at 1:16 pm

      You need to use versionName, which is a string. VersionCode just returns the code used by android market.

      Reply
  2. windarion says

    January 22, 2013 at 9:42 am

    hi
    variable currentVersionNumber is type int. Plz change to float to get right current version.

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 355 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg359 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in