Open In App

How to create a COVID-19 Tracker Android App

Last Updated : 27 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


covid-19


Step by Step Implementation to Create Covid-19 Tracker Android Application 

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.

Step 2: Adding Dependency in Application

- Before going to the coding section first you have to do some pre-task. Go to Gradle Scripts->build.gradle (Module: app) section and import following dependencies and click the "sync Now" on the above pop up. We are adding Volley Library in Android.

build.gradle (:app)

 dependencies {
    // Other dependencies...
    implementation 'com.android.volley:volley:1.2.1'
}

- Go to app->manifests->AndroidManifests.xml section and allow "Internet Permission".

AndroidManifests.xml

 <uses-permission android:name="android.permission.INTERNET" />

Step 3: Designing the UI 

Below is the code for the xml file. 

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@color/white"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Global Stats"
                android:layout_marginBottom="20dp"
                android:textColor="#050505"
                android:textAllCaps="true"
                android:textAlignment="center"
                android:textSize="25sp"
                android:textStyle="bold"
                android:background="@color/green"/>



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Total Cases"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvCases"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Recovered"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvRecovered"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <!-- Repeat similar pattern for other statistics -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Critical"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvCritical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Active"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvActive"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Today Cases"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvTodayCases"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Total Deaths"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvTotalDeaths"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Today Deaths"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvTodayDeaths"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#E0E0E0"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:fontFamily="sans-serif-medium"
                    android:text="Affected Countries"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvAffectedCountries"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textAlignment="textEnd"
                    android:textColor="@color/color_one"
                    android:textSize="18sp"
                    android:textStyle="bold"/>

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>


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

Layout


Step 4: Working with Java file

We can break the whole application into multiple steps:

  • Creating instance of TextViews and attach them with the views available in the Layouts(To Display the whole data).
  • Creating Volley newRequestQueue , it is resonsible for managing Network request in Android.
  • After that we need a API for collecting data for the application. [ Check on this API : "https://disease.sh/v3/covid-19/all" ]
  • Extract Data from the API in JSON format using volley and update the values in the Application.
  • Run the Application in your device.

Note: Remember that the parameter inside the getString() must match with the name given in JSON format.

MainActivity.java:

Java
package com.gfg.covid_java;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private TextView tvCases, tvRecovered, tvCritical, tvActive,
    private TextView tvTodayCases, tvTotalDeaths, tvTodayDeaths, tvAffectedCountries;
    private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // API with Covid Data
        String url="https://disease.sh/v3/covid-19/all";

        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);

        queue = Volley.newRequestQueue(this);

        // Creating JSON Object Request from URL
        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            
            //Responsible for Reading data in JSON format
            
            @Override
            public void onResponse(JSONObject response) {
                try{
                    
                    //Updating the data in the Layout elements
                    tvCases.setText(String.format("%,d", response.getInt("cases")));
                    tvRecovered.setText(String.format("%,d", response.getInt("recovered")));
                    tvCritical.setText(String.format("%,d", response.getInt("critical")));
                    tvActive.setText(String.format("%,d", response.getInt("active")));
                    tvTodayCases.setText(String.format("%,d", response.getInt("todayCases")));
                    tvTotalDeaths.setText(String.format("%,d", response.getInt("deaths")));
                    tvTodayDeaths.setText(String.format("%,d", response.getInt("todayDeaths")));
                    tvAffectedCountries.setText(String.format("%,d", response.getInt("affectedCountries")));

                }
                catch (JSONException e){
                    e.printStackTrace();
                    
                    // Making Toast if error occurs
                    Toast.makeText(MainActivity.this, "Error fetching data", Toast.LENGTH_SHORT).show();
                }
            }
        }, error -> {
            
            // Handle error
            Log.e("COVID_DATA", "Error fetching data", error);
                    
            // Making Toast if error occurs
            Toast.makeText(MainActivity.this, "Network error. Please check your connection.",
                            Toast.LENGTH_SHORT).show();
        });

        // Add the request to the RequestQueue.
        queue.add(req);

    }

    @Override
    protected void onStop() {
        super.onStop();
        
        // Cancel any pending requests when the activity stops
        if (queue != null) {
            queue.cancelAll(this);
        }
    }
}


Output:

Output


 


Next Article

Similar Reads