I want to display the versionName ( number) located in the android manifest on my spalsh screen. I found some example codes but they are not working. This seems like a simple task but it does not display for me
text view from splash xml:
<TextView
android:id="@+id/ver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="33dp"
android:layout_marginRight="30dp"
android:text=" " />
display method:
private void displayVersionName() {
String versionName = "";
PackageInfo packageInfo;
try {
packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
versionName = "ver:" + packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
TextView tv = (TextView) findViewById(R.id.ver_name);
tv.setText(versionName);
}
Perhaps NameNotFoundException gets thrown? Or perhaps something happens when you are displaying the version string?
Anyway, I have exactly the same as you and it does work for me. You can verify with the below code what getVersionInfo returns, and only then try to display it
public String getVersionInfo() {
String strVersion = "Version:";
PackageInfo packageInfo;
try {
packageInfo = getApplicationContext()
.getPackageManager()
.getPackageInfo(
getApplicationContext().getPackageName(),
0
);
strVersion += packageInfo.versionName;
} catch (NameNotFoundException e) {
strVersion += "Unknown";
}
return strVersion;
}
Try this :
TextView tv = (TextView) findViewById(R.id.ver_name);
tv.setText("version : " + BuildConfig.VERSION_NAME);
And import the BuildConfig of the package where you want to have the versionName :
import com.yourpackage.BuildConfig;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With