This shows you the differences between two versions of the page.
dapm:laboratoare:06 [2017/02/20 12:33] 127.0.0.1 external edit |
dapm:laboratoare:06 [2021/04/20 10:03] (current) ioana_maria.culic [Exercises :] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Laboratorul 06. ===== | + | ===== Lab 07 - Shared Preferences ===== |
+ | ====SharedPreferences==== | ||
+ | Android provides many ways of storing data of an application. One of this way is called Shared Preferences. 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.\\ | ||
+ | \\ | ||
+ | **Initialization** | ||
+ | \\ | ||
+ | Application shared preferences can be fetched using getSharedPreferences() method.You also need an editor to edit and save the changes in shared preferences. The following code can be used to get application shared preferences.\\ | ||
+ | SharedPreferences pref = context.getSharedPreferences("MyPref", 0); // 0 - for private mode | ||
+ | Editor editor = pref.edit(); | ||
+ | \\ | ||
+ | **Storing Data** | ||
+ | \\ | ||
+ | You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. Call editor.commit() in order to save changes to shared preferences.\\ | ||
+ | editor.putBoolean("key_name", true); // Storing boolean - true/false | ||
+ | editor.putString("key_name", "string value"); // Storing string | ||
+ | editor.putInt("key_name", "int value"); // Storing integer | ||
+ | editor.putFloat("key_name", "float value"); // Storing float | ||
+ | editor.putLong("key_name", "long value"); // Storing long | ||
+ | |||
+ | editor.commit(); // commit changes | ||
+ | \\ | ||
+ | **Retrieving Data** | ||
+ | \\ | ||
+ | Data can be retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor.\\ | ||
+ | // returns stored preference value | ||
+ | // If value is not present return second param value - In this case null | ||
+ | pref.getString("key_name", null); // getting String | ||
+ | pref.getInt("key_name", null); // getting Integer | ||
+ | pref.getFloat("key_name", null); // getting Float | ||
+ | pref.getLong("key_name", null); // getting Long | ||
+ | pref.getBoolean("key_name", null); // getting boolean | ||
+ | \\ | ||
+ | **Clearing / Deleting Data** | ||
+ | \\ | ||
+ | If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear()\\ | ||
+ | editor.remove("name"); // will delete key name | ||
+ | editor.remove("email"); // will delete key email | ||
+ | | ||
+ | editor.commit(); // commit changes | ||
+ | \\ | ||
+ | Following will clear all the data from shared preferences | ||
+ | editor.clear(); | ||
+ | editor.commit(); // commit changes | ||
+ | \\ | ||
+ | ====== Exercises : ====== | ||
+ | **Ex1** Create a new project.\\ | ||
+ | In the activity_main xml, add an edit text, and 2 buttons and a textView like in the picture.\\ | ||
+ | {{:dapm:laboratoare:lab6_firstactivity.png?300|}} | ||
+ | \\ | ||
+ | **Ex2** Create a class named MySharedPreference and add the code below:\\ | ||
+ | public class MySharedPreference { | ||
+ | SharedPreferences settings; | ||
+ | SharedPreferences.Editor editor; | ||
+ | | ||
+ | public MySharedPreference (Context context) { | ||
+ | //TODO | ||
+ | } | ||
+ | | ||
+ | public void save(String text){ | ||
+ | //TODO | ||
+ | } | ||
+ | | ||
+ | public String getValue(){ | ||
+ | //TODO | ||
+ | } | ||
+ | | ||
+ | public void clearSharedPreferences(){ | ||
+ | //TODO | ||
+ | } | ||
+ | } | ||
+ | \\ | ||
+ | TODO: \\ | ||
+ | - In the constructor, Initialize SharedPreferences.\\ | ||
+ | - In the same function, put the given String in the editor. Do not forget to commit\\ | ||
+ | - In the getValue function take the string from the Shared Preferences and return it\\ | ||
+ | - In the clearSharedPreferences function delete all values from preferences | ||
+ | |||
+ | **Ex3** Create another activity named SecondActivity and in the XML file, add a text view: | ||
+ | {{:dapm:laboratoare:lab6_secondactivity.png?300|}} | ||
+ | |||
+ | **Ex4** In the MainActivity java file create a new MySharedPreference.\\ | ||
+ | - When the add button is clicked, take the text from the editText and save it in the SharedPreference using the save method.\\ | ||
+ | - When the Go To Second Activity Button is clicked, make an intent and start the next activity(second activity)\\ | ||
+ | |||
+ | **Ex5** In the SecondActivity java class, Initialize the MySharedPreference and set the value of the textView with the String from the sharedPreference. | ||
+ | \\ | ||
+ | RUN THE APP\\ | ||
+ | **Ex6** Change the default colours of the application. Add Background to the items from the XML file.\\ | ||
+ | **Ex7** Send a primitive with the intent and show it in the second Activity (putExtra)\\ |