Shared Preferences in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will learn how to store and display data using share preference so from this video we will be starting our Android kotlin Advanced Series where we'll be going to cover topics like mvvm room database API retrofit and many more all of these topics are kind of tricky but don't worry we will learn each topic with the help of project now come to today's video where we will understand what is share preference also just a little note that in 2023 developers prefer data store rather than share preference which we will cover in Jetpack compose series now what is share preference let's understand with a simple example imagine you have a small notebook like a diary in your Android phone where you can jot down some notes that you want to remember later this not could be things like your favorite color a game level you reached or whether you want the app to show music or not share preference in Android is like that little notebook it's a way for apps to store and remember small pieces of information that they can use later just like you open your diary to see what you wrote app can open share preference to see what settings or values you have saved it's handy because even if you close tab or turn off your phone the information you wrote down in share preference will still be there it's like your app's own private memory where it can store simple things you wanted to remember even when you are not using the app got it now let's create it choose empty views activity then write the name as learn shared preference and click on finish let me give you a quick overview of what exactly we are going to create so it's a very simple notes project where the user will write a note in edit text and by clicking on save button it will store the note in share preference and then when the user will click on display node button it will display the exact note in the text view simple right you can further modify the project by maybe adding a recycler view to display multiple nodes it's all up to you here I'm just going to give you a basic idea of how share preference works so first let's do the prerequisite so go to colors.xml add the red color whose hex code is FF 3131 and n then go to themes.xml here mention all the three parameters color primary color pyramid variant and Android status bar color and then next go to Gradle belt here I will write build features and then inside it Right View binding as true now click on sync now and then let me quickly add a background just to make the project look more presentable you can skip this drawable step if you want to and then now prerequisites are done so go to activity main.xml here I will create an edit text with a save button and below that a display node button with a text view where the note will be displayed and that's it let me quickly write the code and done you can pause the video and copy the layout code okay now see we have kept parent test relatively out then inside it too linear layout and then inside it and edit text whose ID is notes edit text and a button whose ID save note button then outside a linear layout there is a button whose ID display node button and the text view whose ID is not text View and that's it this is how it looks now go to main activity let me give you a quick overview what exactly we are going to do so we will initialize share preference with a name then inside the save node button we will store the data using share preference methods and then lastly inside the display node button we will retrieve the data and display it on text View and that's it so first let me quickly set a binding and then then declare share preference and then now I will initialize it as share preference is equal to get share preference this is very important method to store name of the share preference and its mode so inside it I will write Note data as a share preference name and then context mode as private this is a way to specify the excess mode for your share preference in this case you are using mode private which means only your app can access this share preference cut it then we need to store data when the user will click on save note button right so for that I'll write binding dot save node button dot set on click listener and then inside it first we will take the input from user through edit text so let me quickly write it and then so whatever the text user will write in notes edit tags will be stored in the note variable then remember in sqlite database we used to define access mode for database like writable database or readable database likewise we need to specify here as well so I'll write well shared edit as share preference dot edit means the preference can be modified or edited now write Dot put string so put string is a method to store share preference using a key so I'll write key as note this is the key you are giving to the piece of information you are storing that is node through edit text and this node is an actual content that you need to store then finalize everything by using apply method and then I'll throw a toast as note stored successfully [Music] lastly once the note is stored you can wipe off the edit text using clear method simple right now when the display node button will be clicked then it will retrieve the node and display it on text View so to do that first write binding dot display node button dot set on click listener on it and then inside it I'll retrieve the data first so write will stored note is equal to shared preference dot get string see to store the data we have used put string method and to retrieve the data we have used kit string method cut it net inside it write the exact key that is note and then I'll keep the value as null which means it will act as a default value that will be written if there is no value associated with the Keynote and then the data will be stored in the store not variable then lastly display the note so I'll write binding dot node text View dot text is equal to mention the stored not variable while the actual node is retrieved and stored and that's it we add in with the coding now let's run the app see this is how it looks I'll quickly write here some note and now I'll click on save note and see the dose says as note stored successfully now I click on display note and look the notice displayed here perfect also let me quickly show you where exactly share preferences saved so go to device Explorer while the device is in running mode then go to data and then again data and then inside it find your Project's package name here it is click on it then go to share preference folder and here you will see of share preference name that is no data I click on it and see its XML file where the exact node is stored using map attribute and that's it you can follow us on Instagram or join a telegram group for more updates Link in the description box so yeah that is it for the video if you are new to this channel then please consider subscribing to my channel and I'll see you in the next video [Music]
Info
Channel: Android Knowledge
Views: 2,275
Rating: undefined out of 5
Keywords: shared preferences, sharedpreferences, shared preferences tutorial, android shared preferences, android sharedpreferences, android studio shared preferences, how to make android apps, android, android studio, android tutorial, android save data, android store data, android development, save data, android studio tutorial, android how to save data, android apps programming, store data, android how to save variables, android development for beginners, knowledge, notes., advance
Id: eFl8_S4pyP4
Channel Id: undefined
Length: 11min 45sec (705 seconds)
Published: Fri Sep 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.