What are the storage methods in Android systems and the storage methods?
12 minute(s) read
Published on: Aug 07, 2021
Updated on: Dec 14, 2021
There are different types of data in Android systems that we must save so that we can use them in the future. It should be noted that there are many ways to store different types of data in Android systems, one of which is SharedPreferences. As we said, there are many ways to store data in Android systems, and each of them is up to developers and application designers to choose. Application developers choose the right methods to store different types of data in Android systems based on taste or application, but one method is much more suitable than the others, which is mostly used by designers and developers. Applications are used. For example, we can say that to store application information such as fonts, colors, styles, night or day modes, application size, user information registration or such information, we must consider the method we want to store them. . In this educational article, we will get acquainted with their types.
For more details, consider that a user has selected a photo as his profile for himself in the designed application, and in this case, this profile photo selected by the user must be saved, otherwise with each time out. The user also leaves the profile image application, and the user is forced to select the image as a profile every time he exits and enters the application. To prevent this and to permanently store information, we use SharedPreferences in Android systems.
It should be noted that SharedPrefs is intended for storing key-pair data pairs, which is why the security of the stored information is very high and the data that is stored will be secure.
What are the benefits of using SharedPreferences and why should we use it to store data?
- SharedPreferences is very fast and low volume.
- It is possible to store very small data in such a way that it does not require the use of separate databases.
- This method of data storage is compatible with all versions of Android systems.
We use the following three methods to access them:
- getPreferences ()
- getSharedPreferences ()
- getDefaultSharedPreferences ()
1- We create a new project in Android Studio and choose the desired name for it. The name chosen for this project in this part of the Preference tutorial.
2- Then add an EditText and a Button to the activity_main.xml section. We do this so that the user can enter their name.
3- Then we define two widgets in MainActivity.java.
4- The codes that should be in the activity_main.xml section are as follows:
5- Then we have to put the following code in MainActivity.java.
6- We need to create an instance of the SharedPreferences interface. In this section, we consider the desired name for it, which is also shPref.
7- Then set shPref equal to the getSharedPreferences () method.
8- It should be noted that this method has two inputs and parameters. The input and the first parameter are name and the second is mode.
Check different types of MODE.
This is the default mode that will call the data content file available.
Using this mode, other applications can also read and access the data.
Allows other applications to edit Preference information if we use this mode.
This mode will also be used to check preferences, even if Shared Preference has already been implemented.
Using this mode we will combine new preferences with old preferences.
In this tutorial we use MODE_PRIVATE.
In this part of the tutorial, our intention is to ask the user to enter his name and then press the Save button to save the user's entered information, and then after opening and closing the program again to be able to access the user's entered information. Have. In other words, we ask users to enter their information and register it, and after opening and closing the program, the information they have entered will be registered so that they can finally access that information again.
9- Complete the method related to the btnSave button. Like the following:
In the code above, we first define a variable of type String and name n using the following command.
User input is taken from edtName by Text and stored.
We then created an example of SharedPreferences.Editor called sEdit, which uses shPref.edit () to edit and save information.
Then we move the contents inside n to Name using the putString method.
Finally, the commit () method is used to save the applied changes. Like the following:
10- After them, we must add the conditional statement so that if the values stored in the Preference section are displayed in the desired location.
By entering the above code, we say that if shPref has a name, it will get its value using the getString method and then display it on the edtName widget using the setText method.
Get methods are like set methods, get methods like getString, getInt, getBoolean, getFloat and getLong are used to retrieve data.
By inserting the above code, if something is not already saved in the Name field, it will display the phrase "Name not saved!" When displaying it in the edtName widget.
But in this tutorial, we want to consider the value of null for it. Like the following:
11- The complete codes that should be in the MainActivity section are as follows:
12- We will implement the project.
13- After executing the project, we enter a name and then save it.
14- After that, it is better to leave the application to check whether the result is saved or not.
15- We run the program again.
16- After running it, we will see that the program works properly and the name we entered in that section is displayed in the EditText section.
Even if we edit this name and save it again, we will see that the name we just entered has been replaced.
17- Then we add two fields with the titles of surname and age to the project. Like the following:
18- Then we run the project.
19- Enter the information you want from us and then click save.
20- Information is stored successfully.
The int value does not have the ability to be sent to the widget by setText.
21- To solve it, we must add a simple string to them. Like the following:
In addition to this method, there are other methods and principles that we use the principal method in this part of the training.
22- Convert the integer to String using String.valueOf () and then send it to edtAge.
There are two other application methods in Shared Preferences:
- remove ("KeyName"):
This method is used to delete data.
- clear ():
This method is also used to delete all existing and stored information.
23- We add two buttons to the program. The names of the buttons are Reset and Remove Name and Family.
24- In this part of the tutorial, we want to provide changes that can be removed by pressing Remove to remove the name and surname that the user has entered, and by pressing Reset, all entered information will be deleted.
25- Complete the button method as follows.
26- We will implement the project.
27- After running the program, we will see that the information we have already entered will be displayed.
28- In this section, we expect that by pressing Remove, the information related to the name and surname will be deleted.
29- After pressing Remove, the information related to the name and surname will be deleted.
30- Then enter the first and last name again and click on Reset all.
31- After that we will see that the clear () method successfully performs the desired operation and if we close the program and exit it, then re-enter it, the entered information will be deleted.
Note that it is also possible to access the Preference file.
32- Open Tools - Android menu and Android Device Monitor window.
33- On the left side, in the Device section, select the name of the application.
34- Follow Data - data - [Application name] - shared_prefs on the right and the File Manager tab.
35- Save the MyPrefers.xml file using the Pull a file from a device option and then open it.
36- After that we will see that all the information is in that file.
37- All the code that should be in the activity_main.xml section is as follows:
38- Next, the code below should also be in the MainActivity.java section.
In other cases, and using SharedPreferences, we can create a project in a different way and configure that project as well. The code below is about the second and new project we use to store data in SharedPreferences.
1- In this section, we create a new project and choose a name for it. This time we choose the name of this project SharedPref.
2- Complete the activity_main.xm layer as follows.
3- The codes listed below should be in the activity_main.xm section.
After entering the above code, the output that we can see is that it creates a page and asks the user to enter his name, then adds two buttons. It should be noted that the color of the buttons is one blue and the other purple. The text on the buttons is "blue theme" and "purple theme".
4- Then we must complete the coding of the MainActivity section.
5- After we have done the coding related to it and applied the changes and edits, we will see that the result will be that the applied changes will not disappear every time we exit and enter it.
In the code listed above, to be able to work with SharedPreferences and save data at a lower volume, we must create an instance of the class object, this class has two parameters that when creating it for the first parameter is one Select the desired name and set the second parameter to MODE_PRIVATE.
In the code above, we have created the SharedPreferences.Editor class so that we can edit the information. Edit information in such a way that every time you log in or log out of the account, we must edit the information.
In the code above, we use AppCompatSeekBar and change the font of the text that is placed inside the toolbar, and at the same time, the information about SharedPreferences is changed.
Using the put and commit functions, objects created from the class are edited and executed. In the code listed above, we use the findView, getColor and seekBarSetting functions.
Descriptions of applications of different types of functions are as follows:
- GetColor function:
We use this function to save the latest status of SharedPreferences using a boolean value. Using this function, we can change the tone by pressing the buttons. By changing the status of the themes, the latest status of the themes will be saved in changeTheme, which is an object of the SharedPreferences class.
To be able to change the background color of the toolbar, we use the setBackgroundColor function, on the other hand, if we want to change the background color of the status, we will use the setStatusBarColor function. Note that the setBackgroundColor function is used to change the background color of the entire page.
In the code above, we can use the recreate command in the saveThemeBlue and saveThemePurple functions to be able to apply settings that change the status of the theme at the push of a button, and we can see the result. So, we knew that in the saveThemePurple and saveThemeBlue functions we use the recreate command because it can easily see the result every time you press the button to change the theme and appearance.
- SaveName function:
This function can be used to do everything related to logging in or out.
How to initialize in Shared Preferences
To be able to save the information in Shared Preferences and edit it, we need to create an Editor.
The code below can also be used to receive Shared Preferences.
Another example of Shared Preferences in Android
has two EditText views that can be used to save the username and email.About our Android and iOS app development services