android - Implementing PreferenceFragment? -


i've been struggling implement preferencefragment in app. goal have preferences view replace main_activity fragment container can keep same nav drawer, action bar, etc.

i have created preference fragment class so:

public class madlibssettings extends preferencefragment {  android.support.v7.app.actionbar actionbar; checkboxpreference themeswitch; listpreference fontsize;   @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view view = super.oncreateview(inflater, container, savedinstancestate);     view.setbackgroundcolor(getresources().getcolor(android.r.color.white));      actionbar = (android.support.v7.app.actionbar) ((mainactivity)getactivity()).getsupportactionbar();     actionbar.settitle("settings");      addpreferencesfromresource(r.layout.madlibs_settings);      //fontsize = (listpreference) findpreference("list");      return view; } } 

and prefs in r.layout.madlibs_settings are:

<preferencecategory android:title="preferencecategory a" >     <checkboxpreference         android:id="@+id/checkbox1"         android:defaultvalue="false"         android:key="checkbox1"         android:summary="switches app layout dark theme"         android:title="darker theme" /> </preferencecategory> <preferencecategory android:title="preferencecategory b" >     <listpreference         android:id="@+id/listpreference"         android:defaultvalue="8"         android:entries="@array/list"         android:entryvalues="@array/lvalues"         android:key="list"         android:summary="choose font size"         android:title="font size" /> </preferencecategory>  </preferencescreen> 

i'm not sure in main activity in order inflate preferences , access data prefs usings haredpreferences. great, i'm rookie fragments.

because preferencefragment actual fragment, can exchange using fragmenttransation other fragments in nav drawer. in whatever onclick event or otherwise, use following go preferencefragment:

getfragmentmanager().begintransaction()             .replace(r.id.fragmentcontainer, new madlibsettings())             .commit(); 

source: http://developer.android.com/guide/topics/ui/settings.html#fragment

and accessing preferences anywhere, following code should able started.

sharedpreferences settings = preferencemanager.getdefaultsharedpreferences(getcontext()); string val1 = settings.getstring("key", "default_value"); settings.putstring("key", "new_value"); 

if decide make own preferences file in addition settings one, use:

sharedpreferences settings = getcontext().getsharedpreferences("pref_file_name", 0); string val1 = settings.getstring("key", "default_value"); settings.putstring("key", "new_value"); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -