Open In App

How to create a COVID-19 Tracker Android App

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Pre-requisites:
 

 

The world is facing one of the worst epidemics, the outbreak of COVID-19, you all are aware of that. So during this lockdown time let’s create a COVID-19 Tracker Android App using REST API which will track the Global Stats only.
 

 

  1. Step1: Opening a new project 
    • Open a new project just click of File option at topmost corner in left. 
       
    • Then click on new and open a new project with whatever name you want. 
       
    • Now we gonna work on Empty Activity with language as Java. Leave all other options as untouched. 
       
    • You can change the name of project as per your choice. 
       

  • By default, there will be two files activity_main.xml and MainActivity.java.
  1. Step 2: Before going to the coding section first you have to do some pre-task.
    • Go to app->res->values->colors.xml section and set the colors for your app.
       

colors.xml

 
  • Go to Gradle Scripts->build.gradle (Module: app) section and import following dependencies and click the “sync Now” on the above pop up.
     

build.gradle (:app)

 

 

AndroidManifests.xml

 
  1. Step3: Designing the UI 
    • Below is the code for the xml file. 
       

actibity_main.xml

 
  • After using this code in .xml file, the UI will be like:
     

  1. Step4: Working with Java file
    • Open the MainActivity.java file there within the class, first of all create the object of TextView class
       

// Create the object of TextView Class 
TextView tvCases, tvRecovered, tvCritical, tvActive, tvTodayCases, tvTotalDeaths, tvTodayDeaths, tvAffectedCountries; 
 

  • Secondly inside onCreate() method, we have to link those objects with their respective id’s that we have given in .XML file.
     

// link those objects with their respective id’s that we have given in .XML file 
tvCases = findViewById(R.id.tvCases); 
tvRecovered = findViewById(R.id.tvRecovered); 
tvCritical = findViewById(R.id.tvCritical); 
tvActive = findViewById(R.id.tvActive); 
tvTodayCases = findViewById(R.id.tvTodayCases); 
tvTotalDeaths = findViewById(R.id.tvTotalDeaths); 
tvTodayDeaths = findViewById(R.id.tvTodayDeaths); 
tvAffectedCountries = findViewById(R.id.tvAffectedCountries); 
 

  • Create a private void fetchdata() method outside onCreate() method and define it.
  • Inside fetchdata() method the most important task is going to happen that is how we fetch the data from a third party API and implement it in our app. My request is please read thoroughly the two articles Volley Library in Android and REST API (Introduction) to understand the following concepts.
  • Create a String request using Volley Library and assign the “url” with “https://corona.lmao.ninja/v2/all” link. 

     

Java




// Create a String request using Volley Library
 
String url = "https:// corona.lmao.ninja/v2/all";
 
StringRequest request
    = new StringRequest(
        Request.Method.GET,
        url,
        new Response.Listener() {
            @Override
            public void onResponse(
                String response)
            {
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(
                VolleyError error)
            {
            }
        });
 
RequestQueue requestQueue
    = Volley.newRequestQueue(this);
requestQueue.add(request);


  • Please refer this website to take a look at the requested data are in JSON format.
     

  • So the next thing you have to do is, inside the onResponse() method create the object of “JSONObject” class then set the data in text view which are available in JSON format with the help of “jsonobject”. Make sure that you have to do these things inside a “try” block. Remember that the parameter inside the getString() must match with the name given in JSON format.
     

Java




// Handle the JSON object and handle it inside try and catch
 
try {
    // Creating object of JSONObject
    JSONObject jsonObject
        = new JSONObject(
            response.toString());
 
    // Set the data in text view
    // which are available in JSON format
    // Note that the parameter
    // inside the getString() must match
    // with the name given in JSON format
    tvCases.setText(
        jsonObject.getString("cases"));
    tvRecovered.setText(
        jsonObject.getString("recovered"));
    tvCritical.setText(
        jsonObject.getString("critical"));
    tvActive.setText(
        jsonObject.getString("active"));
    tvTodayCases.setText(
        jsonObject.getString("todayCases"));
    tvTotalDeaths.setText(
        jsonObject.getString("deaths"));
    tvTodayDeaths.setText(
        jsonObject.getString("todayDeaths"));
    tvAffectedCountries.setText(
        jsonObject.getString("affectedCountries"));
}
catch (JSONException e) {
    e.printStackTrace();
}


  • And inside onErrorResponse() method you have to show a toast message if any error occurred. 
     
Toast.makeText(MainActivity.this, 
               error.getMessage(), 
               Toast.LENGTH_SHORT)
     .show();
  • At last invoke the fetchdata() method inside onCreate() method. 
     

Output:
 

 



Last Updated : 24 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads