2

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);
  }
4
  • @njzk2 I have main in the settings screen so one can quickly check
    – Alexander
    Commented Dec 7, 2012 at 16:24
  • post stack trace ? do you have logs ? when do you call this method ?
    – njzk2
    Commented Dec 7, 2012 at 16:24
  • Have you tried to Log the version number at any point? Log.i("displayVersionName", "Version: " + versionName);. I can't see anything wrong with the code you're showing here.
    – jnthnjns
    Commented Dec 7, 2012 at 16:40
  • BuildConfig.VERSION_NAME duuuuuuuuuuuuuuude!!!!!! Commented Apr 9, 2020 at 20:15

3 Answers 3

8

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;
    }
2
  • you are really not supposed to receive an exception here, given that the package name is the name of the currently running package, which, obviously, is installed (and hence can be found)
    – njzk2
    Commented Dec 7, 2012 at 16:25
  • @Alex(@njzk2) LOL - in the ADT I actually get reprimanded for not catching the exception if left out - but there's a missing semi-colon after "Unknown". Still +1.
    – flowtron
    Commented Sep 10, 2014 at 10:36
1

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;
1
  • yeah just the BuildConfig.VERSION_NAME , don't understand the efforts in the other answers Commented Apr 9, 2020 at 20:13
0

Change your code as to get Your application version name using code

    private void displayVersionName() {
    String verName = "";
    try {
        verName = "ver:" + Current_Activity.this.getPackageManager().
                            "YOUR_APPLICATION_PACKAGE_NAME", 0).versionName;  
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    TextView tv = (TextView) findViewById(R.id.ver_name);
    tv.setText(verName);
  }

Not the answer you're looking for? Browse other questions tagged or ask your own question.