Permalink
Browse files

Done S06.02-SettingsFragment

  • Loading branch information...
GinaHsu committed Sep 1, 2017
1 parent 50c3aca commit be411f1520b53e025b59e2bb23f0756b8439ca52
@@ -21,10 +21,12 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
compile 'com.android.support:appcompat-v7:25.1.0'

compile 'com.android.support:recyclerview-v7:25.0.1'
// TODO (1) Add preferences dependency to gradle
compile 'com.android.support:preference-v7:25.0.1'

// Instrumentation dependencies use androidTestCompile
// (as opposed to testCompile for local unit tests run in the JVM)
@@ -29,27 +29,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_settings);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// TODO (2) Create an xml resource directory
// TODO (3) Add a PreferenceScreen with an EditTextPreference and ListPreference within the newly created xml resource directory

// TODO (4) Create SettingsFragment and extend PreferenceFragmentCompat

// Do steps 5 - 11 within SettingsFragment
// TODO (10) Implement OnSharedPreferenceChangeListener from SettingsFragment

// TODO (8) Create a method called setPreferenceSummary that accepts a Preference and an Object and sets the summary of the preference

// TODO (5) Override onCreatePreferences and add the preference xml file using addPreferencesFromResource

// Do step 9 within onCreatePreference
// TODO (9) Set the preference summary on each preference that isn't a CheckBoxPreference

// TODO (13) Unregister SettingsFragment (this) as a SharedPreferenceChangedListener in onStop

// TODO (12) Register SettingsFragment (this) as a SharedPreferenceChangedListener in onStart

// TODO (11) Override onSharedPreferenceChanged to update non CheckBoxPreferences when they are changed
}

@Override
@@ -0,0 +1,89 @@
package com.example.android.sunshine;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.preference.CheckBoxPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;


/**
* Created by Gina on 8/30/17.
*/

// TODO (4) Create SettingsFragment and extend PreferenceFragmentCompat
// TODO (10) Implement OnSharedPreferenceChangeListener from SettingsFragment

public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener{

// TODO (5) Override onCreatePreferences and add the preference xml file using addPreferencesFromResource
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
/* Add 'general' preferences, defined in the XML file */
addPreferencesFromResource(R.xml.pref_general);

// Do step 9 within onCreatePreference
// TODO (9) Set the preference summary on each preference that isn't a CheckBoxPreference
SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
PreferenceScreen preferenceScreen = getPreferenceScreen();

int count = preferenceScreen.getPreferenceCount();
for (int i = 0 ; i < count ; i++){
Preference p = preferenceScreen.getPreference(i);
if( !(p instanceof CheckBoxPreference)){
String value = sharedPreferences.getString(p.getKey(),"");
setPreferenceSummary(p, value);
}
}
}

// TODO (11) Override onSharedPreferenceChanged to update non CheckBoxPreferences when they are changed
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference preference = findPreference(key);
if(preference != null){
if(!(preference instanceof CheckBoxPreference)){
setPreferenceSummary(preference,sharedPreferences.getString(key,""));
}
}

}

// TODO (8) Create a method called setPreferenceSummary that accepts a Preference and an Object and sets the summary of the preference
private void setPreferenceSummary(Preference preference,Object value){
String stringValue = value.toString();
String key = preference.getKey();

/* For list preferences, set the summary to the correct display value in
the preference's 'entries' list (since they have separate labels/values). */
if(preference instanceof ListPreference){
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if(prefIndex >= 0){
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
}else{
preference.setSummary(stringValue);
}
}

// TODO (12) Register SettingsFragment (this) as a SharedPreferenceChangedListener in onStart
// TODO (13) Unregister SettingsFragment (this) as a SharedPreferenceChangedListener in onStop
@Override
public void onStart() {
super.onStart();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onStop() {
super.onStop();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}




@@ -15,7 +15,8 @@
limitations under the License.
-->
<!-- TODO (7) Make the SettingsFragment the root layout for SettingsActivity -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/weather_settings_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.android.sunshine.SettingsFragment"
android:id="@+id/weather_settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
@@ -23,6 +23,8 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--TODO (6) Add preferenceTheme to AppTheme and set to @style/PreferenceThemeOverlay-->
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

</style>

</resources>
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--TODO (2) Create an xml resource directory
TODO (3) Add a PreferenceScreen with an EditTextPreference and ListPreference within the newly created xml resource directory -->

<EditTextPreference
android:defaultValue="@string/pref_location_default"
android:key="@string/pref_location_key"
android:inputType="text"
android:singleLine="true"
android:title="@string/pref_location_label" />

<ListPreference
android:defaultValue="@string/pref_units_metric"
android:key="@string/pref_units_key"
android:entries="@array/pref_units_options"
android:entryValues="@array/pref_units_values"
android:title="@string/pref_units_label" />

</PreferenceScreen>

0 comments on commit be411f1

Please sign in to comment.