DEV Community

HMS Community
HMS Community

Posted on

Fetch the Covid-19 cases list using the Volley Library in Patient Tracking Android app (Kotlin) – Part 3

Image description
Introduction

In this article, we can learn how to get the number of Covid-19 affected cases world-wide as well as India wise. Here, we have used the Volley as Library to fetch the data using the REST API to display in the application. In this application, we can find the total cases affected, number of cases recovered and number of deaths on world wide as well as in India can find the state wise list also.

So, I will provide a series of articles on this Patient Tracking App, in upcoming articles I will integrate other Huawei Kits.

If you are new to this application, follow my previous articles.

https://forums.developer.huawei.com/forumPortal/en/topic/0201902220661040078

https://forums.developer.huawei.com/forumPortal/en/topic/0201908355251870119

Requirements

  1. Any operating system (MacOS, Linux and Windows).
  2. Must have a Huawei phone with HMS 4.0.0.300 or later.
  3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 and above installed.
  4. Minimum API Level 24 is required.
  5. Required EMUI 9.0.0 and later version devices.

How to integrate HMS Dependencies

  • First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

  • Create a project in android studio, refer Creating an Android Studio Project.

  • Generate a SHA-256 certificate fingerprint.

  • To generate SHA-256 certificate fingerprint. On right-upper corner of android project click Gradle, choose Project Name > Tasks > android, and then click signingReport, as follows.

Image description

Note: Project Name depends on the user created name.

Image description

  • Enter SHA-256 certificate fingerprint and click Save button, as follows.

Image description

  • Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'

Enter fullscreen mode Exit fullscreen mode
  • Add the below plugin and dependencies in build.gradle(Module) file.
apply plugin: id 'com.huawei.agconnect'
// Huawei AGC
implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
// Recyclerview
implementation 'androidx.recyclerview:recyclerview:1.2.1'
// Volley Library
implementation 'com.android.volley:volley:1.2.1'
Enter fullscreen mode Exit fullscreen mode
  • Now Sync the gradle.

  • Add the required permission to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />
Enter fullscreen mode Exit fullscreen mode

Let us move to development

I have created a project on Android studio with empty activity let us start coding.

In the Home.kt activity to find the business logic for button item click.

class Home : AppCompatActivity(), HomeAdapter.ItemListener {

    private lateinit var recyclerView: RecyclerView
    private lateinit var arrayList: ArrayList<HomeIcons>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        recyclerView = findViewById(R.id.recyclerview_list)
        arrayList = ArrayList()
        arrayList.add(HomeIcons("Patient", "Add details", R.drawable.add_icon, "#00b0ff"))
        arrayList.add(HomeIcons("Covid-19", "Covid Cases", R.drawable.covid_icon, "#00b0ff"))

        val adapter = HomeAdapter(applicationContext, arrayList, this)
        recyclerView.adapter = adapter
        recyclerView.layoutManager = GridLayoutManager(this, 2)
        recyclerView.setHasFixedSize(true)

    }

    override fun onItemClick(item: Int) {
        when(item ) {
            0 -> {val intent = Intent(this, PatientActivity::class.java)
                startActivity(intent)
            }
            1 ->  {val intent = Intent(this@Home, CovidActivity::class.java)
                startActivity(intent)
            }

        }
    }


}
Enter fullscreen mode Exit fullscreen mode

In the CovidActivity.kt activity to find the business logic for cases list data.

class CovidActivity : AppCompatActivity() {

