Created
June 12, 2018 22:40
-
-
Save RaminMammadzada/d39f90685e8429b02653791834bb0f7e to your computer and use it in GitHub Desktop.
NewsApp - SettingsActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2016 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example.android.newsappstage1; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import android.preference.ListPreference; | |
import android.preference.Preference; | |
import android.preference.PreferenceFragment; | |
import android.preference.PreferenceManager; | |
import android.support.v7.app.AppCompatActivity; | |
public class SettingsActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.settings_activity); | |
} | |
public static class NewsPreferenceFragment extends PreferenceFragment | |
implements Preference.OnPreferenceChangeListener { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.settings_main); | |
Preference minMagnitude = findPreference(getString(R.string.settings_min_magnitude_key)); | |
bindPreferenceSummaryToValue(minMagnitude); | |
Preference orderBy = findPreference(getString(R.string.settings_order_by_key)); | |
bindPreferenceSummaryToValue(orderBy); | |
} | |
@Override | |
public boolean onPreferenceChange(Preference preference, Object value) { | |
String stringValue = value.toString(); | |
if (preference instanceof ListPreference) { | |
ListPreference listPreference = (ListPreference) preference; | |
int prefIndex = listPreference.findIndexOfValue(stringValue); | |
if (prefIndex >= 0) { | |
CharSequence[] labels = listPreference.getEntries(); | |
preference.setSummary(labels[prefIndex]); | |
} | |
} else { | |
preference.setSummary(stringValue); | |
} | |
return true; | |
} | |
private void bindPreferenceSummaryToValue(Preference preference) { | |
preference.setOnPreferenceChangeListener(this); | |
SharedPreferences preferences = | |
PreferenceManager.getDefaultSharedPreferences(preference.getContext()); | |
String preferenceString = preferences.getString(preference.getKey(), ""); | |
onPreferenceChange(preference, preferenceString); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment