diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 53e3bae..90005f9 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -6,7 +6,6 @@
-
time, temp, speed, image;
@@ -91,7 +79,6 @@ protected void onCreate(Bundle savedInstanceState) {
loaderProgressBar = (ProgressBar)findViewById(R.id.loaderProgressBar);
contentRelativeLayout = (RelativeLayout)findViewById(R.id.contentRelativeLayout);
- noConnectionRelativeLayout = (RelativeLayout)findViewById(R.id.noConnectionRelativeLayout);
searchView = (SearchView)findViewById(R.id.searchView);
degreeTextView = (TextView)findViewById(R.id.deqreeTextView);
conditionTextView = (TextView)findViewById(R.id.conditionTextView);
@@ -102,10 +89,6 @@ protected void onCreate(Bundle savedInstanceState) {
backgroundImageView = (ImageView)findViewById(R.id.backgroundImageView);
localTimeTextView = (TextView)findViewById(R.id.localTimeTextView);
- btnTryAgain = (Button)findViewById(R.id.button);
-
- handler = new Handler();
-
recyclerView = (RecyclerView)findViewById(R.id.cardsRecycleView);
recyclerView.setHasFixedSize(true);
layoutManagerRecycleView = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
@@ -119,10 +102,27 @@ protected void onCreate(Bundle savedInstanceState) {
// Used for controlling location updates
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- // Request location permissions from device and access location
- getLocation();
+ // Check permission for location api
+ if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
+ PackageManager.PERMISSION_GRANTED &&
+ ActivityCompat.checkSelfPermission(this,android.Manifest.permission.
+ ACCESS_COARSE_LOCATION) !=
+ PackageManager.PERMISSION_GRANTED){
+ // Ask for permission
+ ActivityCompat.requestPermissions(MainActivity.this,
+ new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
+ android.Manifest.permission.ACCESS_COARSE_LOCATION},
+ PERMISSION_CODE);
+ }
- checkConnection();
+ try {
+ /* This takes last location provided by network, if not have no information about location,
+ will be returned null */
+ location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+ }
+ catch (Exception ex){
+ Log.i("PERMISSION ERROR", ex.getMessage());
+ }
if(location != null)
cityName = localization.getCityName(location.getLongitude(), location.getLatitude(),
@@ -139,7 +139,6 @@ public boolean onQueryTextSubmit(String query) {
contentRelativeLayout.setVisibility(View.GONE);
loaderProgressBar.setVisibility(View.VISIBLE);
- cityName = query;
getAndShowWeatherData(weatherAPI.requestUrl(query));
return false;
}
@@ -149,16 +148,6 @@ public boolean onQueryTextChange(String newText) {
return false;
}
});
-
- btnTryAgain.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- noConnectionRelativeLayout.setVisibility(View.GONE);
- loaderProgressBar.setVisibility(View.VISIBLE);
- getLocation();
- getAndShowWeatherData(weatherAPI.requestUrl(cityName));
- }
- });
}
// Handle user choice
@@ -175,131 +164,83 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
}
}
- void getLocation(){
- // Check permission for location api
- if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
- PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this,android.Manifest.permission.
- ACCESS_COARSE_LOCATION) !=
- PackageManager.PERMISSION_GRANTED){
- // Ask for permission
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
- android.Manifest.permission.ACCESS_COARSE_LOCATION},
- PERMISSION_CODE);
- }
+ void getAndShowWeatherData(String url){
+ RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
+ formatter = new Formatter();
- try {
- /* This takes last location provided by network, if not have no information about location,
- will be returned null */
- location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- catch (Exception ex){
- Log.i("PERMISSION ERROR", ex.getMessage());
- }
- }
+ JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
+ new Response.Listener() {
+ @Override
+ public void onResponse(JSONObject response) {
+ // Hide progress bar.
+ loaderProgressBar.setVisibility(View.GONE);
+ // Show app content
+ contentRelativeLayout.setVisibility(View.VISIBLE);
+ try {
+ // Get JSON objects from server response
+ JSONObject location = response.getJSONObject("location");
+ JSONObject current = response.getJSONObject("current");
+ JSONObject forecastday = response.getJSONObject("forecast").getJSONArray("forecastday")
+ .getJSONObject(0);
+ JSONArray hour = forecastday.getJSONArray("hour");
+
+ // Setting data to all views.
+ cityTextView.setText(location.getString("name"));
+ degreeTextView.setText(current.getString("temp_c")+"°c");
+ conditionTextView.setText(current.getJSONObject("condition").getString("text"));
+ regionTextView.setText(location.getString("region"));
+ countryTextView.setText(location.getString("country"));
+ localTimeTextView.setText(formatter.dateTimeFormatting(location.getString("localtime")));
+
+ // Load the image from url using picasso library.
+ if(current.getInt("is_day") == 0)
+ Picasso.with(MainActivity.this).load(nightBG).into(backgroundImageView);
+ else
+ Picasso.with(MainActivity.this).load(dayBG).into(backgroundImageView);
+
+ Picasso.with(MainActivity.this).load("https:"+current.getJSONObject("condition")
+ .getString("icon"))
+ .into(conditionIconImageView);
+
+ time = new ArrayList<>();
+ temp = new ArrayList<>();
+ speed = new ArrayList<>();
+ image = new ArrayList<>();
+
+ for(int i = 0;i < hour.length();i++){
+ JSONObject hourObj = hour.getJSONObject(i);
+ String timeObj = hourObj.getString("time");
+ String temperature = hourObj.getString("temp_c");
+ String imageObj = "https:"+hourObj.getJSONObject("condition").getString("icon");
+ String wind = hourObj.getString("wind_kph");
+
+ time.add(timeObj);
+ temp.add(temperature);
+ speed.add(wind);
+ image.add(imageObj);
+ }
+
+ adapterRecycleView = new CardView(time, temp, speed, image);
+ recyclerView.setAdapter(adapterRecycleView);
+
+ } catch (JSONException e) {
+
+ e.printStackTrace();
+ }
+ }
+ }, new Response.ErrorListener() {
- void checkConnection() {
- // ConnectivityManager for connection checking
- connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- // Check the internet connection
- connected = Connection.checkConnection(connectivityManager);
- Log.i("ACCESS NETWORK STATE", "Connected: " + connected);
- }
+ @Override
+ public void onErrorResponse(VolleyError error) {
- void getAndShowWeatherData(final String url){
- checkConnection();
-
- handler = new Handler();
- handler.postDelayed(new Runnable() {
- public void run() {
- if(connected){
- RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
- formatter = new Formatter();
-
- JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
- new Response.Listener() {
- @Override
- public void onResponse(JSONObject response) {
- // Hide progress bar.
- loaderProgressBar.setVisibility(View.GONE);
- // Show app content
- contentRelativeLayout.setVisibility(View.VISIBLE);
- try {
- // Get JSON objects from server response
- JSONObject location = response.getJSONObject("location");
- JSONObject current = response.getJSONObject("current");
- JSONObject forecastday = response.getJSONObject("forecast").getJSONArray("forecastday")
- .getJSONObject(0);
- JSONArray hour = forecastday.getJSONArray("hour");
-
- // Setting data to all views.
- cityTextView.setText(location.getString("name"));
- degreeTextView.setText(current.getString("temp_c")+"°c");
- conditionTextView.setText(current.getJSONObject("condition").getString("text"));
- regionTextView.setText(location.getString("region"));
- countryTextView.setText(location.getString("country"));
- localTimeTextView.setText(formatter.dateTimeFormatting(location.getString("localtime")));
-
- // Load the image from url using picasso library.
- if(current.getInt("is_day") == 0)
- Picasso.with(MainActivity.this).load(nightBG).into(backgroundImageView);
- else
- Picasso.with(MainActivity.this).load(dayBG).into(backgroundImageView);
-
- Picasso.with(MainActivity.this).load("https:"+current.getJSONObject("condition")
- .getString("icon"))
- .into(conditionIconImageView);
-
- time = new ArrayList<>();
- temp = new ArrayList<>();
- speed = new ArrayList<>();
- image = new ArrayList<>();
-
- for(int i = 0;i < hour.length();i++){
- JSONObject hourObj = hour.getJSONObject(i);
- String timeObj = hourObj.getString("time");
- String temperature = hourObj.getString("temp_c");
- String imageObj = "https:"+hourObj.getJSONObject("condition").getString("icon");
- String wind = hourObj.getString("wind_kph");
-
- time.add(timeObj);
- temp.add(temperature);
- speed.add(wind);
- image.add(imageObj);
- }
-
- adapterRecycleView = new CardView(time, temp, speed, image);
- recyclerView.setAdapter(adapterRecycleView);
-
- } catch (JSONException e) {
-
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
-
- @Override
- public void onErrorResponse(VolleyError error) {
- loaderProgressBar.setVisibility(View.GONE);
- contentRelativeLayout.setVisibility(View.VISIBLE);
- Toast.makeText(MainActivity.this, "No matching location found.", Toast.LENGTH_LONG).show();
- }
- });
-
- queue.add(jsonObjectRequest);
- }
- else{
- loaderProgressBar.setVisibility(View.GONE);
- noConnectionRelativeLayout.setVisibility(View.VISIBLE);
- Toast.makeText(getApplicationContext(), "Unable to get weather information.", Toast.LENGTH_LONG).show();
- }
+ loaderProgressBar.setVisibility(View.GONE);
+ contentRelativeLayout.setVisibility(View.VISIBLE);
+
+ Toast.makeText(MainActivity.this, "No matching location found.", Toast.LENGTH_LONG).show();
}
- }, 2000); //2 seconds
- }
+ });
+
+ queue.add(jsonObjectRequest);
- @Override
- protected void onResume() {
- super.onResume();
}
}
diff --git a/app/src/main/java/com/miguelf03kai/weatherapp/utils/Connection.java b/app/src/main/java/com/miguelf03kai/weatherapp/utils/Connection.java
deleted file mode 100644
index c29cbb2..0000000
--- a/app/src/main/java/com/miguelf03kai/weatherapp/utils/Connection.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package com.miguelf03kai.weatherapp.utils;
-
-import android.net.ConnectivityManager;
-import android.net.NetworkInfo;
-
-public class Connection {
-
- // Check if the device have internet connection
- public static boolean checkConnection(ConnectivityManager connectivityManager){
- boolean connected = (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
- .getState() == NetworkInfo.State.CONNECTED || connectivityManager
- .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED);
-
- return connected;
- }
-}
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 01335ad..2e28d77 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -168,40 +168,4 @@
-
-
-
-
-
-
-
-
-
diff --git a/app/src/main/res/mipmap-hdpi/ic_no_connection.png b/app/src/main/res/mipmap-hdpi/ic_no_connection.png
deleted file mode 100644
index 21ee25e..0000000
Binary files a/app/src/main/res/mipmap-hdpi/ic_no_connection.png and /dev/null differ
diff --git a/app/src/main/res/mipmap-mdpi/ic_no_connection.png b/app/src/main/res/mipmap-mdpi/ic_no_connection.png
deleted file mode 100644
index 02be513..0000000
Binary files a/app/src/main/res/mipmap-mdpi/ic_no_connection.png and /dev/null differ
diff --git a/app/src/main/res/mipmap-xhdpi/ic_no_connection.png b/app/src/main/res/mipmap-xhdpi/ic_no_connection.png
deleted file mode 100644
index 90c10d8..0000000
Binary files a/app/src/main/res/mipmap-xhdpi/ic_no_connection.png and /dev/null differ
diff --git a/app/src/main/res/mipmap-xxhdpi/ic_no_connection.png b/app/src/main/res/mipmap-xxhdpi/ic_no_connection.png
deleted file mode 100644
index 7d35539..0000000
Binary files a/app/src/main/res/mipmap-xxhdpi/ic_no_connection.png and /dev/null differ
diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_no_connection.png b/app/src/main/res/mipmap-xxxhdpi/ic_no_connection.png
deleted file mode 100644
index f67cbac..0000000
Binary files a/app/src/main/res/mipmap-xxxhdpi/ic_no_connection.png and /dev/null differ