android - How to use preference fragment -


i have following code preferences named settings. want code happens if l click item lets list dialog , select text size. want text size of activities changed.

settings.xml

    <preferencescreen     android:title="settings"     android:summary="setrings text              size , background colour">      <intent     android:targetpackage="com.ecb"             android:targetclass=         "com.ecb.hymbook.hym1"/>       </preferencescreen>       <checkboxpreference     android:key="checkbox_preference"     android:title="@string/            title_checkbox_preference"     android:summary="@string/        summary_checkbox_preference" />     </preferencecategory>     <preferencecategory     android:title="@string/          dialog_based_preferences">        <listpreference     android:key="list_preference"     android:title="@string/         title_list_preference"     android:summary="@string/       summary_list_preference"     android:entries="@array/         entries_list_preference"     android:entryvalues="@array/      entryvalues_list_preference"     android:dialogtitle="@string/    dialog_title_list_preference" />      <listpreference     android:key="list_preference2"     android:title="@string/      title_list_preference2"     android:summary="@string/      summary_list_preference2"     android:entries="@array/     entries_list_preference2"     android:entryvalues="@array/     entryvalues_list_preference2"     android:dialogtitle="@string/     dialog_title_list_preference2" />       </preferencecategory>       <preferencecategory       android:title="@string/          launch_preferences">      <!-- preferencescreen tag         serves screen break (similar      page break     in word processing). other       preference types, assign key     here able save , restore         instance state. -->       <preferencescreen     android:title="@string/         title_intent_preference"     android:summary="@string/     summary_intent_preference">      <intent                 android:action=     "android.intent.action.    view"         android:data="https://       chat.whatsapp.com/     0gwqdb5idas0fmj0eerals"/>     </preferencescreen>      </preferencecategory>         </preferencescreen>          settings java           public class settings         extends activity {          @override          protected void oncreate(bundle               savedinstancestate) {          super.oncreate           (savedinstancestate);         // display fragment main               content.            getfragmentmanager().            begintransaction(  )           .replace(android.r.id.content, new                 prefsfragment()).commit();             }          public class prefsfragment                extends               preferencefragment {          @override          public void oncreate(bundle               savedinstancestate) {           super.oncreate          (savedinstancestate);          // load preferences               xml resource         addpreferencesfromresource        (r.xml.settings);        }               }     }              array.xml             <resources>       <string-array           name="entries_list_preference">       <item>default</item>       <item>small 1</item>        <item>small 2</item>        <item>medium 1</item>         <item>medium 2</item>          <item>large 1</item>        <item>large 2</item>        <item>extra large 1</item>       <item>extra large 2</item>        <item>extremely large</item>         </string-array>       <string-array name="entryvalues       _list_preference">     <item>default</item>     <item>small1</item>     <item>small2</item>     <item>medium1</item>     <item>medium2</item>       <item>large1</item>     <item>large2</item>     <item>extralarge1</item>     <item>extralarge2</item>     <item>extremelylarge</item>       </string-array>       <string-array             name="entries_list_preference2">     <item>blue</item>     <item>brown</item>     <item>gray</item>     <item>violet</item>       </string-array>       <string-array name="entryvalues_     list_preference2">     <item>#ff000099</item>     <item># 5f1e02 </item>     <item># 333333 </item>     <item># 421c52 </item>     </string-array>      </resources> 

ok, creating settings page can little confusing @ first, when new android development.

while doesn't answer exact question, may give better understanding of how settings work in android, can apply own use case. of course, if you're still unsure of something, let me know in comments , i'll update answer.

create preferences or settings xml file

it looks you've done already, i'll briefly explain anyways.

this file determines kind of settings shown user. goes in res/xml directory. mine looks little this:

<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android">       <preferencecategory          android:title="@string/prefcategory_general">           <checkboxpreference              android:key="pref_example_checkbox"              android:title="@string/pref_examplecheckbox_title"              android:summaryon="@string/pref_examplecheckbox_summaryon"              android:summaryoff="@string/pref_examplecheckbox_summaryoff"              android:defaultvalue="false" />      </preferencecategory>       <preferencecategory          android:title="@string/prefcategory_notifications">           <switchpreference              android:key="pref_enable_notifications"              android:title="@string/pref_notificationtoggle"              android:defaultvalue="true" />           <edittextpreference              android:key="pref_notification_time"              android:title="@string/pref_notificationtime"              android:summary="@string/pref_notificationtime_summary"              android:inputtype="number"              android:dialogmessage="@string/pref_notificationtime_dialog"              android:defaultvalue="5" />     </preferencecategory>      <preferencecategory         android:title="@string/prefcategory_about">          <preference             android:key="pref_about_licenses"             android:title="@string/pref_licenses" />          <preference             android:key="pref_about_app_version"             android:title="@string/pref_appversion" />     </preferencecategory>  </preferencescreen> 

there lots of different types of preferences - can see have checkboxes (checkboxpreference), switches (switchpreference), , plain/standard preferences (which normal list items - preference). each type of preference has different properties have key , title.

the key value preference stored under required. stored on user's device.

create settings layout xml

next, need create activity layout settings page (which goes in res/layout.

it can simple or complex like, needs have framelayout. android inserts list of settings defined in previous xml file framelayout. here example:

<?xml version="1.0" encoding="utf-8"?> <linearlayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent">      <android.support.v7.widget.toolbar         android:id="@+id/toolbar"         style="@style/widget.myapp.toolbar"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <framelayout         android:id="@+id/content"         android:layout_width="match_parent"         android:layout_height="wrap_content" /> </linearlayout> 

your framelayout should have id can refer later.

add activity code

public class settingsactivity extends appcompatactivity {      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_settings); // layout xml here          // insert settings fragment in framelayout added earlier         getsupportfragmentmanager().begintransaction()                 .replace(r.id.content, new settingsfragment())                 .commit();     }       public class settingsfragment extends preferencefragment {          @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             addpreferencesfromresource(r.xml.preferences); // settings xml here (not layout)         }     }  } 

detecting preference changes:

you can detect when user has changed preference using onpreferencechangelistener:

preference preference = findpreference(key); preference.setonpreferencechangelistener(new onpreferencechangelistener() {     @override     public boolean onpreferencechange(preference preference, object newvalue) {         // when preference has changed.     } }); 

this allows additional actions - note android take care of changing preference (so value preference key gets changed automatically).

reading preferences

to read preferences need values sharedpreferences. can use methods getint, getboolean, getstring, etc.

 sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences(getcontext());  int preferencevalue = prefs.getint(key, 14); 

i put kind of thing in method getabc.


your use case

in case, want set font depending on preference. store font in sp (i.e. use in settings page). then, in layouts, set font size of textviews programatically.

i think honest, may not worth effort - code can quite messy if you're having set font sizes of lots of textviews programatically. android os has settings option users can use increase or decrease font size on of apps, advise against it.

however, trying implement learn more android , if have strong reason having settings option, that's fine too. ;)

edit: settings option

as mentioned, android os has option changing font size. they're available in:

settings > display > font size

if remember correctly, newer versions of android have option changing display size, scales size of items on screen, not fonts.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -