Hello students and this the holy grail we're going to go ahead and create another Android application.
So let's just click on this.
Start in your Android studio project and for the application and just they've here at sixty six dash
of line capability of line capability.
And you can specify the company domain package name and the project location and then click on next.
And here to this minimum STK as the API 16 Android 4.1 jelly bean.
And then click on next and here choose empty activity next.
And here are just a click and finish.
So here let's actually connect our application to the firebase.
So now at the top just click on Tools and Firebrace And then here click on realtime database and click
on Save and retrieve data.
And first just you just need to click on Connect to firebase so you can see here this starting connect
dialog.
And here you can just see that we have actually created a new project.
So if you want to actually create a new firebase project app 66 offline capability.
So in this tutorial I will actually let you know about the offline capability of fireplace.
So let's create a new project and then you can specify the country or region and then click on our connect
to firebase.
So here we can see that it says connecting our two firebase.
So it's going to take some time in order to connect that application to the firebase.
So we should actually wait a little bit here and now as you can see here it says firebase project create
it and connect it locally to my real app.
And here you can see that it says connected.
So now we should do the second step and it says at the real time database to your app.
So just click on the spot in here and here just accept changes.
So now as you can see here it can actually make our application ready for using their real time database.
And here are first of all it's actually the go inside this activity underline main the example.
And now let's actually collapse this assistance here.
This firebase assistant and here are essentially agitatedly to this hello world takes you here to get
it.
And now first let's actually write a text view or a text field so distasteful.
Here say this takes viewless for folder.
Just drag this plain text here and put it here inside this relatively out.
And now let's write a button instead.
We just want to just select and drag it here below this text field.
And put it here.
So now let's let's actually select this edit takes here.
And for the editors step in here EDT data this actually deletes So here as you can see we have this
dialogue and it says update usages as well.
So here you can just click on yes.
And for the name attribute here just delete this value and then person enter key on your keyboard and
select the spot.
And for the ideas they've been here between send data and sent it up and the person into a general keyword
and then are here for the text that should be that have this button just typed in here sent data to
set and then person entelechy on your keyboard.
So let's actually go inside.
Our main activity that have a file Let's actually just declare these objects here.
So now the first one is actually an edit text so EDT data semicolon.
And the second one is actually a button.
So between and data semicolon and here inside is uncreate Meffert It's actually to initialize these
variables.
So it is the data assignment operator and you literally just create this object.
First we need to cast this object to an edit text object find view by ID or that Id that EDT data.
And then at the end of your statements you need to put a semi-colon.
And here we need to actually to initialize our part in PTEN send data assignment operator and he'll
essentially cast this object to our putten object find view by id Artaud ID dot between send data.
So as you can see here it cannot actually recognize our IDs so let's actually go inside our activity
underlaid minute example here and here as you can see I actually made a mistake.
And here we need to put PTEN sent data.
So here I just need to press on until she on my keyboard.
So let's go back to her domain activity class so PTEN sent data and then I need to press put a semicolon
at the end of my statement.
So he'll actually make the Spartan disobedience and date hot button our listeners with the clicks of
the users to send data that said unclick listener new view that unclick listener.
So here Enceladus uncle Anklage method we can actually put our own logic so here first of all this actually
creates somebody else inside.
Our main activity class.
So the first one is our Firebrace firebase database and the name of my valuable is database semi-colon.
So as you can see here I'm just declaring my variable here right.
I'm not initializing my variable.
I'm just declaring here.
Now let's actually declare another variable of type database reference and the name of the variable
is they have a reference.
So now let's actually initialize our variables here inside our uncreate Smurfette.
So here I can say a database right.
Assignment operator firebase database that get instance semicolon and then lets actually initialize
our database reference.
So database reference assignment operator database that gets reference semi-colon.
Right.
So now I want to talk about offline capability.
So what do you mean by offline capability.
So the offline capability is going to actually enable your application to actually interact with a database
of when the internet connection is actually gone.
Right.
So for example it's actually let me just put these lines of code here.
And when we run our project and it will make more sense.
So first of all I just need to type in here database here.
I'm referring to the name of this valuable database that says persistence enabled and this method actually
accepts a boolean variable.
So of here we just need to pass a boolean value right.
So here I can say true because I want to enable the offline capability of the firebase for my application
I need to pass the value true for this method.
So a database that says persistence enabled truth.
And here I can say that database reference that cheap sinked true.
So these are the two lines of code that we should put here in order to actually enable the offline capability
and that's all.
So that was actually sent this actually just set a value for our node.
So now I can just type in here database reference dots set value.
And here I can say for example some value some value right.
And now let's actually run our project just a click and run app here and now it's actually a little
bit here.
Here we need to choose one of these devices one of these virtual devices.
And then I just click on OK.
So as you can see here our application crashed.
Right.
So let's go to Android studio and here able to show you why our application crashed.
First of all in order to see of why your application crashed at the bottom you just need to click on
this android monitor here.
Right.
And here you can see what's going on here.
Let's actually make this area just bigger and you just need to look at these codes.
You can look at these texts that are in red.
And here it says fatal exception made.
So Maine means actually in the forget.
The main freight.
And as you can see it says here you can actually find the cause of this error.
It says jobs that landed at runtime exception.
So this is the type of this exception run pump exception is actually an exception that occurred when
you actually when you are running your application.
Right.
So when we tried to run our application this error occurred and here as you can see it says 66 of brain
capability has stopped.
And here it says Unable to start activity component info.
So these are the info overnight activity.
And here is actually scroll to the left.
And here it says let's scroll to the right as you can see here it says come that Google that firebase
that database that database acception.
So this is actually on our database exception.
And here it says calls to set persistence enabled must be made before any other uses of file based database
instance.
So here you can see that it says that V must actually call the said persistence in a belt method or
before any other usage of firebase database instance.
So what does this mean.
So this actually is just go to our codes here.
As you can see inside our uncreate Smurfette.
First of all we actually initialized our database instance right.
And then we actually called this method.
So this is not right as the here.
It says that first we should call this method before any other usage of firebase database instance.
So here let me actually show you this area.
Again let me show you these texts.
It says this method must be made before any other usage of firebase database is this.
So now as you can see it is actually just caught this line of code here.
So if you are using a match just hold the common key on your keyboard and then press on the letter X
key on your keyboard.
And if you are using a Windows machine just hold the control key on your keyboard and then press on
the letter X key on your keyboard in order to cut this line of code and put it here before this line
of code here before using the firebase database instance.
So here I can just paste it here right.
And in this way as you can see we are actually using this method before using the firebase database
instance.
So again we are going to have a problem.
So but now it's actually on our project and then I'm going to actually let you know what is this.
Right.
We may actually encounter another problem.
So let's run our project again.
Let me show you the emulator here.
So again our application crashed.
So let's see why this time our application crashed again.
We should go to the Android studio and then click on this android monitor at the bottom.
And here let me make this area just bigger.
And here again we should look at these lines of code that are in red.
And here again it says we have actually our runtime exception.
And now let me actually scroll to the right and here it says Java the land dot null pointer exception.
So it says attempt to invoke virtual Meffert void com that Google that firebase that database that firebase
database that says set persistence enabled boolean on a null object reference as you can see here you
are calling a method on a null object reference.
So this is actually an exception.
So let me show you why this error occurs.
As you can see here in line number 19 we declared are valuable which is of type firebase database in
here.
Right.
And the name of our valuable is database.
As you can see here.
I didn't initialize this variable.
I just declared this variable.
And inside our uncreate method before I initialize this variable.
As you can see here I use this method.
So in line number 44 I use I actually I actually initialize the Smurfette But in line number 52 are
used this method.
So this database here is actually not.
And if you are calling the sniffer said persistence enabled we are calling this Nefert on a null object.
This these are valuable database.
As you can see is not quite because in line number 44 we just initialized it.
And before this line of code here as you can see here we did it initialize.
So now in line number for it is if to this database where the event is actually null and you're calling
the Smurfette on a null object on a null variable.
So it is going to actually cause an error and our application is going to crash.
So in order to solve this problem I can just delete this database here and here I can say firebase database
dot get instance that sets persistence enable true.
Right.
So in this way it is much more better.
So now it's actually our project again.
Now let me show you the emulator.
Now as you can see here no errors occurred here.
And that application is actually running on this emulator.
So let me show you the web browser here and now I just need to go to as you can see here you just need
to put this you are in here and then click on go to console.
And here you should click on this app 5:54 dash of line capability here.
So this is our project right.
Just click on it and then click on database and click on rules.
Just click on this rules tab here and here let's again change these rules so just delete these exclamation
marks and put an equal operator here and again just delete this exclamation mark and put an equal operator
and then click and publish.
So now it's going to actually publish the rules as you can see here.
And here again just click on the state bar tab here.
And now this is our note right.
So here I want to actually just tell the wife right off.
Right.
As you can see here my wife is actually on now.
You want to turn my wife off.
So now as you can see here my wife is now turned off.
Right.
So now let me show you the emulator and now let's actually just click on the spot and send data to server.
As you can see here I just clicked on this button and now I'm able to actually turn my phone on.
So now I turn to my wife right on.
Right.
And now as you can see here this values some value here is actually here.
Right.
So if you have noticed here I actually first turned to my wife right off and then I clicked on the spot
in and when I turned my wife on again this value is actually sent to the server and we can see the value
here.
And that's the offline capability of Firebrace.
And this is I think the best feature of the file by spackled.
So this is very nice.
So if I go back to android studio by just putting two lines of code we did manage to use the offline
capability as you can see here.
We did manage to use the offline capability and we could actually use it for our great applications.
Right.
So now here I just I'm just sending this value some value so I can actually change this value to the
value of the EDT date hot that just text that to strike.
I could actually send the value of this and it takes to the server.
So I think that's enough for this tutorial.
And if you have any questions about the of line capability of the firebase or please post the discussion
in the course and then I'll do my best to answer all of your questions.
Thank you very much for watching and I'll see you in the next story or.