Pre-requisites:
- Android App Development Fundamentals for Beginners
- Guide to Install and Set up Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- REST API (Introduction)
- Volley Library in Android
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.
- 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: 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
<?xmlversion="1.0"encoding="utf-8"?><resources><colorname="colorPrimary">#42C14B</color><colorname="colorPrimaryDark">#3BC545</color><colorname="colorAccent">#05af9b</color><colorname="color_one">#fb7268</color><colorname="color_white">#ededf2</color></resources>chevron_rightfilter_none - 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)
// Volley libraryimplementation 'com.android.volley:volley:1.1.1'chevron_rightfilter_none - Go to app->manifests->AndroidManifests.xml section and allow “Internet Permission“.
AndroidManifests.xml
<!--Allow Internet Permission--><uses-permissionandroid:name="android.permission.INTERNET"/>chevron_rightfilter_none
- Go to app->res->values->colors.xml section and set the colors for your app.
- Step3: Designing the UI
- Below is the code for the xml file.
actibity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/color_white"android:orientation="vertical"tools:context=".MainActivity"><!--Linear Layout to display all the details--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="20dp"android:orientation="vertical"><!--Text view to display Global stats--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Global Stats"android:textColor="#050505"android:textAllCaps="true"android:textAlignment="center"android:textSize="35sp"android:textStyle="bold"/><!--Text view to display Total Cases--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:layout_marginTop="20dp"android:text="Total Cases"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvCases"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Recovered Cases--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Recovered"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvRecovered"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Critical Cases--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Critical"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvCritical"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Active Cases--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Active"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvActive"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Today Cases--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Today Cases"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvTodayCases"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Total Deaths--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Total Deaths"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvTotalDeaths"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Today Deaths--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Today Deaths"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvTodayDeaths"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/><!--Text view to display Affected Countries--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:fontFamily="sans-serif-light"android:text="Affected Countries"android:textAlignment="center"android:textStyle="bold"android:textSize="30sp"/><!--Text view to display the updated number when datawill fetch from the API. For now default set to 0 --><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="0"android:id="@+id/tvAffectedCountries"android:textAlignment="center"android:textSize="25sp"android:textColor="@color/color_one"android:textStyle="bold"android:fontFamily="sans-serif-light"/></LinearLayout></ScrollView>chevron_rightfilter_none - After using this code in .xml file, the UI will be like:
- Below is the code for the xml file.
- 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); - Creat a
private void fetchdata()method outsideonCreate()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.
// Create a String request using Volley LibraryStringRequest request=newStringRequest(Request.Method.GET,url,newResponse.Listener() {@OverridepublicvoidonResponse(String response){}},newResponse.ErrorListener() {@OverridepublicvoidonErrorResponse(VolleyError error){}});RequestQueue requestQueue= Volley.newRequestQueue(this);requestQueue.add(request);chevron_rightfilter_none - 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 thegetString()must match with the name given in JSON format.// Handle the JSON object and handle it inside try and catchtry{// Creating object of JSONObjectJSONObject jsonObject=newJSONObject(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 formattvCases.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();}chevron_rightfilter_none - And inside
onErrorResponse()method you have to show a toast message if any error occured.Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT) .show(); -
At last invoke the
fetchdata()method insideonCreate()method.
Below is the complete code for MainActivity.java file:
MainActivity.java
packagecom.example.covid_19tracker;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.TextView;importandroid.widget.Toast;importcom.android.volley.Request;importcom.android.volley.RequestQueue;importcom.android.volley.Response;importcom.android.volley.VolleyError;importcom.android.volley.toolbox.StringRequest;importcom.android.volley.toolbox.Volley;importorg.json.JSONException;importorg.json.JSONObject;publicclassMainActivityextendsAppCompatActivity {// Create the object of TextViewTextView tvCases, tvRecovered,tvCritical, tvActive,tvTodayCases, tvTotalDeaths,tvTodayDeaths,tvAffectedCountries;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Link those objects with their respective id's// that we have given in .XML filetvCases= 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);// Creating a method fetchdata()fetchdata();}privatevoidfetchdata(){// Create a String request// using Volley LibraryStringRequest request=newStringRequest(Request.Method.GET,url,newResponse.Listener<String>() {@OverridepublicvoidonResponse(String response){// Handle the JSON object and// handle it inside try and catchtry{// Creating object of JSONObjectJSONObject jsonObject=newJSONObject(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 formattvCases.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();}}},newResponse.ErrorListener() {@OverridepublicvoidonErrorResponse(VolleyError error){Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_SHORT).show();}});RequestQueue requestQueue= Volley.newRequestQueue(this);requestQueue.add(request);}}chevron_rightfilter_none - Open the MainActivity.java file there within the class, first of all create the object of TextView class.
Output:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to create a COVID19 Data Representation GUI?
- Android | How to Create/Start a New Project in Android Studio?
- How to Create a Quiz App In Android?
- How to create AnimatedGradient in Android?
- How to create a Stopwatch App using Android Studio
- Android | Alert Dialog Box and How to create it
- How to Create Your Own Shortcut in Android Studio?
- How to create a custom AlertDialog in Android
- How to create Swipe Navigation in an Android App
- How to create an Expandable CardView in Android
- How to Create Static Shortcuts in Android App?
- How to create a nested RecyclerView in Android
- How to create a Facebook login using an Android App?
- Android | Auto Complete TextBox and How to Create it
- How to Create RecyclerView with Multiple ViewType in Android?
- How to Create a Morse Code Converter Android App?
- Create an Expandable Notification Containing a Picture in Android
- Create an Expandable Notification Containing Some Text in Android
- Create an Instagram/Twitter Heart Like Animation in Android
- How to create a Circular image view in Android without using any library?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AmiyaRanjanRout





