i'm using shared preferences for the settings menu of my android app. it's working very well but i didn't know how to use these settings on my code:
For example how to use the selected language and use it in another activity:
<PreferenceCategory
android:title="General Settings"
android:key="general_settings"
>
<ListPreference
android:key="language"
android:title="Language"
android:summary="Define the default language"
android:defaultValue="Spanish"
android:entries="@array/Languages"
android:entryValues="@array/LanguagesValues"
/>
Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.
Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory.
It's not a secret that SharedPreferences is not a secure place to store sensitive data because the data is saved in simple key-value pairs in an XML file. In some cases, it can easily be hijacked.
On code behind;
SharedPreferences prefs = this.getSharedPreferences("general_settings", Context.MODE_PRIVATE);
String lanSettings = prefs.getString("language", null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With