Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading shared preferences

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"

    />  
like image 835
AndroidM Avatar asked May 15 '13 19:05

AndroidM


People also ask

What is shared preferences explain with example?

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.

How can I get shared preference data?

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.

Where are shared preferences Android?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory.

Can shared preferences be hacked?

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.


1 Answers

On code behind;

SharedPreferences prefs = this.getSharedPreferences("general_settings", Context.MODE_PRIVATE);
String lanSettings = prefs.getString("language", null);
like image 127
Mehmet Emre Avatar answered Oct 06 '22 00:10

Mehmet Emre