From 5e6832ba0343179e7086388c562c5a8035f05a7e Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Tue, 1 Apr 2014 04:41:05 +0200 Subject: [PATCH] Android app weather info. Preferences, WIP. --- AndroidManifest.xml | 12 +- res/layout/activity_main.xml | 2 +- res/layout/fragment_main.xml | 2 +- res/menu/weather_main_menu.xml | 3 +- res/values/arrays.xml | 11 + res/values/strings.xml | 4 + res/xml/weather_preferences.xml | 13 ++ .../exampletdd/WeatherInformationActivity.java | 40 ++-- .../WeatherInformationPreferencesActivity.java | 19 ++ .../exampletdd/fragment/WeatherDataFragment.java | 208 ------------------- .../fragment/WeatherInformationDataFragment.java | 228 +++++++++++++++++++++ .../WeatherInformationPreferencesFragment.java | 57 ++++++ 12 files changed, 369 insertions(+), 230 deletions(-) create mode 100644 res/values/arrays.xml create mode 100644 res/xml/weather_preferences.xml create mode 100644 src/de/example/exampletdd/WeatherInformationPreferencesActivity.java delete mode 100644 src/de/example/exampletdd/fragment/WeatherDataFragment.java create mode 100644 src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java create mode 100644 src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 2a24350..a0f5d7b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -19,7 +19,6 @@ android:theme="@style/AppTheme" > @@ -28,6 +27,17 @@ + + + + + + + + diff --git a/res/layout/activity_main.xml b/res/layout/activity_main.xml index f6e05a5..e872532 100644 --- a/res/layout/activity_main.xml +++ b/res/layout/activity_main.xml @@ -10,7 +10,7 @@ android:id="@+id/weather_data_frag" android:layout_width="match_parent" android:layout_height="match_parent" - class="de.example.exampletdd.fragment.WeatherDataFragment" + class="de.example.exampletdd.fragment.WeatherInformationDataFragment" tools:layout="@layout/fragment_main" /> diff --git a/res/layout/fragment_main.xml b/res/layout/fragment_main.xml index 879d382..b110cec 100644 --- a/res/layout/fragment_main.xml +++ b/res/layout/fragment_main.xml @@ -6,7 +6,7 @@ android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" - tools:context="de.example.exampletdd.fragment.WeatherDataFragment" > + tools:context="de.example.exampletdd.fragment.WeatherInformationDataFragment" > + android:showAsAction="collapseActionView"> diff --git a/res/values/arrays.xml b/res/values/arrays.xml new file mode 100644 index 0000000..e38881b --- /dev/null +++ b/res/values/arrays.xml @@ -0,0 +1,11 @@ + + + + Celsius + Fahrenheit + + + Celsius + Fahrenheit + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 27fe592..5445332 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -23,5 +23,9 @@ Impossible to receive weather data. Icon weather Weather Information + weather_preferences_units + Fahrenheit + Celsius + Units of measurement diff --git a/res/xml/weather_preferences.xml b/res/xml/weather_preferences.xml new file mode 100644 index 0000000..3e2cbe0 --- /dev/null +++ b/res/xml/weather_preferences.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/src/de/example/exampletdd/WeatherInformationActivity.java b/src/de/example/exampletdd/WeatherInformationActivity.java index 569c903..e3c53c4 100644 --- a/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/src/de/example/exampletdd/WeatherInformationActivity.java @@ -6,13 +6,14 @@ import android.app.DialogFragment; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; +import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import de.example.exampletdd.activityinterface.ErrorMessage; import de.example.exampletdd.activityinterface.OnClickButtons; import de.example.exampletdd.fragment.ErrorDialogFragment; -import de.example.exampletdd.fragment.WeatherDataFragment; +import de.example.exampletdd.fragment.WeatherInformationDataFragment; public class WeatherInformationActivity extends Activity implements ErrorMessage { private OnClickButtons onclickButtons; @@ -21,32 +22,36 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); - - final ActionBar actionBar = getActionBar(); + + PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, + false); + + final ActionBar actionBar = this.getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); - actionBar.setTitle(getResources().getString(R.string.header_action_bar)); + actionBar.setTitle(this.getResources().getString(R.string.header_action_bar)); - // Better using xml files. + // Better using xml files? How to deal with savedInstanceState with xml files? // final WeatherDataFragment weatherDataFragment = new WeatherDataFragment(); // // if (savedInstanceState == null) { // this.getFragmentManager().beginTransaction() // .add(R.id.container, weatherDataFragment).commit(); // } - final WeatherDataFragment weatherDataFragment = (WeatherDataFragment) this + final WeatherInformationDataFragment weatherDataFragment = (WeatherInformationDataFragment) this .getFragmentManager().findFragmentById(R.id.weather_data_frag); this.onclickButtons = weatherDataFragment; + } @Override public boolean onCreateOptionsMenu(final Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - this.getMenuInflater().inflate(R.menu.main, menu); - return true; + this.getMenuInflater().inflate(R.menu.weather_main_menu, menu); + + return super.onCreateOptionsMenu(menu); } @Override @@ -56,14 +61,14 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage // as you specify a parent activity in AndroidManifest.xml. super.onOptionsItemSelected(item); switch (item.getItemId()) { - case R.id.weather_menu_settings: - final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO"). - setComponent(new ComponentName("de.example.exampletdd", - "de.example.exampletdd.WeatherInformationSettings")); - this.startActivity(intent); - return true; - default: - break; + case R.id.weather_menu_settings: + final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO"). + setComponent(new ComponentName("de.example.exampletdd", + "de.example.exampletdd.WeatherInformationPreferencesActivity")); + this.startActivity(intent); + return true; + default: + break; } return super.onOptionsItemSelected(item); @@ -79,4 +84,5 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage public void onClickGetWeather(final View v) { this.onclickButtons.onClickGetWeather(v); } + } diff --git a/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java b/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java new file mode 100644 index 0000000..3fe6e9a --- /dev/null +++ b/src/de/example/exampletdd/WeatherInformationPreferencesActivity.java @@ -0,0 +1,19 @@ +package de.example.exampletdd; + +import android.app.Activity; +import android.os.Bundle; +import de.example.exampletdd.fragment.WeatherInformationPreferencesFragment; + +public class WeatherInformationPreferencesActivity extends Activity { + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + this.getFragmentManager() + .beginTransaction() + .replace(android.R.id.content, + new WeatherInformationPreferencesFragment()).commit(); + } + +} diff --git a/src/de/example/exampletdd/fragment/WeatherDataFragment.java b/src/de/example/exampletdd/fragment/WeatherDataFragment.java deleted file mode 100644 index 1efe3fb..0000000 --- a/src/de/example/exampletdd/fragment/WeatherDataFragment.java +++ /dev/null @@ -1,208 +0,0 @@ -package de.example.exampletdd.fragment; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -import org.apache.http.client.ClientProtocolException; -import org.json.JSONException; - -import android.app.Activity; -import android.app.Fragment; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; -import android.os.AsyncTask; -import android.os.Bundle; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.EditText; -import android.widget.ImageView; -import de.example.exampletdd.R; -import de.example.exampletdd.activityinterface.ErrorMessage; -import de.example.exampletdd.activityinterface.OnClickButtons; -import de.example.exampletdd.httpclient.WeatherHTTPClient; -import de.example.exampletdd.model.WeatherData; -import de.example.exampletdd.parser.IJPOSWeatherParser; -import de.example.exampletdd.parser.JPOSWeatherParser; -import de.example.exampletdd.service.WeatherService; - -public class WeatherDataFragment extends Fragment implements OnClickButtons { - private Activity currentActivity; - EditText weatherDescription; - EditText temperature; - EditText maxTemperature; - EditText minTemperature; - EditText sunRise; - EditText sunSet; - ImageView imageIcon; - - - @Override - public View onCreateView(final LayoutInflater inflater, - final ViewGroup container, final Bundle savedInstanceState) { - final View rootView = inflater.inflate(R.layout.fragment_main, - container, false); - - this.currentActivity = this.getActivity(); - - this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription); - this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature); - this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature); - this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature); - this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise); - this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet); - this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon); - - return rootView; - } - - @Override - public void onClickGetWeather(final View v) { - - final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); - final WeatherService weatherService = new WeatherService( - JPOSWeatherParser); - final AndroidHttpClient httpClient = AndroidHttpClient - .newInstance("Android Weather Information Agent"); - final WeatherHTTPClient HTTPweatherClient = new WeatherHTTPClient( - httpClient); - - final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService); - - final EditText cityCountry = (EditText) this.getActivity() - .findViewById(R.id.editTextCity); - - weatherTask.execute(cityCountry.getText().toString()); - } - - public void updateWeatherData(final WeatherData weatherData) { - final DecimalFormat tempFormatter = new DecimalFormat("#####.#####"); - final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z"); - - if (weatherData.getWeather() != null) { - this.weatherDescription.setText(weatherData.getWeather() - .getDescription()); - double conversion = weatherData.getMain().getTemp(); - conversion = conversion - 273.15; - this.temperature.setText(tempFormatter.format(conversion)); - conversion = weatherData.getMain().getMaxTemp(); - conversion = conversion - 273.15; - this.maxTemperature.setText(tempFormatter.format(conversion)); - conversion = weatherData.getMain().getMinTemp(); - conversion = conversion - 273.15; - this.minTemperature.setText(tempFormatter.format(conversion)); - } - - if (weatherData.getSystem() != null) { - long unixTime = weatherData.getSystem().getSunRiseTime(); - Date unixDate = new Date(unixTime * 1000L); - String dateFormatUnix = dateFormat.format(unixDate); - this.sunRise.setText(dateFormatUnix); - - unixTime = weatherData.getSystem().getSunSetTime(); - unixDate = new Date(unixTime * 1000L); - dateFormatUnix = dateFormat.format(unixDate); - this.sunSet.setText(dateFormatUnix); - } - - if (weatherData.getIconData() != null) { - final Bitmap icon = BitmapFactory.decodeByteArray( - weatherData.getIconData(), 0, - weatherData.getIconData().length); - this.imageIcon.setImageBitmap(icon); - } - } - - public class WeatherTask extends AsyncTask { - private static final String TAG = "JSONWeatherTask"; - private final WeatherHTTPClient weatherHTTPClient; - private final WeatherService weatherService; - - public WeatherTask(final WeatherHTTPClient weatherHTTPClient, - final WeatherService weatherService) { - this.weatherHTTPClient = weatherHTTPClient; - this.weatherService = weatherService; - } - - @Override - protected WeatherData doInBackground(final Object... params) { - WeatherData weatherData = null; - - try { - weatherData = this.doInBackgroundThrowable(params); - } catch (final ClientProtocolException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - } catch (final MalformedURLException e) { - Log.e(TAG, "Syntax URL exception: ", e); - } catch (final URISyntaxException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - } catch (final IOException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - } catch (final JSONException e) { - Log.e(TAG, "WeatherService exception: ", e); - } finally { - this.weatherHTTPClient.close(); - } - - return weatherData; - } - - @Override - protected void onPostExecute(final WeatherData weatherData) { - if (weatherData != null) { - WeatherDataFragment.this.updateWeatherData(weatherData); - } else { - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); - } - - this.weatherHTTPClient.close(); - } - - @Override - protected void onCancelled(final WeatherData weatherData) { - this.onCancelled(); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_connection_tiemout); - - this.weatherHTTPClient.close(); - } - - private WeatherData doInBackgroundThrowable(final Object... params) - throws ClientProtocolException, MalformedURLException, - URISyntaxException, IOException, JSONException { - final String cityCountry = (String) params[0]; - final String urlAPICity = WeatherDataFragment.this.getResources() - .getString(R.string.uri_api_city); - final String APIVersion = WeatherDataFragment.this.getResources() - .getString(R.string.api_version); - String url = this.weatherService.createURIAPICityCountry( - cityCountry, urlAPICity, APIVersion); - - - final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url)); - - - final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData); - - - final String icon = weatherData.getWeather().getIcon(); - final String urlAPIicon = WeatherDataFragment.this - .getResources().getString(R.string.uri_api_icon); - url = this.weatherService.createURIAPIicon(icon, urlAPIicon); - final byte[] iconData = this.weatherHTTPClient - .retrieveDataFromAPI(new URL(url)).toByteArray(); - weatherData.setIconData(iconData); - - - return weatherData; - } - } -} diff --git a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java new file mode 100644 index 0000000..9e47ce1 --- /dev/null +++ b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java @@ -0,0 +1,228 @@ +package de.example.exampletdd.fragment; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.http.client.ClientProtocolException; +import org.json.JSONException; + +import android.app.Fragment; +import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.http.AndroidHttpClient; +import android.os.AsyncTask; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.ImageView; +import de.example.exampletdd.R; +import de.example.exampletdd.activityinterface.ErrorMessage; +import de.example.exampletdd.activityinterface.OnClickButtons; +import de.example.exampletdd.httpclient.WeatherHTTPClient; +import de.example.exampletdd.model.WeatherData; +import de.example.exampletdd.parser.IJPOSWeatherParser; +import de.example.exampletdd.parser.JPOSWeatherParser; +import de.example.exampletdd.service.WeatherService; + +public class WeatherInformationDataFragment extends Fragment implements OnClickButtons { + private boolean isFahrenheit; + private EditText weatherDescription; + private EditText temperature; + private EditText maxTemperature; + private EditText minTemperature; + private EditText sunRise; + private EditText sunSet; + private ImageView imageIcon; + + + @Override + public View onCreateView(final LayoutInflater inflater, + final ViewGroup container, final Bundle savedInstanceState) { + final View rootView = inflater.inflate(R.layout.fragment_main, + container, false); + + + this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription); + this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature); + this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature); + this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature); + this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise); + this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet); + this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon); + + return rootView; + } + + @Override + public void onClickGetWeather(final View v) { + + final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); + final WeatherService weatherService = new WeatherService( + JPOSWeatherParser); + final AndroidHttpClient httpClient = AndroidHttpClient + .newInstance("Android Weather Information Agent"); + final WeatherHTTPClient HTTPweatherClient = new WeatherHTTPClient( + httpClient); + + final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService); + + final EditText cityCountry = (EditText) this.getActivity() + .findViewById(R.id.editTextCity); + + weatherTask.execute(cityCountry.getText().toString()); + } + + public void updateWeatherData(final WeatherData weatherData) { + final DecimalFormat tempFormatter = new DecimalFormat("#####.#####"); + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z"); + final double tempUnits = this.isFahrenheit ? 0 : 273.15; + + if (weatherData.getWeather() != null) { + this.weatherDescription.setText(weatherData.getWeather() + .getDescription()); + double conversion = weatherData.getMain().getTemp(); + conversion = conversion - tempUnits; + this.temperature.setText(tempFormatter.format(conversion)); + conversion = weatherData.getMain().getMaxTemp(); + conversion = conversion - tempUnits; + this.maxTemperature.setText(tempFormatter.format(conversion)); + conversion = weatherData.getMain().getMinTemp(); + conversion = conversion - tempUnits; + this.minTemperature.setText(tempFormatter.format(conversion)); + } + + if (weatherData.getSystem() != null) { + long unixTime = weatherData.getSystem().getSunRiseTime(); + Date unixDate = new Date(unixTime * 1000L); + String dateFormatUnix = dateFormat.format(unixDate); + this.sunRise.setText(dateFormatUnix); + + unixTime = weatherData.getSystem().getSunSetTime(); + unixDate = new Date(unixTime * 1000L); + dateFormatUnix = dateFormat.format(unixDate); + this.sunSet.setText(dateFormatUnix); + } + + if (weatherData.getIconData() != null) { + final Bitmap icon = BitmapFactory.decodeByteArray( + weatherData.getIconData(), 0, + weatherData.getIconData().length); + this.imageIcon.setImageBitmap(icon); + } + } + + @Override + public void onResume() { + super.onResume(); + + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this.getActivity()); + + final String unitsKey = this.getResources().getString( + R.string.weather_preferences_units_key); + final String units = sharedPreferences.getString(unitsKey, ""); + final String celsius = this.getResources().getString( + R.string.weather_preferences_units_celsius); + if (units.equals(celsius)) { + this.isFahrenheit = false; + } else { + this.isFahrenheit = true; + } + } + + public class WeatherTask extends AsyncTask { + private static final String TAG = "JSONWeatherTask"; + private final WeatherHTTPClient weatherHTTPClient; + private final WeatherService weatherService; + + public WeatherTask(final WeatherHTTPClient weatherHTTPClient, + final WeatherService weatherService) { + this.weatherHTTPClient = weatherHTTPClient; + this.weatherService = weatherService; + } + + @Override + protected WeatherData doInBackground(final Object... params) { + WeatherData weatherData = null; + + try { + weatherData = this.doInBackgroundThrowable(params); + } catch (final ClientProtocolException e) { + Log.e(TAG, "WeatherHTTPClient exception: ", e); + } catch (final MalformedURLException e) { + Log.e(TAG, "Syntax URL exception: ", e); + } catch (final URISyntaxException e) { + Log.e(TAG, "WeatherHTTPClient exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "WeatherHTTPClient exception: ", e); + } catch (final JSONException e) { + Log.e(TAG, "WeatherService exception: ", e); + } finally { + this.weatherHTTPClient.close(); + } + + return weatherData; + } + + @Override + protected void onPostExecute(final WeatherData weatherData) { + if (weatherData != null) { + WeatherInformationDataFragment.this.updateWeatherData(weatherData); + } else { + ((ErrorMessage) WeatherInformationDataFragment.this.getActivity()) + .createErrorDialog(R.string.error_dialog_generic_error); + } + + this.weatherHTTPClient.close(); + } + + @Override + protected void onCancelled(final WeatherData weatherData) { + this.onCancelled(); + ((ErrorMessage) WeatherInformationDataFragment.this.getActivity()) + .createErrorDialog(R.string.error_dialog_connection_tiemout); + + this.weatherHTTPClient.close(); + } + + private WeatherData doInBackgroundThrowable(final Object... params) + throws ClientProtocolException, MalformedURLException, + URISyntaxException, IOException, JSONException { + final String cityCountry = (String) params[0]; + final String urlAPICity = WeatherInformationDataFragment.this.getResources() + .getString(R.string.uri_api_city); + final String APIVersion = WeatherInformationDataFragment.this.getResources() + .getString(R.string.api_version); + String url = this.weatherService.createURIAPICityCountry( + cityCountry, urlAPICity, APIVersion); + + + final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url)); + + + final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData); + + + final String icon = weatherData.getWeather().getIcon(); + final String urlAPIicon = WeatherInformationDataFragment.this + .getResources().getString(R.string.uri_api_icon); + url = this.weatherService.createURIAPIicon(icon, urlAPIicon); + final byte[] iconData = this.weatherHTTPClient + .retrieveDataFromAPI(new URL(url)).toByteArray(); + weatherData.setIconData(iconData); + + + return weatherData; + } + } +} diff --git a/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java new file mode 100644 index 0000000..d48747c --- /dev/null +++ b/src/de/example/exampletdd/fragment/WeatherInformationPreferencesFragment.java @@ -0,0 +1,57 @@ +package de.example.exampletdd.fragment; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import de.example.exampletdd.R; + +public class WeatherInformationPreferencesFragment extends PreferenceFragment +implements OnSharedPreferenceChangeListener { + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Load the preferences from an XML resource + this.addPreferencesFromResource(R.xml.weather_preferences); + + final String unitsKey = this.getResources().getString( + R.string.weather_preferences_units_key); + final Preference connectionPref = this.findPreference(unitsKey); + this.getPreferenceManager().getSharedPreferences() + .getString(unitsKey, ""); + connectionPref.setSummary(this.getPreferenceManager() + .getSharedPreferences().getString(unitsKey, "")); + } + + @Override + public void onResume() { + super.onResume(); + this.getPreferenceManager().getSharedPreferences() + .registerOnSharedPreferenceChangeListener(this); + + } + + @Override + public void onPause() { + super.onPause(); + this.getPreferenceManager().getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public void onSharedPreferenceChanged( + final SharedPreferences sharedPreferences, final String key) { + final String unitsKey = this.getResources().getString( + R.string.weather_preferences_units_key); + + if (key.equals(unitsKey)) { + final Preference connectionPref = this.findPreference(key); + connectionPref.setSummary(sharedPreferences.getString(key, "")); + } + + } + +} -- 2.1.4