Save Data in Firebase Realtime Database using Kotlin | Realtime Database | Kotlin | Android Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video of fox android so in today's video we're gonna learn how to write the data to the real-time database from our android application right so i've already made a video on this topic using java but in this tutorial we'll be using kotlin as the backend language right because we know that now as the kotlin is the official language so we should now start migrating to kotlin so i'll be making the videos on all the crud operation using kotlin right so let me first show you that what we're gonna get by the end of this video right so if you can see on my mobile screen there are four edit text first name last name agent username so if i enter the data in these fields and press this login button so it will create a node in the time database so let me just show you so if i enter robert as the first name and download is the second name h might be 45 and robert s or robert d as the username so now if i click on this login button so you can see a new entry has been made into this real-time database right so this is what we'll be getting by the end of this video so without wasting any time let's get started so guys if you're new to this channel make sure to hit that subscribe button press the bell notification icon for receiving notification of the upcoming videos and by the end of the video if you like the video make sure to hit that thumbs up button as well so in this in this project i've already made this screen right in which we have four edit text fields and the login button so we'll not be looking into the xml code of this screen i've already made a video on this so if you want to make a screen like this you can check out that video by clicking on this i button so i'll just give you a brief idea about the screen we have 48x fields in which the user will enter the data and we have a login button so once the user enter the data and press login button the all the data will be saved to real-time database right so okay guys now we'll just connect our app to the firebase project so for that i'll just go to tools firebase and this tab will open up and inside this will just go to real-time database get started connect to firebase so this will just open up our browser so if you want to create a new project you can just click on this add project other i'm already having a project real-time database i'll just connect my app to this project only so i'll click on real-time database well now let us just click on this connect button you know the our app is connected to our android studio project now let me add the dependencies to the project click on this button accept changes now the build has started let the build be completed so okay guys the build completed you can see depending setup correctly right so we have successfully connected our app to the firebase project now we'll just quickly dive into the backend code so i'll go to the main activity so for creating the data class just right click here go to new and kotlin class so here select data class and give the name as user rate press enter so here you can see we have created the user class so inside this class will be taking four arguments the first one will be first name the type of which will be string the second one will be last name age and username right so i guess there is some mistake here okay i haven't used the valve keyword so guys the class has been created you just need to define the arguments right the arguments that the constructor will take so after that we'll just go to main activity and here we will be using nika's concept of view binding view binding is true so we have enabled the view binding so let this thing be completed so okay guys this thing has been completed now we'll just go to our main activity so now we have enabled the view binding feature right so i'll declare a variable for view binding class so i'll just name it as binding only so those of you who do not know about viewbinding i have made a dedicated video on that you can just check out that video database and type of this is database reference rate so okay guys now we'll just set the on click listener on our button right login button port one click listener so here what we'll do is we'll just get all the values that are being entered by the user first name dot text dot right so here we'll get all the values so guys we have got all the values from our edittext field right so now we will get the reference of our real-time database so for that we will use firebase database dot get instance dot reference and here we'll pass users sorry get reference and users as the node so we got the reference of our database so what this line of code will do is it will get okay time database so it will get the reference for this node so we have got the reference tail here and inside this node we want to add an entity of the user so right so let us create an object of the user so we'll pass first name last name and then h and the username so we got the object of the user class now we'll add the child username so now after the reference of users node we have got the child and the name for that will be the username entered by the user so for example if the users enter maria is the username so we have the reference tail here and the maria will be the child of users so that is what we have written here database dot child and the name of that will be username so after that set value and we'll pass user here and we'll add the on success listener so if the data has been successfully written to our real-time database then we want to make sure that all the editex fields has cleared because we have used the data so we'll clear all the four fields last so we have cleared all the text fields now we'll raise a toast so we have this the toast which says successfully saved and now we'll add a on failure listener as well so here also will there is a toast which will say failed right so guys that is it that is all we need to do let me just give you a brief idea we have created the object of the user class and then we have passed the first name last image and username passed given us by the user so then we got the reference for the real-time database we got the reference tell users then we have entered a child using the username and then we have set the value and we have passed the user object that we have just created as an argument then we'll just attach the callback methods the first one is on success listener so if the data has been successfully written to the database will clear all the fields and raise a toast and in case that the there is some error the data is we are not able to write the data will have attached on failure listener and i've just raised the tools which is fit so let now let me quickly just install this app to my cell phone and we'll see if it is working fine or not right okay guys the app has been installed to my cell phone as you can see on the screen so let us try to add some data into our real-time database so let the first name be james and the second name be charles so age will be 65 and the username will be gmc right so if i just click on this login button so you can see this entry has been made james charles gmc right so i guess we have successfully implemented this feature into our android application right so we have learned that how to write the data to the real-time database using cortland so i hope that you guys have understood the code in case of any doubt you can always ask me in the comment section you can dm me on instagram the instagram username will be there on screen right now i have also put in the link for the source code of this app you can if you want the source code you can just check out that link and if you like the video make sure to hit that thumbs up button and if you're new to this channel make sure to hit that subscribe button and press the bell notification icon so that was it for today's video see another video bye [Music] you
Info
Channel: Foxandroid
Views: 53,272
Rating: undefined out of 5
Keywords: crud firebase, firebase crud kotlin, firebase save data kotlin, kotlin firebase tutorial, firebase kotlin tutorial, saving data in firebase with kotlin, kotlin firebase, firebase kotlin, firebase realtime database kotlin, retrieving data from firebase kotlin, kotlin firebase database example, get all data from firebase database android, realtime read and write data tutorial, realtime store and read data android tutorial, storing and retrieving data from firebase, 2021, foxandroid
Id: MFcMw9jJA9o
Channel Id: undefined
Length: 12min 15sec (735 seconds)
Published: Thu Apr 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.