Skip to content

Instantly share code, notes, and snippets.

@saik0
Created November 14, 2012 18:23
Show Gist options
  • Save saik0/4073835 to your computer and use it in GitHub Desktop.
Save saik0/4073835 to your computer and use it in GitHub Desktop.
How to find preferences and setup listeners in UnifiedPreference
/*
** 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;
}
};
}
@gautamjain
Copy link

Actually, in the case of the example above, I suppose you could change the onCreate method that's giving the problem to the following:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ((DemoUnifiedPreferenceActivity) getActivity()).initGeneralPreferences();
}

@tarunmahe
Copy link

How to change the preference summary? I am not able to change programatically using setSummary function.

@niranjan94
Copy link

I tried this .. But, i never get any notification nor any error .. What have i done wrong here ?

Thanks in Advance !

package com.njlabs.example;

import net.saik0.android.unifiedpreference.UnifiedPreferenceFragment;
import net.saik0.android.unifiedpreference.UnifiedSherlockPreferenceActivity;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.widget.Toast;

/**
 * @author Niranjan
 */
public class preferences extends UnifiedSherlockPreferenceActivity implements OnSharedPreferenceChangeListener  {

    @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("preferences");
        setSharedPreferencesMode(Context.MODE_PRIVATE);
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

    }

    public static class PersonalDetailsFragment extends UnifiedPreferenceFragment {}

    public static class AmritaExplorerFragment extends UnifiedPreferenceFragment {}

    public static class LibrariesFragment 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;
            }
        };      
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; go home
                finish();
                overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
        // TODO Auto-generated method stub

    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment