2

I will show the Versionname of my app in a textview. have anyone an idea why this only changes if you reinstalled the app and not on an update?

String appversion = "Version " + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;

The reading works perfectly! My question was: I have an app with version 1.1 in the store. After an update to 1.2 the Textview still contains 1.1.(only on update) But if the app is reinstalled then 1.2 will be displayed. Why is that, and how can I fix it?

Andreas
  • 255
  • 3
  • 16
  • Elaborate the question, please! – Yogi Apr 18 '18 at 11:57
  • Possible duplicate of [How to get the build/version number of your Android application?](https://stackoverflow.com/questions/4616095/how-to-get-the-build-version-number-of-your-android-application) – AskNilesh Apr 18 '18 at 11:57
  • no duplicate please read the question correct... i not need how i can read the appversion/name. I need why are not change on update.. Reinstallation it work – Andreas Apr 18 '18 at 12:18
  • Is it still happens if you are using `BuildConfig.VERSION_NAME`? – ישו אוהב אותך Apr 24 '18 at 16:20

2 Answers2

0

You can get current version name using

 String versionname = "";
    try {
           versionname = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    TextView versionname = (TextView) findViewById(R.id.textName);
    versionname.setText(versionname);
Janvi Vyas
  • 732
  • 5
  • 16
0

to get Version Name : use below code :

public String getVersion(){
 PackageInfo versionInfo = getPackageInfo();
            String title = context.getString(R.string.app_name) + " v"
                    + versionInfo.versionName;
    return title;
}

  private PackageInfo getPackageInfo() {
    PackageInfo info = null;
    try {
        info = context.getPackageManager().getPackageInfo(
                context.getPackageName(), PackageManager.GET_ACTIVITIES);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return info;
}

define your context If you are in fragment then context = getActivity(); if you are in activity then context = this.

then set it to your textview like textview.settext(getVersion());

Dhiren Basra
  • 820
  • 9
  • 24