Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet160/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   156   157   158   159   160   161   162   163   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

188
CHAPTER 8: APIs
The keys used here must match the key strings used inside the fragment defined earlier. 
Note that for simplicity I used plain strings in that file. For a productive environment, you 
should of course refer to resource strings.
In addition, in res/values add a file arrays.xml and in it write the following for the various 
arrays we use in the example:



List item 1
List item 2
List item 3


1
2
3


1
3


What is left is a place in your app where to insert preferences. You could, for example, add 
the following at a suitable place:
android:id="@+id/prefsFragm"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In the app call, start the preferences workflow.
fragmentManager.beginTransaction()
.replace(prefsFragm.id, MySettingsFragment())
.commit()
For the various default values we defined in preferences.xml to take effect, you must at any 
place in your app where preferences get accessed first call the following:
PreferenceManager.setDefaultValues(
this, preferences.id, false)
This could be done in an activity’s onCreate() callback. Figure 
8-9
shows this preferences 
example.


189
CHAPTER 8: APIs
For more settings, the settings screen as designed so far might be a little hard to read. To 
add some structure, you can gather setting items in groups and add group titles. This is 
done in the preferences.xml file as follows:

"http://schemas.android.com/apk/res/android">
...
android:title="Category Title">
android:key="pref_key_switch"
android:summary="A Switch"
android:title="A Switch"
/>
android:key="pref_key_ringtone"
android:summary="A Ringtone"
android:title="A Ringtone"
/>
...

Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   156   157   158   159   160   161   162   163   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling