While using your smartphones you must have gone through a situation where an app asks the user to update it to use it further. Here, we are going to implement the same and see how an app asks users to update it by showing an update alert dialog.
What we are going to build in this application?
Here is a sample video of what we are going to implement in this application. Note that we are going to use java language for this project.
Step by Step Implementation
Step 1: Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- Name the application at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2. Adding required dependency
Navigate to Gradle Scripts > gradle.scripts(module) and add the following dependency in it
implementation 'org.jsoup:jsoup:1.10.2'
Step 3. Adding Internet Permission
Navigate to the AndroidManifest.xml file and add the following permission to it-
<uses-permission android:name="android.permission.INTERNET"/>
Step 4. Working on XML file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" android:gravity = "center" tools:context = ".MainActivity" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Current Version" android:textSize = "32sp" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/tv_current_version" android:textSize = "32sp" android:textStyle = "bold" android:layout_marginTop = "4dp" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Latest Version" android:textSize = "32sp" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/tv_latest_version" android:textSize = "32sp" android:textStyle = "bold" android:layout_marginTop = "4dp" /> </ LinearLayout > |
Step 5. Working on Java file
Navigate to the MainActivity.java file and use the following code in it. Comments are added to the code to have a better understanding.
Java
package com.example.updatealertdialog; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.widget.TextView; import org.jsoup.Jsoup; import java.io.IOException; public class MainActivity extends AppCompatActivity { // Initialize variables TextView tvCurrentVersion,tvLatestVersion; String sCurrentVersion,sLatestVersion; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Assign variables tvCurrentVersion=findViewById(R.id.tv_current_version); tvLatestVersion=findViewById(R.id.tv_latest_version); // Get latest version from play store new GetLatestVersion().execute(); } private class GetLatestVersion extends AsyncTask<String,Void,String> { @Override protected String doInBackground(String... strings) { try { sLatestVersion= Jsoup +getPackageName()) .timeout( 30000 ) .get() .select( "div.hAyfc:nth-child(4)>" + "span:nth-child(2) > div:nth-child(1)" + "> span:nth-child(1)" ) .first() .ownText(); } catch (IOException e) { e.printStackTrace(); } return sLatestVersion; } @Override protected void onPostExecute(String s) { // Get current version sCurrentVersion=BuildConfig.VERSION_NAME; // Set current version on Text view tvCurrentVersion.setText(sCurrentVersion); // Set latest version on TextView tvLatestVersion.setText(sLatestVersion); if (sLatestVersion != null ) { // Version convert to float float cVersion=Float.parseFloat(sCurrentVersion); float lVersion=Float.parseFloat(sLatestVersion); // Check condition(latest version is // greater than the current version) if (lVersion > cVersion) { // Create update AlertDialog updateAlertDialog(); } } } } private void updateAlertDialog() { // Initialize AlertDialog AlertDialog.Builder builder= new AlertDialog.Builder( this ); // Set title builder.setTitle(getResources().getString(R.string.app_name)); // set message builder.setMessage( "UUpdate Available" ); // Set non cancelable builder.setCancelable( false ); // On update builder.setPositiveButton( "Update" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // Open play store startActivity( new Intent(Intent .ACTION_VIEW, // Dismiss alert dialog dialogInterface.dismiss(); } }); // on cancel builder.setNegativeButton( "Cancel" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // cancel alert dialog dialogInterface.cancel(); } }); // show alert dialog builder.show(); } } |
Here is the final output of our application.
Output: