android - Can't get preference set with PreferenceActivity -
i have main activity:
public class home extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.home); initializestyle(); } private void initializestyle() { sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(this); string foobar = prefs.getstring("foobar", "hi"); log.i("myactivity", foobar); } // ... }
and preferenceactivity:
public class preferences extends preferenceactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences); } }
with xml:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:key="preferences_font" android:title="@string/preferences_font"> <edittextpreference android:title="@string/preferences_font_size" android:key="foobar" android:defaultvalue="12"> </edittextpreference> </preferencecategory> </preferencescreen>
log.i prints "hi", means preference doesn't exist or named badly.
any idea i've done wrong?
regards,
this might on track , having working preferences:
xml file: preferences.xml
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="preferences font"> <edittextpreference android:title="select font size" android:key="foobar" android:summary="enter font size" android:defaultvalue="12" /> </preferencecategory> </preferencescreen>
java file 1: home.java
public class home extends activity { public sharedpreferences prefs; static public string fontpref; //you can make int or whatever need static public string myvalue; //to store new pref value /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { setcontentview(r.layout.home); initializestyle(); } private void initializestyle() { prefs = preferencemanager.getdefaultsharedpreferences(this); fontpref = prefs.getstring("foobar", "hi"); //this doesn't change value myvalue = "this test"; log.i("myactivity", foobar); //won't show change yet } }
java file 2: prefernce.java
import android.preference.preference; import android.preference.preferenceactivity; import android.preference.preferencemanager; //... , other imports public class preferences extends preferenceactivity implements onsharedpreferencechangelistener{ private sharedpreferences prefs; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences); //add other stuff here if need, if might have //prefs use buttons , need set onclicklistener 1 } @override public void onsharedpreferencechanged(sharedpreferences sharedpref, string key) { editor editor = sharedpref.edit(); if(key.equalsignorecase("foobar")){ home.fontpref = sharedpref.getstring("foobar", "some value"); editor.putstring("foobar", home.myvalue); //where ever or whatever new value } editor.commit(); log.i("prefsactivity", foobar); //should show change } }
hopefully may on right track. of course there many ways these things so, luck hope helps.
Comments
Post a Comment