Question Detail

How to get version code in android programmatically?

5 years ago Views 1431 Visit Post Reply

How to get Android Application Current Veriosn Code? I want to show my Version code in my Setting Activity every time when i update my App Version Code / Build Code change it will auto change in my Setting Activity not need any Static Value.
How can i get current version of my Mobile App?


Thread Reply

Hemant Sharma

- 5 years ago

To Implement get Version Code in Your Activity 

TextView appVersion_account_TextView;
appVersion_account_TextView= (TextView) findViewById(R.id.appVersion_account_TextView_ID);
try {
    String versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode+"";  //this will get your Current Version Code
    appVersion_account_TextView.setText("Version : v"+versionCode);
}catch (Exception e){
    Log.e("EXCEPTION",e.toString());
}

 

To get Version Code In Fragment You need to call getActivity()  this function will get instance of your Activity.

TextView appVersion_account_TextView;
appVersion_account_TextView= (TextView) view.findViewById(R.id.appVersion_account_TextView_ID);
try {
    String versionCode = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionCode+"";  //this will get your Current Version Code
    appVersion_account_TextView.setText("Version : v"+versionCode);
}catch (Exception e){
    Log.e("EXCEPTION",e.toString());
}

Hemant Sharma

- 5 years ago

To Implement get Version Code in Your Activity 

TextView appVersion_account_TextView;
appVersion_account_TextView= (TextView) findViewById(R.id.appVersion_account_TextView_ID);
try {
    String versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode+"";  //this will get your Current Version Code
    appVersion_account_TextView.setText("Version : v"+versionCode);
}catch (Exception e){
    Log.e("EXCEPTION",e.toString());
}

 

To get Version Code In Fragment You need to call getActivity()  this function will get instance of your Activity.

TextView appVersion_account_TextView;
appVersion_account_TextView= (TextView) view.findViewById(R.id.appVersion_account_TextView_ID);
try {
    String versionCode = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionCode+"";  //this will get your Current Version Code
    appVersion_account_TextView.setText("Version : v"+versionCode);
}catch (Exception e){
    Log.e("EXCEPTION",e.toString());
}