LoadMore
LoadMore

Reputation: 31

Checking Plays store version with current version and show update dialog

I have published one app on playstore and now i want to dhow update dialog when new apk is available on playstore.

I get latest version and current version

Till now i tried following code

      if (latestVersion != null && !latestVersion.isEmpty()) {
        if (Float.valueOf(currentVersion) < Float.valueOf(latestVersion)) {
            update_dialog = new Dialog(MainActivity.this);
            update_dialog.setContentView(R.layout.update_dialog);
            update_dialog.setCancelable(false);
            TextView dd = (TextView) update_dialog.findViewById(R.id.udata);
            Button update = (Button) update_dialog.findViewById(R.id.btn_update);
            c_font = Typeface.createFromAsset(getAssets(), "fonts/CaviarDreams_Italic.ttf");
            dd.setTypeface(c_font);
            update.setTypeface(c_font);
            update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.this.anything.unknown"));
                    startActivity(intent);
                }
            });
            update_dialog.show();
        } else {

        }
    }

it work fine but now i want to set version 1.2.0(Two dots).then how to check 1.0(one dot) with 1.2.0(two dots)

How to compare 1.2.0 and 1.0 or 1.2.0 and 1.2.1

Upvotes: 1

Views: 88

Answers (2)

Sujay
Sujay

Reputation: 3455

For your question, the easiest way is to convert the version by splitting the version with dots into array & comparing each array elements.

String currentVersion = "1.1";
String latestVersion = "1.2.3";
String[] currentVersionArray = currentVersion.split("\\.");
String[] latestVersionArray = latestVersion.split("\\."); 

int minLength = Math.min(currentVersionArray.length, latestVersionArray.length);
boolean isUpgradeAvailable = false;

for(int i = 0; i < minLength; i++) {
    if(Integer.parseInt(currentVersionArray[i]) < Integer.parseInt(latestVersionArray[i])) {
        isUpgradeAvailable = true;
        break;
    }
}
if(!isUpgradeAvailable && minLength != latestVersionArray.length)
    isUpgradeAvailable = true;

Upvotes: 1

Sourabh Bans
Sourabh Bans

Reputation: 3134

Best way is to compare versionCode not versionName. You will get it in Integer form.

Upvotes: 2

Related Questions