Created
November 14, 2012 18:23
-
-
Save saik0/4073835 to your computer and use it in GitHub Desktop.
How to find preferences and setup listeners in UnifiedPreference
This file contains hidden or 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 2012, Joel Pedraza | |
** | |
** 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 net.saik0.android.unifiedpreference.demo; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.preference.EditTextPreference; | |
import android.preference.Preference; | |
import android.widget.Toast; | |
import net.saik0.android.unifiedpreference.demo.R; | |
import net.saik0.android.unifiedpreference.UnifiedPreferenceFragment; | |
import net.saik0.android.unifiedpreference.UnifiedSherlockPreferenceActivity; | |
public class DemoUnifiedPreferenceActivity extends UnifiedSherlockPreferenceActivity { | |
@Override public void onCreate(Bundle savedInstanceState) { | |
// Set header resource MUST BE CALLED BEFORE super.onCreate | |
setHeaderRes(R.xml.pref_headers); | |
// Set desired preference file and mode (optional) | |
setSharedPreferencesName("unified_preference_demo"); | |
setSharedPreferencesMode(Context.MODE_PRIVATE); | |
super.onCreate(savedInstanceState); | |
} | |
private void initGeneralPreferences() { | |
EditTextPreference exampleText = (EditTextPreference) findPreference("example_text"); | |
exampleText.setOnPreferenceClickListener(exampleChangeListener); | |
} | |
@Override | |
@SuppressWarnings("deprecation") | |
protected void onPostCreate(Bundle savedInstanceState) { | |
super.onPostCreate(savedInstanceState); | |
/* | |
* We must first test if single pane, the lifecycle method is always called whether or not | |
* the Activity is managing the preferences. (The legacy way) | |
*/ | |
if (isSinglePane()) { | |
// Set a listener for the activity | |
initGeneralPreferences(); | |
} | |
} | |
public static class GeneralPreferenceFragment extends UnifiedPreferenceFragment { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Set a listener for a fragment | |
initGeneralPreferences(); | |
} | |
} | |
public static class NotificationPreferenceFragment extends UnifiedPreferenceFragment { | |
// This fragment does not contain the example_text preference, so we do nothing. | |
} | |
public static class DataSyncPreferenceFragment extends UnifiedPreferenceFragment {} | |
/** | |
* A sample listener that shows the key of the Preference that was clicked. It does not consume the click event. | |
*/ | |
private static Preference.OnPreferenceClickListener exampleChangeListener = new Preference.OnPreferenceClickListener() { | |
@Override | |
public boolean onPreferenceClick(Preference preference) { | |
Toast.makeText(preference.getContext(), preference.getKey() + " clicked", Toast.LENGTH_SHORT).show(); | |
return false; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried this .. But, i never get any notification nor any error .. What have i done wrong here ?
Thanks in Advance !