Get Android App Version No and Display in a Text Field

 This is how we get Version Name of Android App from Google PlayConsole.


1.  In onCreate Method

navigationView = findViewById(R.id.nav_view);

if (navigationView != null) {
getVersionName();
} else {
// Handle the case when navigationView is null
Toast.makeText(getApplicationContext(),"Navigation Not Initialized",Toast.LENGTH_LONG).show();
}

2. Now define getVersionName()
private void getVersionName() {
Menu menu = navigationView.getMenu();
MenuItem nav_version = menu.findItem(R.id.nav_version);
PackageInfo pinfo;
try {
pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
String versionName = "App Version: " + pinfo.versionName;
nav_version.setTitle(versionName);
} catch (PackageManager.NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Here we have added a menu item inside DrawerLayout to display version name. A TextView can also be used to display version name anywhere.

Comments

Popular posts from this blog

How to Add Animation to Android App in JAVA

How to Send Crash Report to Email / Mobile Device in Android Java