Back to project page SimpleWeather.
The source code is released under:
Apache License
If you think the Android project SimpleWeather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.eventbooking.simpleweather; /*from w w w . j a va 2s . c o m*/ import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class WeatherFragment extends Fragment { Typeface weatherFont; TextView cityField; TextView updatedField; TextView detailsField; TextView currentTemperatureField; TextView weaterIcon; Handler handler; public WeatherFragment() { handler = new Handler(); } @Override public void onCreate(Bundle saveInstanceState) { super.onCreate(saveInstanceState); weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/weather.ttf"); updateWeatherData(new CityPreference(getActivity()).getCity()); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_weather, container, false); cityField = (TextView) rootView.findViewById(R.id.city_field); updatedField = (TextView) rootView.findViewById(R.id.updated_field); detailsField = (TextView) rootView.findViewById(R.id.details_field); currentTemperatureField = (TextView) rootView.findViewById(R.id.current_temperature_field); weaterIcon = (TextView) rootView.findViewById(R.id.weather_icon); weaterIcon.setTypeface(weatherFont); return rootView; } private void updateWeatherData(final String city) { new Thread() { public void run() { final JSONObject json = RemoteFetch.getJson(getActivity(), city); if (json == null) { handler.post(new Runnable() { public void run() { Toast.makeText(getActivity(), getActivity().getString(R.string.place_not_found), Toast.LENGTH_LONG).show(); } }); } else { handler.post(new Runnable() { public void run() { renderWeather(json); } }); } } }.start(); } private void renderWeather(JSONObject json) { try { cityField.setText(json.getString("name").toUpperCase(Locale.US) + ", " + json.getJSONObject("sys").getString("country")); JSONObject details = json.getJSONArray("weather").getJSONObject(0); JSONObject main = json.getJSONObject("main"); detailsField.setText( details.getString("description").toUpperCase(Locale.US) + "\n" + "Humidity: " + main.getString("humidity") + "%" + "\n" + "Pressure: " + main.getString("pressure") + " hPa"); currentTemperatureField.setText(String.format("%.2f", main.getDouble("temp")) + " F"); DateFormat df = DateFormat.getDateTimeInstance(); String updatedOn = df.format(new Date(json.getLong("dt") * 1000)); updatedField.setText("Last update: " + updatedOn); setWeatherIcon(details.getInt("id"), json.getJSONObject("sys").getLong("sunrise") * 1000, json.getJSONObject("sys").getLong("sunset") * 1000); } catch (JSONException e) { Log.e("SimpleWeather", "The JSON Object could not be parsed."); } catch (Exception ed) { Log.e("SimpleWeather", "One ore more fields not found in the JSON data."); } } private void setWeatherIcon(int actualId, long sunrise, long sunset) { int id = actualId / 100; String icon = ""; if (actualId == 800) { long currentTime = new Date().getTime(); if (currentTime >= sunrise && currentTime < sunset) { icon = getActivity().getString(R.string.weather_sunny); } else { icon = getActivity().getString(R.string.weather_clear_night); } } else { switch (id) { case 2: icon = getActivity().getString(R.string.weather_thunder); break; case 3: icon = getActivity().getString(R.string.weather_drizzle); break; case 7: icon = getActivity().getString(R.string.weather_foggy); break; case 8: icon = getActivity().getString(R.string.weather_cloudy); break; case 6: icon = getActivity().getString(R.string.weather_snowy); break; case 5: icon = getActivity().getString(R.string.weather_rainy); break; } } weaterIcon.setText(icon); } public void changeCity(String city){ updateWeatherData(city); } }