colors.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#024265</color> <color name="colorPrimaryDark">#024265</color> <color name="colorAccent">#05af9b</color> <color name="color_one">#fb7268</color> <color name="color_white">#ededf2</color> <color name="color_two">#E3E0E0</color> <color name="cases">#FFA726</color> <color name="recovered">#66BB6A</color> <color name="deaths">#EF5350</color> <color name="active">#29B6F6</color> </resources> |
build.gradle
1 2 |
// Volley library implementation 'com.android.volley:volley:1.1.1' |
And also we need to add the internet
permission to get data from the api
1 2 |
<!--Allow Internet Permission--> <uses-permission android:name="android.permission.INTERNET" /> |
XML Code
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="17dp" android:text="GEEKSFORGEEKS" android:textColor="@color/recovered" android:textSize="24sp" /> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="550dp" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_marginStart="10dp" android:layout_marginTop="60dp" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" /> </RelativeLayout> |
testing.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@color/color_white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal" android:weightSum="2"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_weight="1" android:orientation="vertical"> <!--Text view to display City name--> <TextView android:id="@+id/state" android:layout_width="210dp" android:layout_height="match_parent" android:gravity="center_vertical" android:text="City Name" android:textColor="@color/colorAccent" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--Text view to display Active Cases--> <TextView android:layout_width="107dp" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="Active- " android:textStyle="bold" /> <!--Text view to display Active Cases in Numbers--> <TextView android:id="@+id/active" android:layout_width="72dp" android:layout_height="wrap_content" android:text="2323223 " android:textColor="@color/active" android:textStyle="bold" /> <!--Image view to display increase in Cases --> <ImageView android:layout_width="17dp" android:layout_height="match_parent" android:src="@drawable/change" /> <!--Text view to display Todays Active Cases in Numbers--> <TextView android:id="@+id/incactive" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="32" android:textColor="@color/active" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--Text view to display Cured Cases --> <TextView android:layout_width="107dp" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="Cured- " android:textStyle="bold" /> <!--Text view to display Total Cured Cases in Numbers --> <TextView android:id="@+id/cured" android:layout_width="72dp" android:layout_height="wrap_content" android:text="2323223 " android:textColor="@color/recovered" android:textStyle="bold" /> <!--Image view to display increase in Cases --> <ImageView android:layout_width="18dp" android:layout_height="match_parent" android:src="@drawable/change" /> <!--Text view to display Todays Cured Cases in Numbers --> <TextView android:id="@+id/inccured" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="32" android:textColor="@color/recovered" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--Text view to display deths Cases --> <TextView android:layout_width="107dp" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="death- " android:textStyle="bold" /> <!--Text view to display total death Cases in Numbers --> <TextView android:id="@+id/death" android:layout_width="72dp" android:layout_height="wrap_content" android:text="2323223 " android:textColor="@color/deaths" android:textStyle="bold" /> <!--Image view to display increase in Cases --> <ImageView android:layout_width="18dp" android:layout_height="match_parent" android:src="@drawable/change" /> <!--Text view to display todays death Cases in Numbers --> <TextView android:id="@+id/incdeath" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="32" android:textColor="@color/deaths" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <!--Text view to display total Number of cases Cases --> <TextView android:layout_width="107dp" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="total- " android:textStyle="bold" /> <!--Text view to display total Number of cases Cases in Numbers --> <TextView android:id="@+id/total" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2323223 " android:textStyle="bold" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> <View android:layout_width="391dp" android:layout_height="6dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_marginStart="11dp" android:layout_marginLeft="20dp" android:layout_marginTop="113dp" android:layout_marginEnd="10dp" android:layout_marginRight="20dp" android:background="@color/color_two" /> </RelativeLayout> |
Java Code
Model.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
public class Model { private String name, total, death, cured, active, incAct, incDec, incRec; public Model(String name, String total, String death, String cured, String active, String incAct, String incDec, String incRec) { this.name = name; this.total = total; this.death = death; this.cured = cured; this.active = active; this.incAct = incAct; this.incDec = incDec; this.incRec = incRec; } public String getIncAct() { return incAct; } public void setIncAct(String incAct) { this.incAct = incAct; } public String getIncDec() { return incDec; } public void setIncDec(String incDec) { this.incDec = incDec; } public String getIncRec() { return incRec; } public void setIncRec(String incRec) { this.incRec = incRec; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTotal() { return total; } public void setTotal(String total) { this.total = total; } public String getDeath() { return death; } public void setDeath(String death) { this.death = death; } public String getCured() { return cured; } public void setCured(String cured) { this.cured = cured; } public String getActive() { return active; } public void setActive(String active) { this.active = active; } } |
Step 7: Create an Adapter Class
Now create a new JAVA Adapter Class to put the data that have fetched into a ListView that has created before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.util.List; // Create A Class Adapter public class Adapter extends ArrayAdapter<Model> { private Context context; private List<Model> modelList; public Adapter(Context context, List<Model> modelList) { super(context, R.layout.testing, modelList); this.context = context; this.modelList = modelList; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.testing, null,true); // In this step we connect the XML with Java File TextView state = view.findViewById(R.id.state); TextView active = view.findViewById(R.id.active); TextView cured = view.findViewById(R.id.cured); TextView death = view.findViewById(R.id.death); TextView total = view.findViewById(R.id.total); TextView incactive = view.findViewById(R.id.incactive); TextView inccured = view.findViewById(R.id.inccured); TextView incdeath = view.findViewById(R.id.incdeath); // Adding Data to modellist state.setText(modelList.get(position).getName()); active.setText(modelList.get(position).getActive()); cured.setText(modelList.get(position).getCured()); death.setText(modelList.get(position).getDeath()); total.setText(modelList.get(position).getTotal()); incactive.setText(modelList.get(position).getIncAct()); inccured.setText(modelList.get(position).getIncRec()); incdeath.setText(modelList.get(position).getIncDec()); return view; } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import android.os.Bundle; import android.widget.ListView; 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.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { ListView listView; public static List<Model> modelList = new ArrayList<>(); Model model; Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); fetchData(); } private void fetchData() { // The Link Through Which We Can Fetch Data String url = "https://api.covid19india.org/state_district_wise.json"; StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { // Creating JSON Object JSONObject object = new JSONObject(response); // From that object we are fetching data JSONObject object1 = object.getJSONObject("Uttar Pradesh"); JSONObject object2 = object1.getJSONObject("districtData"); JSONObject object3 = object2.getJSONObject("Prayagraj"); JSONObject object4 = object3.getJSONObject("delta"); String active = object3.getString("active"); String confirmed = object3.getString("confirmed"); String deceased = object3.getString("deceased"); String recovered = object3.getString("recovered"); String confInc = object4.getString("confirmed"); String confDec = object4.getString("deceased"); String confRec = object4.getString("recovered"); model = new Model("Prayagraj", confirmed, deceased, recovered, active, confInc, confDec, confRec); // placing data into the app using AdapterClass modelList.add(model); // Creating JSON Object object3 = object2.getJSONObject("Ballia"); // From that object we are fetching data active = object3.getString("active"); confirmed = object3.getString("confirmed"); deceased = object3.getString("deceased"); recovered = object3.getString("recovered"); object4 = object3.getJSONObject("delta"); confInc = object4.getString("confirmed"); confDec = object4.getString("deceased"); confRec = object4.getString("recovered"); model = new Model("Ballia", confirmed, deceased, recovered, active, confInc, confDec, confRec); // placing data into the app using AdapterClass modelList.add(model); // Creating JSON Object object3 = object2.getJSONObject("Lucknow"); // From that object we are fetching data active = object3.getString("active"); confirmed = object3.getString("confirmed"); deceased = object3.getString("deceased"); recovered = object3.getString("recovered"); object4 = object3.getJSONObject("delta"); confInc = object4.getString("confirmed"); confDec = object4.getString("deceased"); confRec = object4.getString("recovered"); model = new Model("Lucknow", confirmed, deceased, recovered, active, confInc, confDec, confRec); // placing data into the app using AdapterClass modelList.add(model); // Creating JSON Object object3 = object2.getJSONObject("Varanasi"); // From that object we are fetching data active = object3.getString("active"); confirmed = object3.getString("confirmed"); deceased = object3.getString("deceased"); recovered = object3.getString("recovered"); object4 = object3.getJSONObject("delta"); confInc = object4.getString("confirmed"); confDec = object4.getString("deceased"); confRec = object4.getString("recovered"); model = new Model("Varanasi", confirmed, deceased, recovered, active, confInc, confDec, confRec); // placing data into the app using AdapterClass modelList.add(model); // Creating JSON Object object3 = object2.getJSONObject("Agra"); // From that object we are fetching data active = object3.getString("active"); confirmed = object3.getString("confirmed"); deceased = object3.getString("deceased"); recovered = object3.getString("recovered"); object4 = object3.getJSONObject("delta"); confInc = object4.getString("confirmed"); confDec = object4.getString("deceased"); confRec = object4.getString("recovered"); model = new Model("Agra", confirmed, deceased, recovered, active, confInc, confDec, confRec); // placing data into the app using AdapterClass modelList.add(model); adapter = new Adapter(MainActivity.this, modelList); listView.setAdapter(adapter); // In case of error it will run } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // In case of error it will run Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show(); } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(request); } } |