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

Error : One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

HOW TO DISCARD UNDER REVIEW APP BUNDLE AND UPLOAD NEW APP BUNDLE

Problems To Upgrade Android Gradle Plugin from 4.1.3 to 8.1.2 and solutions