    lateinit var worldCases: TextView
    lateinit var worldRecovered: TextView
    lateinit var worldDeaths: TextView
    lateinit var countryCases: TextView
    lateinit var countryRecovered: TextView
    lateinit var countryDeaths: TextView
    lateinit var stateRV: RecyclerView
    lateinit var stateRVAdapter: CasesAdapter
    lateinit var stateList: List<CasesData>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_covid)

        worldCases = findViewById(R.id.txt_world_cases)
        worldRecovered = findViewById(R.id.txt_world_recovered)
        worldDeaths = findViewById(R.id.txt_world_deaths)
        countryCases = findViewById(R.id.txt_india_cases)
        countryRecovered = findViewById(R.id.txt_india_recovered)
        countryDeaths = findViewById(R.id.txt_india_deaths)
        stateRV = findViewById(R.id.recyclerview_cases)
        stateList = ArrayList<CasesData>()
        getStateInfo()
        getWorldInfo()

    }

    private fun getStateInfo(){
        val url = "https://api.rootnet.in/covid19-in/stats/latest"
        val queue = Volley.newRequestQueue(this@CovidActivity)
        val request = JsonObjectRequest(Request.Method.GET,url, null, { response ->
            try{
                val dataObj = response.getJSONObject("data")
                val summaryObj = dataObj.getJSONObject("summary")
                val cases: Int = summaryObj.getInt("total")
                val recovered:Int = summaryObj.getInt("discharged")
                val deaths: Int = summaryObj.getInt("deaths")

                countryCases.text = cases.toString()
                countryRecovered.text = recovered.toString()
                countryDeaths.text = deaths.toString()

                val regionalArray = dataObj.getJSONArray("regional")
                for(i in 0 until regionalArray.length()){
                    val regionalObj = regionalArray.getJSONObject(i)
                    val stateName: String = regionalObj.getString("loc")
                    val cases: Int = regionalObj.getInt("totalConfirmed")
                    val deaths: Int = regionalObj.getInt("deaths")
                    val recovered: Int = regionalObj.getInt("discharged")

                    val stateCases = CasesData(stateName,recovered,deaths,cases)
                    stateList = stateList+stateCases
                }
                stateRVAdapter = CasesAdapter(stateList)
                stateRV.layoutManager = LinearLayoutManager(this)
                stateRV.adapter = stateRVAdapter
            }
            catch (e:JSONException){
                e.printStackTrace()
            }

        },  {error -> Toast.makeText(this, "Failed to get Data", Toast.LENGTH_SHORT).show()
        }
        )
        queue.add(request)

    }

    private fun getWorldInfo(){
        val url = "https://corona.lmao.ninja/v3/covid-19/all"
        val queue = Volley.newRequestQueue(this@CovidActivity)
        val request = JsonObjectRequest(Request.Method.GET,url, null, { response ->
            try{
                val worldCases1: Int = response.getInt("cases")
                val worldRecovered1:Int = response.getInt("recovered")
                val worldDeaths1: Int = response.getInt("deaths")

                worldRecovered.text = worldRecovered1.toString()
                worldCases.text = worldCases1.toString()
                worldDeaths.text = worldDeaths1.toString()

            }
            catch (e:JSONException){
                e.printStackTrace()
            }

        },  {error ->
             Toast.makeText(this, "Failed to get Data", Toast.LENGTH_SHORT).show()
        }
        )
        queue.add(request)

    }


}
Enter fullscreen mode Exit fullscreen mode

Create a CasesData.kt data class to list the data variables.

data class CasesData(
    val state: String,
    val recovered: Int,
    val deaths: Int,
    val cases: Int
)
Enter fullscreen mode Exit fullscreen mode

Create a CasesAdapter.kt adapter class to hold the list.

class CasesAdapter (private val stateList: List<CasesData>): RecyclerView.Adapter<CasesAdapter.ViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CasesAdapter.ViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(R.layout.cases_list, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val stateData = stateList[position]
        holder.stateCases.text = stateData.cases.toString()
        holder.stateName.text = stateData.state.toString()
        holder.stateDeaths.text = stateData.deaths.toString()
        holder.stateRecovered.text = stateData.recovered.toString()
    }

    override fun getItemCount(): Int {
        return stateList.size

    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val stateName = itemView.findViewById<View>(R.id.txt_state) as TextView
        val stateCases = itemView.findViewById<View>(R.id.txt_state_cases) as TextView
        val stateDeaths = itemView.findViewById<View>(R.id.txt_state_deaths) as TextView
        val stateRecovered = itemView.findViewById<View>(R.id.txt_state_recovered) as TextView
    }

}
Enter fullscreen mode Exit fullscreen mode

In the activity_home.xml we can create the UI screen for Recycler View item buttons.

<?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:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    tools:context=".Home">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:visibility="gone"
        android:layout_marginTop="40dp"
        android:clickable="true"
        tools:ignore="MissingConstraints">
        <ImageView
            android:id="@+id/img_android"
            android:layout_width="155dp"
            android:layout_height="180dp"
            android:layout_marginRight="15dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/add_icon"/>
        <ImageView
            android:id="@+id/img_hms_icon"
            android:layout_width="155dp"
            android:layout_height="180dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/doctor_icon"/>
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Enter fullscreen mode Exit fullscreen mode

In the activity_covid.xml we can create the UI screen for cases list.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".covid.CovidActivity">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:layout_marginTop="0dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="50dp"
                android:layout_height="100dp"
                android:layout_marginLeft="25dp"
                android:layout_marginBottom="25dp"
                android:src="@drawable/world_icon"      >
            </ImageView>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:text="World Wide Record"
                android:textAlignment="center"
                android:layout_marginBottom="30dp"
                android:textSize="18sp"
                android:padding="3dp"
                android:textColor="@color/black"
                android:layout_marginTop="25dp">
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:orientation="horizontal"
            android:weightSum="3" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                tools:ignore="Suspicious0dp"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Cases"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="15sp"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@+id/txt_world_cases"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Number of Cases"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="12sp"
                    android:padding="4dp"
                    android:textColor="@color/blue">
                </TextView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                tools:ignore="Suspicious0dp"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Recovered"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="15sp"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@+id/txt_world_recovered"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Number of Recovered"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="12sp"
                    android:padding="4dp"
                    android:textColor="@color/green">
                </TextView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                tools:ignore="Suspicious0dp"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Deaths"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="15sp"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@+id/txt_world_deaths"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Number of Deaths"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="12sp"
                    android:padding="4dp"
                    android:textColor="@color/red">
                </TextView>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        app:cardCornerRadius="3dp"
        app:cardElevation="3dp">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_marginTop="0dp"
                android:orientation="horizontal">
                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="100dp"
                    android:layout_marginLeft="25dp"
                    android:layout_marginBottom="25dp"
                    android:src="@drawable/india_icon"      >
                </ImageView>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:text="India Record"
                    android:textAlignment="center"
                    android:layout_marginBottom="30dp"
                    android:textSize="18sp"
                    android:padding="3dp"
                    android:textColor="@color/indianblue"
                    android:layout_marginTop="25dp">
                </TextView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:orientation="horizontal"
                android:weightSum="3" >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    tools:ignore="Suspicious0dp"
                    android:layout_weight="1"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Cases"
                        android:textAlignment="center"
                        android:layout_gravity="center_horizontal"
                        android:textSize="15sp"
                        android:padding="4dp"
                        android:textStyle="bold"
                        android:textColor="@color/black">
                    </TextView>
                    <TextView
                        android:id="@+id/txt_india_cases"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:padding="4dp"
                        android:text="Number of Cases"
                        android:textAlignment="center"
                        android:textColor="@color/blue"
                        android:textSize="12sp">
                    </TextView>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    tools:ignore="Suspicious0dp"
                    android:layout_weight="1"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Recovered"
                        android:textAlignment="center"
                        android:layout_gravity="center_horizontal"
                        android:textSize="15sp"
                        android:padding="4dp"
                        android:textStyle="bold"
                        android:textColor="@color/black">
                    </TextView>
                    <TextView
                        android:id="@+id/txt_india_recovered"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Number of Recovered"
                        android:textAlignment="center"
                        android:layout_gravity="center_horizontal"
                        android:textSize="12sp"
                        android:padding="4dp"
                        android:textColor="@color/green">
                    </TextView>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    tools:ignore="Suspicious0dp"
                    android:layout_weight="1"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Deaths"
                        android:textAlignment="center"
                        android:layout_gravity="center_horizontal"
                        android:textSize="15sp"
                        android:padding="4dp"
                        android:textStyle="bold"
                        android:textColor="@color/black">
                    </TextView>
                    <TextView
                        android:id="@+id/txt_india_deaths"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Number of Deaths"
                        android:textAlignment="center"
                        android:layout_gravity="center_horizontal"
                        android:textSize="12sp"
                        android:padding="4dp"
                        android:textColor="@color/red">
                    </TextView>
                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_cases"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>
Enter fullscreen mode Exit fullscreen mode

In the cases_list.xml we can create the UI screen for customized items of state wise cases.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    app:cardCornerRadius="3dp"
    app:cardElevation="3dp" >

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

        <TextView
            android:id="@+id/txt_state"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="State Name"
            android:textSize="18sp"
            android:padding="5dp"
            android:layout_margin="3dp"
            android:textColor="@color/teal_700">
        </TextView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:orientation="horizontal"
            android:weightSum="3" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Cases"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="15sp"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@+id/txt_state_cases"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Number of Cases"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="12sp"
                    android:padding="4dp"
                    android:textColor="@color/blue">
                </TextView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Recovered"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="15sp"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@+id/txt_state_recovered"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Number of Recovered"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="12sp"
                    android:padding="4dp"
                    android:textColor="@color/green">
                </TextView>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Deaths"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="15sp"
                    android:padding="4dp"
                    android:textStyle="bold"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@+id/txt_state_deaths"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Number of Deaths"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:textSize="12sp"
                    android:padding="4dp"
                    android:textColor="@color/red">
                </TextView>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>
Enter fullscreen mode Exit fullscreen mode

Demo

Image description

Tips and Tricks

  1. Make sure you are already registered as Huawei developer.
  2. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.
  3. Make sure you have added the agconnect-services.json file to app folder.
  4. Make sure you have added SHA-256 fingerprint without fail.
  5. Make sure all the dependencies are added properly.

Conclusion

In this article, we have learned that how to get the number of Covid-19 affected cases world-wide as well as India wise. Here, we have used the Volley as Library to fetch the data using the REST API to display in the application. In this application, we can find the total cases affected, number of cases recovered and number of deaths on worldwide as well as in India can find the state wise list also.

I hope you have read this article. If you found it is helpful, please provide likes and comments.

Reference

Volley Library - https://www.geeksforgeeks.org/volley-library-in-android/#:~:text=Volley%20is%20an%20HTTP%20library,interfering%20with%20the%20user%20experience.

Top comments (0)