Android app Development Mini-Course - Build a Complete Todo App [Android ROOM & Android ViewModel]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and hi and welcome so i'm very excited that you're here and one of the things that i've had students constantly asking me about was how do we build a full-fledged application like a real android app right something that is more complex nothing too simple so that they could see exactly how to structure a full-fledged application from start to finish from scratch i decided to put together this mini course which is part of a bigger course of mine that you can actually get from you to me in the links below i decided to put here together so that you have a full view of how to build a clone of a very popular to-do app so in this mini course you will learn how to structure the entire application from scratch so from looking at the user interface and looking at the mechanic and infrastructure in the back end such as room database and android as well as view model and many other things that i'm going to show you in this mini course now before we get started i would like to let you know just to simplify your life because it's a lot of code i've actually put together a starter project you can go and download so you can follow along and start adding things to the project as i show you in this course in this mini course so go ahead and follow the link below so you have access to the starter project that way it's much easier for you to get started in this mini course so i'll let you be now and let's go ahead and get started so in this section we are going to build a full-fledged application that will showcase what we've learned before so we will have a room database so database that we can add and retrieve items from and other things we've learned let me demo the app we are going to be building throughout this section so as you can see here it's a pretty good looking application in my opinion i hope you find it true you see here we have the title in our toolbar so you see here it says to doister so that's the name i came up with actually this application is actually going to be a todoist clone there is a very famous app called todoist which essentially manages all of your to-do's all of your appointments and so forth is really high-end application obviously we're not going to be cloning everything from the main app to do this app but at least this app will mimic the fundamentals of the todoist application in fact you can google todoist app and you'll see what i'm talking about so that is what we're trying to mimic here or that is kind of the clone pretty exciting all right so the great thing here is that you have this button this floating action button you click and when you do that there is this nice dialog that comes up this is what we call a bottom sheet dialog so it gives the application incentive you only have one screen so single page application if you will so in this case here click and i can add a to do so let's say record course because that's what i do and then you click there and you can see now this kind of expanded our bottom sheet expanded and it can actually expand even further so we can see through everything so now we have different options where we can give it a due date so we can say today tomorrow or next or i can just go into the future by clicking this nice calendar and pick one here doesn't matter let's say i want this to be tomorrow i click there and it's going to attach this to do to tomorrow okay and next i can click here to give it some sort of priority so i want is this a high priority or a medium priority or a low let's say this is high because students are waiting for the course so once i have that i can just click this button and voila our bottom sheet dropped and then you can see we do have here this entry okay and it's very contextual too if you look at this because the priority is high and the color is red notice now we have this very nice radio button that is also red next i can click here and let's add another one let's say be kind and then click here and i can expand that even further and if i want go in the future i can go all the way let's say that's going to happen in march or april because that's who i am click there and then let's say that's a medium priority that just tells you what kind of person i am and if you click here notice that was added and because it's medium the colors also are contextual that has changed okay and also we can edit each one of these entries so if i click one look at this it pulls in this name record test or record course and i can go ahead and say updated and done this makes no sense but there it is so i can also change for next week if i want and the priority now is going to be low one minute i click it's going to go and update that everything notice also changed so it's very contextual and this looks very simple but often the very simple applications visually and also structurally right those are actual applications that require a little bit more work to put that together okay and also you see we have these three dots here this is what we call the hamburger menu button if you click here there's this other button that is extended if you click that further you can see it takes us to this about screen okay very good so i can go back to the main screen and we can keep adding even more items here so i can say for instance i want something new something that says brainstorm course ideas can go ahead and click again and i want this to happen a little bit later there's the date priority medium let's save and there you have it right so there's a lot of ground to cover and let's go ahead and get started now to simplify our lives i decided to create a startup project that you should have downloaded previous to this video and once you have downloaded that sort of project and you have unzipped it you would have these structure here so if you looked into our java folder there a new probing inside of this package you can see we have this about activity if you click it's just a very simple about activity with an xml if you click there you can see this xml it's very simple again it doesn't have much he just says here todoist clone app and so forth okay so this is what is already set up for you that way we don't have to spend too much time writing the boilerplate code and all the things that you should know by now how to do yourself so the idea here is that you have a way to get started with the bare bones the skeleton of the application of the project i should say that way we are ready to just keep going all right so that's what we have there and i'm going to close this down and then you can see you have this bottom sheet fragment so double click here now it's a very strange class in that it is a fragment we'll talk a little bit more about fragment but just think of a fragment as a piece of code that can structurally be used anywhere along with an activity so we can actually pull a fragment this piece of screen that we can take and put anywhere else we want so it gives us a lot of structural flexibility when we create android applications we'll talk more about fragments all right there we go so this is where we're going to create that bottom sheet we saw earlier where when we want to add something or edit there is that little bottom pop-up that's why it's called bottom sheet fragment because it pops up from the bottom and you can actually expand it and so forth as you saw okay now it's empty because it's a starter project but we will work on it as we go through this project so i'm just introducing what you should have so far in the starter project and inside here you can see that we're inflating this layout called bottom sheet let's go ahead to bottom sheet you can see our bottom sheet here has some code we have our xml code and we also have we can change to its design mode so we can see exactly what we have here it's very simple in essence we have an edit text top there which also has a hint text that says eat less dinner so users know what to expect or what needs to be added or inserted into this edit text and we have a few icon buttons there that we've added now all of these icon buttons actually pull in icons images that we get from android so android studio gives us the capability of pulling in many different icons or images so all of these are actually added already inside of our resources we can have all this or all of these so double click you can see they open up now one thing you notice is that they end at xml and once you open one up you see it says vector here so vector is just a different image format that is more flexible but most importantly it's a format that allows for the images to never lose a resolution doesn't matter how big the image is it will always keep that resolution and there's no as you can see as a make this bigger the image doesn't pixelate okay so it always keeps that resolution which is real good very well all right so next we have of course our main activity it has some code here now our main activity we notice here we have this toolbar which we can click and look at a toolbar this comes all comes with android by the way depending on what you select when you create a project you can go ahead and get the activity that contains this floating button as well as a toolbar so this is where we can add of course the navbar and other floating buttons there as you saw earlier but most importantly you can see also this component tree here if if this is not open you can always click there and open that you can see in the component tree we have this app bar which is this there and inside we have a toolbar so toolbar it is inside of our bar which allows us to then access this toolbar so where we can add buttons and many other things there and we have our fab floating action button there it is and we have a recycle view that i right away added and has everything but next you see we have this include here what is this included and it's kind of off screen as you see there let's make this a little bit bigger so it turns out with when we create complicated layouts you can actually use the include tag to include either layout so you remember i showed you earlier here let's go to our layout this bottom sheet in fact i think yes this bottom sheet here so we created it separately and then what are we doing now is that we including this bottom sheet into our activity main xml and you notice it's hidden here it's actually showing a little bit here but it's actually hidden because remember the idea is that it will be prompt out it will be shown the moment we do something in this case when we want to add it to do item okay and so the beauty here is that we can actually include that instead of having to code all of it inside of our main activity we can just go ahead and create separately and include it into a different layout in this case main activity so if we look at the code this is very interesting let's go somewhere down here you can see we have include tag and we say layout and we say add layout and we get our bottom sheet okay that's very very helpful all right so that's what we have there and we have this fragment bottom sheet which we won't use at all and i'll get rid of that but most importantly we have this to do row to do row is going to be used to actually inflate right remember we're going to be using a recycle view actually have the recycle view as you see here in our activity main this is the recycle view we need a row that will be inflated on runtime to show up here with all of our to do items all right so now we have all the pieces and bits that we're going to be using to create our project here you should have everything ready to go in fact let's go ahead and give it a run real quick to make sure everything is good all right there you have it you should have this running without any issues now one thing to remember is that depending on when you're watching this video and things do change in android development meaning our tools do change you will need regardless you will have to update or let the gradle sync up everything to your machine in order for this to work properly okay so once you get the starter project you will have to go through that process but if all goes well you should at least be able to see this bare bones application working so now that we know that everything should be set up correctly we will continue in the next video where we will get started putting together the pieces that will compose this to do start project let's go ahead and take a look at the database entity structures that way we have an overall idea of what is it that we're going to be building start putting together the backend database all of the supporting classes and code as i call it so first i'm going to look at the actual table or entity okay how is that going to look like well the table is going to look like as follows so we're going to have a task id column that's where all of the item or to-do's ids will be generated automatically and then we'll have a task column this is where we're going to actually store the actual task such as eat ice cream okay and of course we're going to have the priority so that way we can store whether the priority of this task is high low or medium we have a due date we're going to pass a date object so when is this task due and we also have another date type column called created at so this created at will have the timestamp of when this item was added into our table and then we have it's done could be true or false a boolean that we can store okay and there you have it so let's go ahead and start putting together this task entity so back to our project let's go inside of our package here i'm going to create a new package to organize our code this one is going to be called model like that so inside of a model here i'm going to add a new java class and this class is going to be the task class as such okay it's going to be actually our entity all right so the first thing we need to do of course is to make sure that this is indeed recognized as an entity in room so i'm going to say at entity this only works if you have already set up the room dependencies and so forth which you should have especially if you have downloaded this starter project okay if not you should know how to do that by now all right so let's go ahead and say table name we're going to call this task table as such there it is and let's go ahead and add a few properties which are going to be translated into columns the first one is going to be public i'm going to say long now notice here i could have said int so all of the ids could be integers but long it's usually better because actually when these ids are saved or auto-generated i'm talking about a task id that we will be creating here when they are generated in room they're stored as long so let's just go ahead and add the long type from the beginning i'm gonna call this task id okay and let's say public another public and make sure that all of these are actually public fields okay so it's a task this is going to be our task saying also public and say priority now there is different kind of priority classes that already exist in the android realm if you will these are not the ones that we're looking for we're actually going to create our own priority class in a second here let's say priority okay i'm not going to import it anything yet because i don't want this from render script we'll create that in a second okay and then we're going to say public again this is going to be a date type okay make sure you get the java util date type you can see it was important as such call this due date say public again this is going to be another date type call the date created public and the final one is going to be a boolean type called is done as such okay let's make sure that room knows that these are columns for our entity so i'm going to say first one so this is going to be a column the column info and because i want this task id to have a different name so i'm going to override it by say name i'm going to call this task underscore id now keep in mind passing this task id as such what we're saying is that we want this column to be called as such task underscore id if i wanted this task id to actually continue with this name cascadi as is written they here we would just leave this alone so we would just get rid of that all together okay but that's not the case we want this to be task underscore id okay very good we're going to continue for our string here task i'm going to also say at column information or info i'm going to say name for our name here actually i don't need to change that because i it's just one word it's going to be just tasks i'm going to just remove that so again in this case here room will know that yes this name property task as it is is going to be also the name for our column when the entity is created all right and for our priority here i'm also going to leave as it is because i want it to be priority like this when it's created the column name okay all right so now for our due date in this case here i'm going to name something different so i'm going to say column info i'm going to say name as such and i want this to be written as due date as such and add column info also i want this to be say name it's going to be created at such and also is done i'm going to also change say column info name is going to be is underscore done like to give spaces here so they're easily visible okay very well now one thing to not forget is that we know that this task id is going to be our primary key so we need to annotate that so i'm going to say at primary key and i want to say this is going to be auto generated pass it to true very important very well now before i create getters and setters and constructor here let's talk about this priority that we have created or we must create the actual class of ours so what we're going to do we're going to create a separate class but it's going to be a special class this class is what we call an enum now what is this thing let's take a look so we're going to create this class called priority and it's going to be an enum class so it's going to look something like this public in a priority and it has these fields that kind of look a little bit different than what we used to first of all what is enum well an enum is essentially a class that represents a group of constant variables so meaning variables that don't change just like final variables okay so the idea here is that when we create this enum class we're going to be able to say whenever we want we can say priority dot and axis either high or medium or low or anything else if we had other fields inside of this enum so it's a simpler class that we can put all of our constant variables in it so we can quickly go and fetch those variables when we need them okay let's go ahead and code let's go back to our code and create this priority enum class all right so back to our code again remember this is red and it gives you this option to import this render script that priority that's not what we want we want to create our own priority class which is going to be an enum back to our project and inside here i'm going to go ahead and create say right click new class and just going to go class let's call this priority it's going to be a class that is let's go back and say priority and such and there we have it now it's going to be public class and as i said it has to be an enum so we're not going to add this class there i'm going to say enum as such okay all right so the constant variables in this case the fields that we want are three and you will notice the syntax is a little bit different usually what we need to do is we need to make these fields or these constant variables i should call them too they have to be all caps so in this case i'm going to have high okay and then i'm going to say medium and low as such and there you have it that's all now sometimes with enums you can actually create constructors and do all sorts of things but in this case this is all we need that's totally fine so now that we have this enum we can go back to our task notice now it's going to have another option here notice let's see it went ahead and got our enum let's see is this priority the one yes it is our priority enum make sure that that's what we have all right there we go all right so now we have created the priority enamel again make sure if you hover over like this let it stay there for a bit it's going to tell us exactly that this is from our package model public in a priority it has to be from our package otherwise you're going to be having a different enum that it's not very useful for us okay let's go back here let's go and actually create a constructor first so i'm not going to add the task id because remember the task id will be created and added automatically so i'm just going to select anything else all of other properties or fields as such okay and next let's go ahead and create uh getters and setters for all of them and there you have it and for now what i also like is i like to override the to string that way it gives me the string representation of this class if i need to if i create a new task and just want to see what that task the string representation has all the fields as you can see here i'll be able just to call that this task class and it will give me this string representation of all the fields which is very handy especially when we are debugging our application okay very well so we've done a few things here so we've created the task class which as you know by now it will be transformed into an actual table or an entity and we have all the fields we have the task id and it's going to be our generated as our primary key and we have all of the fields or columns there and we created the constructor and getters and setters okay and something new here is that we actually extracted or created is priority enum and you can see now it says e there to say that this is a special class called enum all right it's very nice very good so in the next video we'll continue working on our models and other classes that we need to build up this application okay i'll see you next very well so we're making real good progress next let's go ahead and create the actual database room okay so inside so inside of our main package i'm going to right click and create a new package and this one is going to be called util for utility so this is where i'm going to put all of the util classes right click say new another java class and in this case let's call this task room database such good and we know that this has to be actually an abstract class so i'm gonna say abstract as such very good and right away i'm gonna go ahead and extend our room database okay there it is you can see now it was imported automatically so let's create a few instance variables here that we're going to be using first of all i'm going to say psf this is very handy can actually create static final fields here by just saying psfi or for string this is for int okay it's very handy so you don't have to type all of that yourself in this case here i'm going to add this is going to be the number underscore of threads okay so i'm going to add four we can add any number but four that's fine so what this is gonna do we're gonna be using in a second here because we want to make sure that all this transaction of course we're gonna go through this executor service that will allow us to do things in the backlog or in the background thread let's go ahead and create another psf there and this one is going to be volatile okay i'm going to call this it's going to be an instance of this same class here task room database such i'll call this instance i'm going to be using zoom let's make this private because we don't want this to be shown anywhere else and doesn't have to be final you can't do that okay there we go and then i'm going to create another psf there and this one is going to be our executor service okay from java concurrence there and it's going to be gonna call this database writer executor i'm gonna set it to executors that and i'm gonna go ahead and create new thread pool here new fixed that is thread pull and pass the number of threads we've created earlier right so next i'm going to create a public static method here of type task room database right i'm going to call this get database essentially will allow us to actually create an instance of this one database called final context here because that's what we need and i'm going to just go ahead looks like we need to import our context inside here i'm going to make sure that our instance current instance of our database if that's null it doesn't exist then i'm going to use synchronize this is all about making sure that everything goes back to the background thread and there's no issues really and this is where i'm going to go ahead and pass our task room database such we need to pass that class and if also our instance again is no in this case then i'm going to go ahead and create a new instance of this room database okay so we're trying just to make sure that we only have one instance of this database created throughout this whole project so let's say room database builder and i'm going to build the database to build database i'm going to have to pass context and get the application context like that and then i'm going to pass the task room database the class right because you will know exactly what needs to be done and then i'm going to pass the name of our database so what i can do really is create another string here so pfs or psf that is string type okay and i'm gonna just i'm gonna say database let's make this cap database name as such and i want a database name to be doister database like this it's easier that way so we can actually call it here called database name guys okay and then we're going to continue i'm going to build that database but before we even do that we can also add a call back here which will allow us to do all sort of things the first thing is we could input or add things to our database before when we start this database when the first time the database is created we can add things to it but the other thing too is this is where we can delete everything every time we do something to our database that way we have always a clean slate okay we can do all sort of things so in this case here i'm gonna just pass s room database callback which we haven't created so we're going to go ahead and create this callback at the top real quick here so i'm going to say going to be a final it's going to be a room database that callback such i'm going to call that like this what we passed there as a callback and we're going to say new room database that callback and then here let's make sure we have finished business there okay so now inside here let's go ahead and override a method there which is going to be the oncreate method okay so again that's what i'm saying so when this database has been created we can then invoke and do whatever we want right in this case i'm just going to go ahead and say database writer executor and that execute and pass a runable here so again this will allow us to do things in the background which is exactly what we always want to do when it comes to databases and writing things into database okay in this case here we can invoke dao and write and do all sort of things and we'll come back and do just that later but at least we have that callback ready for us to go now we have an issue here because we must of course return say return what is gonna return the instance that we want to make sure is around or if it's not around we want to create that instance of the database very good and while we're here it's a public abstract method here called task doubt which we haven't created and call this task now as such and you know by now that any abstract method doesn't have a body that's the whole idea all right so we need to create this task dao class what we're going to do is go back to our project and in our package we're going to create a new package call this data to organize our code i'm going to create a new java class and this is going to be task dao hit enter and remember this is actually going to be an interface okay let's go back down here and let's go ahead and import that all right now we have no errors and our task dial is created and of course it's empty but that's fine because we're going to be working on our task dial okay so our task now here first of all let's never forget we need to make sure that this is recognized as a dao indeed we can use annotation say at dao data access object that's very important because this is the interface that we always talk about which will allow us to then invoke the insert methods and actually insert things into our database and do all of the crud the create read update and delete operations all right so the first thing is going to be our insert so i'm going to add insert as such i'm gonna say void call this insert task to insert a task well we need a task object and we already have that we've created that okay very good and then another thing we would want to do is to delete all of our tasks in our table which is not necessarily something you want to do but it's nice to have it so i'm going to say query i'm going to pass our query here which is going to be i'm going to say delete all from our table which is task underscore table okay looks like i'm having issues here which means i need to then say void delete all this is the method when it's called it's gonna do that and i have an issue here because i may have made a mistake delete all ah actually i don't have to say all i just have to say delete from task table again very dangerous that means we'll wipe everything from our task table but sometimes we need to purge things all right we'll continue here and let's say we want to get tasks so a list of all tasks so in this case we're going to say live data because we want to encapsulate this data the list of tasks that we may be receiving right the types are going to be task all of that is going to be inside or wrapped inside of live data because we want the flexibility of live data because we can then listen to events when the data changes then anything that has been listening to that data in this case on our user interface it will also change automatically beautiful stuff all right in this case here i'm going to call this get tasks that's the name of our method and i'm going to say add query and it's going to be again select all from our table task table as such now that could work but i want to make sure that i call it exactly as it was saved as and there we have it okay very simple very simple indeed so really exciting things here the next thing is it could be that we want to get just one task right we want to just get one task by passing the id again i'm going to say live data i'm looking at one task not a list of tasks okay say get in this case here i'm going to pass a long type as the id and let's go ahead and pass the actual query here the query is going to be something along these lines i'm going to say select all from our table task table that is and i want to say where our task id i can say task id right but i like to say task underscore table dot task id okay where that is equal of course to our id notice the syntax here is very important we say calling id to say that whatever we are passing this id here is going to be passed there and it will compare if the task id is equal to whatever we pass then that's indeed what we want to delete i'm sorry then that is indeed what we want to retrieve not delete all right and we're going to continue here the next one is update we want to of course to be able to update a task let's say void update and we need to pass a task that needs to be updated and the beauty here is that we don't need to do any query really we can just use this annotation call update and we're done ah beautiful and delete we want to delete a task yes we can do that by say void delete a task we need to pass the task that we want to delete okay and we're going to go ahead and annotate to say delete just like that and we're done with our task now now going back to our database taskroom database there inside of our callback it's very easy now we can go ahead and do something like this we can go ahead and say task dao such book create the actual instance is equal to instance our database instance task dao so now i'm actually invoking this task dao method that we created there okay and then i can say task now and delete all right so a clean slate so we can do that and then if you wanted we can go ahead and also start writing things into our database writing to our table and so forth and there you have it and you can see that the get database is actually grayed out because we haven't actually used that to create and get database because we haven't actually used the get database in an activity or fragment or anywhere else where we want to start creating that database and go from there but the infrastructure as you see is now set up for us to move on actually we have one more thing we need to do you notice here that i have not done what is really important which is to make sure to annotate our class this taskroom database to be known as an actual database okay it's very easy to forget about this important detail that's why in many cases the moment i create the class i annotate it right away because it's really really easy to forget as you see here and then you start getting errors and then you get frustrated and it doesn't help anybody in your household so what are we gonna do now i'm gonna say at database and then i'm gonna say entities remember we need to pass okay when we create this database what are the entities or entity in this case could be just one two or a hundred let's hope it's not a hundred at this point but at least one we need to pass here so in this case here i'm just gonna go and pass a task dot class remember if we had more entities we could have just gone and add another entity here like priority or so forth okay and of course we need to go ahead and pass the version of our database it's one and export schema i'm going to just say to false because we don't want that all right and there you have it next we are going to create our repository because we have a task dao where we have all of our crud operations that is and we have our task room database where we create database and don't forget to actually add the entities that will be created when this database is created this class is run okay it's very important so let's go back to our project tab there and now we're going to create our repository so our repository is going to be inside of our data package i'm going to say new java class and this is going to be i'm going to call this doister repository there it is a very nice name so the repository is actually not a necessary class to be had however it's very helpful because you now have one central place where all the data independent of where the data is coming from could be coming from a table database table or room database table that is or could be coming from a network somewhere obscure okay so we have one central place where we have all our data all right and one reason you can see that this is indeed not part of room database is because you will notice that we don't even have to add any of those annotations right like the at database or at repository so there's no such thing so that's when we know that this is merely for code organization purposes all right so our repository here needs to have access or have a handles for our task dao okay so i'm going to say private make it final and this is going to be task dao such call this task dao and i'm going to also have another private final it's going to be live data which will have a list of all of our tasks it makes sense i'm going to call this all tasks let's go ahead and import and looks like it doesn't recognize our task let's go and import our class and doesn't recognize the list as well let's also import list that way we are set okay so we got this squeaky lines here the reason why is because we must actually create a constructor that will instantiate all of those so i'm going to say command n to create a constructor i'm going to pass those two like that now i'm going to change a few things here instead of passing all of that i'm actually going to pass application say application here because you will see in a second here this is where we're going to instantiate our database and it needs the application context so now i'm going to say task database room database that is database and i'm going to say is equal to task room database invoke the get database aha then we pass our application context very nice now we have the handle here of our room database in this case here this task that now of course is not going to work because we're not passing as parameters there so that means we need to instantiate them how do we instantiate our task now well we go through our database which we just instantiated or created so once a database that task dao remember this task dial and click there comes from where comes from our room database it is indeed a method of type task dao which indeed returns a task down and this task down here if you notice correctly the moment this run the first time we actually have a handle of that that's where we use to delete everything that may exist in our table if there's nothing there then nothing really happens because generally speaking we can't remove things that don't exist okay very good so there we go we're gonna do the same thing for all tasks now what is it that we want for all tasks well all tasks gonna be a list of tasks which is going to be wrapped in live data that means then we can still use here our task dao this guy there so we don't even have to say this because we're not setting up as we would have before right because we know that we're talking about this task out there so we can remove that so in this case i'm going to say task dao what we just got from our database i'm going to say dot get tasks so now whenever we instantiate our repository this will run which means we're going to create a database instance we're going to get that instance and then we are going to fetch a task dao remember task dao has all of these methods we can use to insert delete query the database all right and then we are going to fill out this all tasks with all tasks we get from task dial so essentially we are calling this get tasks from select all from task table which returns a list of tasks wrapped in live data hence the type that we want to make sure that all tasks also is live data as such okay okay very well so now we're going to create some sort of public interfaces meaning these are the we're going to create methods that will be used anywhere else we want to actually retrieve the information we want so the first one i'm going to say public this is going to be live data because we'll be expecting a list of tasks go back there as such and i'm going to call this get all tasks well get all tasks as a method we'll have to return say return what all tasks this all tasks here has been filled up when the do store repository constructor was called very handy right there it is and the next method for a repository can say public this one is going to be avoid i'm going to call insert i'm going to pass a task such and to insert we're going to say task room database dot we're going to use the database writer executor this is very important again because we want to go through this guy here to do this insertion in the background thread because we don't want to be in front of our user interface thread which will then bog down our system in our application may end up lagging and all of the sort of things right so in this case here i'm going to say dot execute notice i need to pass a runnable which allows me to run things in the background again that's exactly what i want and in this case here we're going to use lambda because that's what we do and let's see make sure this and put the arrow there to say then i'm going to use task dao dot insert and pass the task that we are passing there so now when we say repository that insert we pass task down inside it's going to go and say okay it's time to insert we're going to call the taskroom database and invoke the database right executed to execute that is to make this happen in the background it's very important and then we pass this task down say insert and insert the actual task let's continue here let's say public let's say now we want to get one task i'm going to say live data i just want one task and say get and we need to pass long id and this just return a task down get long and pass the id right very simple now i'm going to update to update and say void update pass a task that we want to update and we're gonna call again our task room database database executor because it's very important execute pass a lambda expression there task dao and we want update and pass the task that we want to update and of course the most famous one is delete let's say public void delete and to delete we need to pass the task we want to delete and we're going to go say task room database that database writer again what do you know and let's go ahead and pass the lambda again task dow and we are going to delete what is that we want to delete we want to delete a specific task that we pass and just like that we've done our repository okay all right so we're making real good progress so we're really working on our back infrastructure the databases doing all the crud so create a read update and delete and make sure that the repositories and the task room database which were created all of that works together and of course with our task dao which is what links between our repository and the inner infrastructure of our database right because if you look back to our task dao this is what's happening here we actually going to our task table and get things or deleting things and so forth okay so this is an infrastructure that you should really think more and more about and do more exercises and really understand that this is the best way so far to get this job done if you were to try to do this the old way which means you would have to use the sqlite helper classes and all of that it would have it would still work but it will take more time a lot of boilerplate code and you're prone to having a lot of issues because there's no safe checking really but with room database makes our code much simpler as you can see here to delete things to other things this is all you do to update you add the add update notation and then you are ready to go much much easier much simpler so i know it's a little bit more code but it is indeed what is recommended to do when we are trying to do anything related to databases in android very well and in the next video we'll continue making good progress all right i'll see you next next we'll look at the task view model this is the getting closer to the top of the mountain if you will closer to the user interface this is going to be the direct interactor with the user interface so let's go back to our project and go to our model let's open our model there inside of model and right click new class this is going to be called this task view model such and the this will extend android view model my cycle like this and of course we'll have to create a matching constructor as such and in our matching constructor here what we need to do is we are going to create so and there you have it close this down we have more space so we're going to have a few fields here the first one i'm going to say public static we need to get a hold of our repository right so doist repository call this repository that's fine i'm going to say public final and it's going to be live data which will have a list of all of our tasks that we may have call this all tasks like that and so of course we will need to create a repository or instantiate a repository by saying new repository to which repository that is and pass our application as the context because that was the contract and once we have our repository we can use the repository again to go and invoke any of these public methods public interfaces i call them because we can then go and get for instance the insert by calling insert passing the task and then this code is run and we insert a task okay so it makes it much much easier all right so now we have a repository we can say all tasks first and i'm going to use the repository we just created or instantiate it and say get all tasks and always keep in mind the types that we're getting are very important because get all tasks go back to our repository i notice get all task is a method that returns live data containing a list that contains tasks that's why we also notice that these all tasks we know ahead of time this all tasks will need to also be the same type otherwise obviously things won't work so always keep in mind of types in java it's very very crucial very good so we have our task view model constructor there then here we can go ahead and create a few other methods in this case here i can create a public say public as such live data and this is going to be a list called task again it sounds like reputation but it's actually not now the great thing here we can call the same names we can use the same names for all of our methods because we're in different classes per se okay so whenever i say task view model dot get all tasks that means then in this case i'm expecting to return say return all tasks because at this point i should have all of these tasks filled up from our repository that get all tasks okay so remember this is get all tasks from our repository and not from this tax review model all right okay i'm going to say public i'm going to create a static one here void say insert i'm going to insert a task object i'm going to say repository insert and pass that task we're going to continue in this case get with the public live data i'm going to just get one task it has to be live data say get and pass along id this will return go through repository that get pass that id like this okay return that live data task and let's say public static void update and update this task such let's say repository update and pass the task probably again static void delete and it's a repository dot delete pass that task we are done with all of the underlining infrastructure that we need and i think it's important for us to revisit the android room architecture diagram so we can see where we're at at this point uh with the infrastructure that we've built for this application and it's always a good idea to have that in the back of your mind because it kind of helps you guide and see the reasons why we do what are we doing so let's take a look at the diagram once again so here is our android room architecture diagram that i think again you should always have handy especially when you're creating databases using room okay so at the bottom we've created the table entity which actually is going to be inside of our sqlite database right and right away we created dao so data access object which interacts directly with our sqlite so we saw the dao has the methods that do the crud operation so create read update and delete okay and within all of that we actually encapsulate it all inside of room database so this is where we create our database we pass in our entity to say okay when you create this database these is the entity class that you need to use to map out how the structure of the database will look like the tables the columns the ids and so forth okay and then we created the repository which is the single source of truth for all app data which allows us to have this clean api for ui to communicate with okay again it's not necessary to have repository it's not part of this architecture but it's very helpful because you could have many sources of data where your data is coming from so having repository and have that connector as the place where all the sources of data that you may have are coming through is a good thing to have and just now we just finished the view model which includes the live data to allow the changes to be notified through the observer mechanisms with the user interface so when data shows up and it changes it notifies this interface that something has changed and therefore it should also update that data so this makes our lives as developers way easier and less stressful all right so now that we have that we can start playing at the ui controller or the user interface side of things so in our activity or fragments or whatever it is that we want to use or see this data from right so it makes sense so in the next video we're going to test this infrastructure that we created by adding pieces of data and log them out so we can see that things are indeed working so now that we have created the underlining infrastructure it's time for us to take a look and see if we can save something into our database all right so the first thing we need to do let's go ahead at the top here i'm going to create a private instance variable here call this item view and this is going to be a task view model call this task view model as such and right inside of our main somewhere around there i can go ahead and instantiate it so it's a task view model and i'm going to use the new view provider that is the model view provider factory as such and we pass main dot this and then dot get application contact that get application as such new line let's put this in a new line so it's easier to read and then outside i'm going to say dot create and we need to pass our task view model class like that okay so now we have our task view model that means we can then use our task view model and do all of these and call all these methods that we want all right so the first thing i'm going to do let's go inside of our fab button here this floating action button on click we're going to get rid of all this snack bar because we don't need it i'm going to just go straight first of all we can actually minimize this by using lambda like this and here i'm going to go ahead and use our view model first of all i'm going to create a task object and say task such call this task is equal to new task and i want to pass a few parameters so first of all i'm going to pass the string task and say to do and then let's pass the priority priority i can just go ahead and say priority dot hi remember that we are using the priority enum class and then the next thing we need to pass let's see it's not helping us let's see what else so we got a due date and date created now to get a due date in this case here what i need to do is i'm going to use a calendar class a calendar java.org let's say dot get instance like this and then get the time like that so this will just get the due date which is going to be pretty much the moment we add this item into our table which is not necessarily what we want but this works i'm going to do the same thing for i believe that's going to be that was due date we want a date created so this is actually more appropriate if i say like this calendar that get instance and that get time because this will be when this is added to our database we're testing things out right now and so we don't need to worry too much about a lot of things okay so and then we need to pass a boolean whether this is done or not so i'm going to say false to say that it's not done yet all right so we have our object here a task that we can put into our table to do so of course we're going to go ahead and say task view model and we can say dot look at this because in our task view model class we have methods that we can easily use directly so for task view model notice for the insert is a static that we can go ahead and use this means that we don't need to actually go through in this case our model task view model object we can instantiate it here right because it's a method that is static we can actually just say task view model let's say task view model like that and i can go ahead and directly say that insert now the reason why this is easier that way we don't have to go through our task view model even though i did instantiate it here we're going to use it later to do other operations such as get all tasks as well as get a particular task now the difference here is that when we get all tasks we are receiving live data which is a list of tasks and wrapping that into live data the same thing happens when we're getting one task okay but the rest because we're inserting and updating and delete we just made those static so that it's easier for us just to go ahead and straight away do what we want to do right so all right so to insert obviously we just need to pass a task now in this case here you would think that if we ran this application in fact let's go ahead and run and see if this is going to work i think this is a good test let's run aha this is what i wanted to tell you about even though our emulator is still not awake but there is an error that happened right away while this was compiled this is the power of room database so it's telling here that cannot figure out how to save this field into database which field is that is this priority field and it says you can consider adding a type converter for it what is this all about well it turns out because we said if we go back to our task in fact it went and showed our task.java and this is the field this is the field here but also remember this is an entity which means a table which also means this is a column so when we run this piece of code we say we want to insert this task to our table and this task has to do as the task and priority is probably that high the compiler room database goes ahead and looks at this and says i don't understand what this is because i can't add this to my database because i don't know what type this is that means i can't really add that to the database so the same thing will happen obviously with this calendar that get instance and get the time because these are types even though we know in our tables we said we understand that those are date types as well as priority types this is a custom type that we created and in a way this is also some sort of a custom type or say so those are the types that it doesn't know how what to do with so it is saying you need to create some sort of a converter class that will automatically convert these types into the appropriate types that can be parsed and added or saved or persisted into our database now this is another way for me to showcase how great room database really is because all you have to do is to create a class called converter you can call whatever you want let's go ahead and do that real quick here let's put that inside of our util write new java class let's call this converter you could call anything you want but naming is very important in java in fact let's say converters because we can add a few methods here like that hit enter let's close this down for more space okay so we have our converter class here what do we nee what we're going to do is we are going to create a few methods that will convert this for instance as an example this date here into a timestamp unix type which is recognized by room database as a valid type to be saved as so that way when we run our application whenever the compiler gets to this part it's going to convert that automatically before it adds that to our database and convertibly what we also want to do is create another method that will convert that back into a data that way we can extract back into that format that we want the same thing with priority okay so we're going to say public we're going to make them static that way we don't have to have to create an object of this converter class in order to access it i'm going to say start with date okay date like that i'm going to say from time stamp so i'm covering from time stamp into something else in this case a date so we're going to pass a long value called value we could call anything we want and then this will return what so we're going to do some very awesome coding here really so what we're going to do here is going to say something like this well first of all i'm going to say value that we received if that value is equal to null if that value is null that is then well we're going to return null else we're going to say new date and create a date out of this value that we are receiving because remember the idea is that is this will convert the same way when we get stuff from our database once we've saved it will have been saved as a timestamp which means it's going to be a long value okay so we're going to convert that back into a new data type so we can get that that's why it says from timestamp and returns a data type now for this to work with all of our classes or all of our databases and entities and everything we need to tell the compiler by annotating it and say that this is indeed a type converter like this all right i'm gonna do the same reverse of what we have here so i'm just gonna copy this and instead of returning a date we're going to return along so this is what will be used from time stamp actually let's say this is going to be date to time stamp so this is the inverse of what we have from time stamp in this case to date because returns a date and now we're going to say from date to timestamp so this is going to be chosen or used to convert the date type which is exactly what we need this day type into a timestamp which will be acceptable to be added into our database so instead of returning that i'm going to do the opposite so we're going to say instead of value this is going to be actually date that we're passing like that and i'm gonna check if date is null then everything is null else we're gonna say date dot get time like that okay and now we're gonna do the same conversion for this other type which is in this case our priority we can go back to main which is what we're passing here when we create the task all right so let's come down here i'm going to say at type converter that's very helpful public static in this case i'm going to expect a string type coming here say from priority and then i'm going to say pass the priority type as such and so i'm going to return is going to be priority of course i'm going to check if that is null then of course we return null else we're going to return the priority dot name the beauty here is that what is this that name field is that name field comes already from enum class so any enum class you can actually return the name of that field as a text as you see as a string okay again what are we doing here well from priority meaning we're going to pass the priority and we're going to try to convert that into just a string which means we're going to just get the string representation of that enum field and we're going to do the same thing at the opposite in fact let's just go ahead and type all out i'm going to say add type converter and give some space and i'm going to say public static now we're going to get priority or this is what we're going to be returning say to priority like this and of course we're going to pass string priority and we're going to return priority that we're passing if that's null well if that's null then we're going to just say it's null else actually this is the opposite like that else we're going to say priority that get the value of this string probability that we're passing so this is the converter class now the beauty here really is that all we have to do is pass this converter class inside of our database so that it knows automatically so let's go back to our database there see we have a database entity we can pass that also by annotation i'm going to say at type converters ah look at that and we can pass as many type converters as we want just like as we can pass as many entities as we want in our entities array there okay in this case we only have one class that we want to pass in this case it's going to be converter class there make sure it's within our project okay say dot class that's all you have to do really because now what happens is that when this run it will know okay there is indeed a conveyor class that needs to be passed around when we create the database so you will know exactly which types that we have added to our database in our dao in our table and everything that we'll need converting before and after we when we want to add to database and we want to retrieve that's all you have to do so all of this is actually being used here along with our database annotation we have this type converters annotation does it make sense so now this problem we had before this one we should not see anymore in fact let's go ahead and see if that's true let's go ahead and run one more time see now it's installing no errors nothing really is preventing us from running this app if you go to run again there's no errors build everything is good nice all right so what we're going to do next is i'm going to go ahead and open the database inspector just because we want to see if we are able to add that first to do task that we are adding here when we click this floating button let's go ahead and open our database i think it's still opening i believe give it a second ah the reason why you don't see this now look at this everything is set up but it says databases and this is all grayed out the reason why is because we're running this pretty much for the first time so the database really hasn't been created yet right so it makes sense so what we're gonna do really is we're going to click voila the moment you clicked now the database was created and you can see there is a task table we have the task id task priority and so forth the table is indeed empty because if you click here you'll see that let's say select all from table like that if you run actually it's not empty because you can see we were able to add one task there and you can see also there is the id the due date and created at it these are unix timestamps that were added automatically when we added this calendar that get instance get time right so this was converted because we have this type converter class which we created which automatically was called to do the conversion automatically everything is working perfectly the same thing happened with our priority field if we go to main here this one that we added it was also automatically converted and all is good so now we're able to actually pass those values to our persist in this case into our database in our table so if i go ahead and click again we're going to add the same item let's go ahead and refresh the same item was added twice and the other thing you also you can do is you can say live updates if you take that live update every time you click you should see the live update happening we are adding essentially the same item anyway so that doesn't really help but you see that it's working perfectly so the infrastructure we put together is actually working we were able to create the database and the table and the converters everything is indeed great now it's time for us to look more towards putting together the user interface part of things to allow users to add items update and delete into all sort of things all right very well let's go ahead and keep going doing just that so we've learned how to add a few tasks actually so we added three of them they happen to be all the same that's fine but at least we know things are working perfectly now of course the question will be like let's test to see if we can get something from our database now the question is now of course it's always nice to see if we can we are able to also retrieve something from our table you should be able to do this so i would ask you to pause the video for a few minutes and see if you can figure out how to retrieve information from our table keep in mind that we are going through our task view model all right go ahead and give it a try all right so i hope you were able to do that if not that is not a problem whatsoever so here's what we're going to do outside of our floating action button here in fact right below our task view model what we're going to do is we'll say task view model as such and then i'm going to say dot get all tasks and there you have it but the beauty here is that we can attach an observer to say that because this is a live data right i can say observer or observe like this and pass this as the owner of this life cycle in this case i'm going to say new observer listed like this and there you have let's give ourselves a little bit more space so i can see and as you know let's go ahead and use lambda to simplify our life so now we have a list in this case of tasks so i can say something like 4 i'm going to say task call this task and pass our task this is a different kind of for loop so what are we saying here is that every task that we get from a task array list or list i should say same thing really we're going to put that in a task object which makes sense so now i can say log d inside here and i can pass say plus task and say let's get get task like this and for a log here let's create a field here that's called let's call this item like this okay so if we have something in our database obviously when we run our app we should be able to of course see the task for each task that we have in our database now we know of course because we can see that our database has tasks and we should see at least three to-do's let's go ahead and run in fact let's first go to log cat instead of verbose it's debug and we're going to just pass our item tag so i tell them like this okay let's go ahead and run and you can see we have our to-do's now why is it that we're only getting two the main reason why we only get two is what happens is it just shows to to signify that the rest is just reputation that's just how in most cases our log cat works okay so initially we do know that we have three but at least we can see that we are getting all of them and what we can do also is that we can say for instance instead of get our task like that we want to get something more unique say date created say get let's get id i think that's probably going to be great so we can see all of the ids let's run again open our logcat and now you can see one two three because these are ids and they're all unique okay so just keep in mind that if you don't see the right number as we saw earlier the to do's they all have the same name so logcat usually will cut off the repetitions if there are too many repetitions just shows like couple or a few to signify that things are working so it's a little bit misleading but now because the ids are unique then you can see all of these does that make sense all right there we go so things are indeed working right this is good very good news so what we have left really is to start putting together the infrastructure the user interface side of things so we can of course when we click this add button the something will pop up in this case our bottom sheet view fragment will pop up and then we can add things and then when we say save then we're going to start seeing a recycle view showing all the information now it's all about just mechanics but at least we know that everything the underlying architecture is set up for us to work on the user interface without having to worry about a lot of things very good so that's what i'm going to be start doing in the next videos let's start with our recycle view adapter that is the central piece if you remember correctly we want to make sure that let's run this app once again you want to make sure that when we add something or if there's something already in our database which in this case it is indeed true we saw earlier we want to populate those items those to-do's or those tasks right away and if we look back to let's look at our resources here let's go to our layouts we notice we have this to do row which came with the starter project so it's very simple we have a radio button there that's added we have a to do row text view and we have this to do row chip so the chip is a very interesting view or widget that allows you to have something like this so you can add an icon maybe a little bit bigger you can see it has that very nice enclosing background there looks like a pill really and you can add text so it really enhances the user interface so and then we have this view that i added here which is a linear layout that we have this nice kind of gray divider there i kind of like that okay so that's what we have here nothing really out of this world and of course if you want you can look at the actual code it's always nice remember you can always do write the xml code or use the design or you can do both okay so likely this time you have the server project and has all of this done for you but you can always come down here and change everything if you wish to do so that's actually very encouraged in this course very good so olive is there and that's what we're going to be doing so we're going to use all of this to add that into our activity main which you look at this it has this recycle view added there so all right let's go ahead and get started i'm going to get rid of all of these and i'm getting rid of that as well so let's go ahead inside of our package there i'm going to create another package and this is going to be called adapter because this is we're going to that's where we're going to add our recycle view adapter so i'm going to right click new java class call this recycler view adapter as such all right so first thing we're going to do we're going to extend our recycler view such and i'm going to go and get adapter and we are going to pass of course a recyclerview adapter a class called view holder which we haven't created yet okay so first thing we're going to do let's go ahead and create the viewholder class there it is and we will get back to that and the next thing we need to do is of course do some implementation of some methods that are needed when we use a recycle view adapter so we have the oncreateviewholder on pineviewholder count and so forth now for our viewholder here because it's also an independent class it has to extend the recycler view that view holder class as such and which means we also create a constructor that matches exactly the view holder all right there it is we'll come back to that obviously okay so let's go ahead and put together the pieces and bits real quick so at least we can start seeing things hopefully in our user interface the main thing i'm going to pass here is going to be final and it's going to be a list which will contain task objects so let's say task list like this okay let's go ahead and import our task there and of course it's going to complain because we made this private that means we have to create a constructor so i'm going to add constructor in this case and pass that parameter there all right so that's all good now okay so next here we know that on create a view holder this is where we create or we inflate our row that will be attached to our recycle view widget so i'm going to say view because we need to create that view say view as such i'm going to use layout inflator it's going to inflate from our parent view group that get context and then we're going to use inflate or that layout and i'm going to get the to do row okay it should be task row but to do row that store will find we're going to parent and we're not going to attach to root so pass false alright now that we have that view of course i need to make sure that this is equal to that okay now that we have our view that's when we're going to say new viewholder class at the bottom here and we're going to pass that view which is exactly this view we just created there there we go okay let's go down to our view holder and this is going to be setting up our user interface because remember we have our view which we call itemview which is essentially what we are getting from our layout to do row so we can now invoke that to go and get all of let's find out to do row as such to get all of these widgets that are inside there so let's put a few fields here the first one i'm going to say public i'm going to say app compat radio button i'm using compact radio button because that's what i've used if you go back to where to do row there this radio button here let's go to our code it's a public radio button now there's two options here i always like to use the app compact radio button because there's that compatibility component there that's fine call this radio button and i'm gonna use another public here this app compact text view this is our to do so essentially i'm pulling in all of these widgets and that's to do that's fine you can call it task to be consistent i guess and public i'm going to go and get the chip right it's going to be today chip like this so we'll need to go ahead and import that all right so that means then inside of our constructor here this is where we have our item view so the view actually where we can go and find those views so radio button i'm going to set it to item view notice it has to go through item view because this is what we have set up here to go through the view holder to get our view okay so the idea we're passing this view through our view holder constructor which is this viewholder constructor there which essentially we are calling in again this view that we inflated there all right enough of mechanics there okay see all right let's go ahead so dot and i'm going to say find by find view pi id r.id dot to do uh radio button that's the id i'm gonna do the same to others with a task item find by id i think i call it to do row to do like that and then today chip item view that find by id or that id that today chip like this all right so now i got a hold of all those we are ready to continue so let's go on bind view holders in our mind view holder here it's where we actually bind the views and the data okay so i'm going to say task because i need that object say task equal to task list remember we've created this task list here and we're actually setting up those tasks so that they can receive all of the tasks that means then we're going to go ahead and get one task at least each time right how do we do that we can say dot get right this method and we're going to pass the position of the row at this point so we can get the exact task that we want we're going to pass position and there we go so now we have a task object which we can do all sort of things so the first thing i'm going to do i'm going to just start with adding the text so i'm going to say holder remember this holder is coming from on bind view passes the viewholder class right object which is this viewholder here which internally we have these public fields that we can go and fetch right those widgets so we can set them up okay that's beautiful so i'm going to say holder say dot and get to do or task i call it task text view i'm gonna set the task to what oops a task that's set text like this i'm gonna use the task object we just got and say get task okay for now i'm to set up this one before we move forward because there's other things we are going to be doing right and let's not forget that the get count is very important because it needs to know exactly what is the size of this list so it knows internally how to set up everything and get everything going so i'm going to go ahead and say again task list dot size to get the size of the list all right and we should be good for now so let's go back to our main activity i can just double click shift there and find main activity like that recent files enter and this is what we get all right so what do we do well we need to instantiate first of all our recycler view some say private call recycler view okay and call this recyclerview as such i'm going to say private let's get our recycle view adapter we just created called recyclerview adapter and of course instead of on create somewhere around there below our task view model in fact we can do way before that because we're setting up the widget at first i'm going to say recyclerview and we're going to go get that recycled view right so i'm going to say it's equal to find by our id call this recycle view that was the id we gave and i'm going to set up the recycle view to say that set has fixed size to true just a few things and then recycle view i'm going to set layout manager which is responsible for laying everything out i want this to be a linear layout manager like that i'm going to pass this for the context all right there we go so we have our recycle view set up next we need to set up our recycle view adapter but we're going to do that inside of our lambda because it's all dependent on the data that's coming in what i mean by that is as follows so where we say task view that get all tasked we have this observe we pass this and we have all our tasks so we know we're getting tasks because we have this for loop there so i can get rid of that for now because we no longer need that here is when i'm going to set up our adapter so i'm going to say recycler view adapter say it's equal to new recycler view adapter remember and the recycle view adapter if you remember correctly it wants us to pass in what the list of all of the tasks makes sense where do we get those lists well those are being passed right under our nose there all right and that's it next i'm going to use a recycler view and set adapter what adapter well the recycle view adapter we just created and there you have it so now when we run this app we're hopefully going to be able to see something in our user interface let's hope for that at least fingers crossed and just like that folks we have things working so we have this to do to do and to do not very helpful because again remember all of those are these ones that we've added earlier and it's not very helpful so what i can do now check this out i can come up here let's see right about here inside to do i'm going to say something like this i'm going to change to task and because i want to make this somewhat customized i'm going to just pass a counter here we haven't created this counter let's create this counter create a field counter it's going to be a string actually actually an int that's what i meant to say and when we start our counter is going to be zero and each time we click we're going to go ahead and say plus plus like this okay and let's say this to instead of high let's say medium priority okay let's go ahead and run real quick of course we're going to see the same thing but then we're going to be able to hopefully click on the add button and hopefully add a more interesting task okay let's see if this works click in the beauty here notice the moment we clicked it was added automatically and because we are talking about live data i think this is always important to remember live data is really amazing it gives us this observable and notifies the user interface that hey some data was added something has changed and go ahead and change this interface all of that is happening without us having to do refreshing do anything else this is huge okay you should be celebrating about this because this is amazing all right so we have task zero it's happening there look at this i go next task one incrementing so these tasks are actually being added into our database and automatically shown there because we have our recyclerview working perfectly there all right we can keep adding as many as we want very good very good indeed and we can go to our database inspector there refresh you can see we have all of those tasks that are being added mediums and so forth very nice so at least now we're able to look at something at least we can see that things are being added and we can retrieve that information all of the tasks or to do's and they're shown right here and even the these buttons are working perfectly even though that's not what we want but at least we know things are working and remember if we go back to our recycle view adapter those are not set up the only thing that we populating is just this one field which is this to do that's why anything else it just looks kind of the same very well well congratulations and we're making really good progress and i want you to spend some time and look at how awesome this is especially when we talk about room database and i'm very excited about room database for many reasons that i've shown you i've shared with you in previous videos if you've been following us along and you should be also very excited about it good progress ladies and gentlemen i will see you in the next video very well so far things are looking really good and one thing we want to do is let's add the actual due date instead of today because this is just a placeholder text let's see how we do that so the idea really is we're going back to our recycle view adapter scroll down here we have inside of onbindviewholder so we have the contextual task object that we can use to populate as you see here so working fine now let's go ahead and see if we can add the date the due date there but for that we'll need to of course do some conversion meaning if i go ahead and say holder and get the chip date or today date so this chip here and i can say something like that set text if i said text i can say task that get due date like this now that's a date type now i can go ahead and say that to string and will give me the string representation but what i really want to do is i want to be able to format this date so that it looks better right so i want to say something like january 12 that's the due date instead of having a long date okay so let's separate the concerns here so so let's go ahead and separate the concerns let me close this down and also keep in mind actually let's go back to our database inspector the dates are saved as you may recall are saved as unix timestamp so it's a long number that we need to translate back into an actual date first i'm going to create a string called for matted it's equal to utils we haven't created the utils class yet we will call format date as such and then we're going to pass task the object and say get due date like this obviously this is not going to work let's go ahead and create our class here let's uh create details class and this is going to be say dot util like this okay that's where we're going to add this new class to and there we have it so we have our utils class here and for our utils class this is just going to be a simple method that will have functions or methods that will have helper functions or method the first one is going to be public static the string type right format date i'm going to pass a date object that's what's needed let's go ahead and import that there it is so there are different ways to format dates in android but we're going to use the simple date format class it's a simple date format class from java text that call this simple date format and we're going to say simple date format class get date instance like that now i'm going to have an issue because we have to wrap this around a simple date object let's close this down so more space right so once we have our simple date format object there we can use it first of all we're going to set it up so that we know exactly what patterns we want to show so what i mean by date pattern is for instance we can choose if we want to show something like this january 30th something like that or we want to also january 30th 2025 okay so we can actually specify that by passing a string so we say dot apply pattern it's a string pattern and there are different kinds of string patterns you can use actually to show different data patterns okay so in this case here you're going to say e e e three e's m m for month and then day just like that so this just tells us gonna show month and day and then i'm going to say return simple data format that format we're going to pass our date that we're receiving from our function all right save this now we should be able to see that we no longer have an error there so now that we have this formatted string that's what we can use to pass it along in our today chip that way we can get that formatted date so now if i save this let's go ahead and run hopefully we should see a nicer date and it's not working what's going on let's click here looks like here holder today chip said formatted on a null object reference let's go and check at bottom right that is the wrong one this is taking us to our bottom sheet we actually need to get from our to-do so that is the problem this is the correct id which is to do row chip i was getting the wrong id that's why it wasn't accepting it wasn't accepting that as an actual valid widget so it has to be the to-do row chip that's why i've appended this to do to know that these ids from the correct xml all right now we should be good let's go save and run once again and there you have it you can see now says to do is going to be tuesday january 19th because that's yesterday when we added it if i went ahead and added a new one let's add something you can see this one says wednesday january 20th that's what i added today if i keep adding see task 1 today's date all right so it is indeed working that's really good now one thing you also notice is that it's cutting off a little bit here our recycler view so let's see if we can fix that real quick let's go to main xml close this down a little bit let's give a padding of this entire recycle view say padding so it looks like we already have a padding there so what we can do we can say command and click this will take us to where we have our dimensions all right 16 actually works way way better so there we go now you don't see that cutting off at all there very well things are looking pretty good and in this lesson we created this utils class which holds this static method called format date so what it does it just formats the date so that it looks better like this and we're using that inside of a recycle view and we make sure to go and get the today chip and set that format date and this format date is of course coming from our task get due date now we're going to look at the bottom sheet fragment and essentially what that is is going to be the little pop-up we're going to see at the bottom of the screen where we're going to be able to add an item in this case a to do or a task to our database now why are we using bottom sheet fragment we're using bottom sheet frogman because it is another user interface widget in essence that is given to us for us to use and it makes our application have that feel of a single page application right instead of having a lot of different activities going back and forth we just feels as if we only using one single view or one single activity now a bottom sheet fragment as the name imply it is a fragment what that means is that when we want to create this bottom sheet fragment we need to extend a fragment or in this case this is going to be a fragment type now what is this fragment well essentially a fragment is a reusable portion of a user interface now fragments are very special for many reasons one of which is that they must be hosted by other fragments or activity so the best way to really understand fragment in general the usage and the reason why fragments are great is the ability that they give us to reuse certain components or views of our user interface interchangeably there's a lot of fragments than what i've just told you and the other reason why fragments are great is that it allows us to have a very uniform user interface so imagine that you're building an application that needs to run on android tvs need to run on android tablets and phones and so forth and also these applications they need to adapt to changes right so if the application is running on a portrait mode or a landscape mode so it is much easier to use a user interface that is more flexible in this case indeed a fragment will be the best candidate because we can create different kinds of user interfaces using fragments that will be called at runtime depending on the circumstances whether you're running on a bigger screen right a larger screen or smaller phone screen or on a tv and so forth so there's a lot to fragment that i'm not going to get into right now but i just want to give you some sort of an overview of what fragment in essence is and why we're going to be using bottom sheet fragment all right let's go ahead and get started so looking here in our project when we open our project you see that we have our bottom sheet fragment double click you should have this code already although it's very empty but it should have everything there okay so there we go we have bottom sheet fragment and extends bottom sheet dialog fragments so this is going to be a dialog that pops up and kind of grays out everything behind so that this bottom sheet is the central piece that's what a dialogue in android is okay so there's different kind of bottom sheet fragments of course but this is what we're going to be using you can see here we have a few methods and the main method here is the on create view now you've seen something similar before if we go back to our adapter there's a recycle view adapter okay if you go up here you can see there is this on create view holder which is kind of similar to what we have inside of our create view because here what we're doing we're inflating right meaning we are getting the layout and inflated and create an actual view object which is exactly the same that we had to do before in our recycle view adapter okay we use a layout inflator we inflate that layout to create an actual view so that is the similarity there that means this fragment here we are going to do the same thing we're inflating the bottom sheet let's go ahead and go to that this is what we're going to be inflating okay close all of that we're going to be inflating all of that so that we can actually show in our user interface when we click the add button all right and of course we look at our user interface here you can see our user interface we have all these information buttons that i've already created and that i have already created and you have access to all of these of course and it's very simple really i've got some drawables meaning images and icons i've added as buttons they all have ids and we have this very nice edit text here where we can enter and add the text that we want to save but most importantly also you'll see let's open this component tree here let's hide this around this is important for you to look at so also notice why there's a few things that are a little bit strange here in a sense that i've grouped together a few items now if you notice to the right we have this sign that says something is restrictive or it's invisible what that means is is that this view is gone when we run this app so if i click on this view and let's look say visibility it says it's gonna i'm gonna make sure that this is actually visible so you can see what's going on the moment you do that you can see now this is what is gone so the idea is when we first run this app when we click the button this shows up all of these views are going to be invisible at first and programmatically we're going to make them visible and within this view you can see this radio group priority okay i have a few radio buttons here so these are the radio buttons and i've added different colors to differentiate them okay that's we're going to be using in our code let me put that back to gun so you don't see it very good and the next thing we have here is the calendar group so i grouped together some of the widgets that i want to be hidden let's go ahead and visibility is gone i'm going to say invisible again so we can see exactly what's going on oh actually this should be visible there we go all right you can see here i have a text view at the top and i have these chip buttons there or i made them buttons because we are going to be able to click on them right so these are going to be helpful in a sense that for instance if i know that this task i want it to be done by tomorrow i can click just tomorrow and then in the background what we're going to do in code we are going to save that tomorrow in a date format actually in a unix timestamp so that we can save that so it's much easier as opposed to trying to go through our calendar and find tomorrow okay just the most convenient way to get users to add due dates there's also next week and so we do those calculations when they click we know that this particular task is going to be due next week of course they can do the same thing by adding or by finding in our calendar here so this is of course a calendar view you can see here is the calendar view it's a widget that comes with the android sdk that makes it way easier for us to look at a calendar and select the date and so forth so we don't have to do much this comes already pre-packed in android which is very very handy so this is what we have going on in our bottom sheet so there's a lot of widgets there and well organized and so forth okay so now that we have all of that let's see if we can pull all of this in our bottom sheet fragment let's go back to our activity there we go so that when we click this button here the add fab there this bottom sheet is going to pop up and show itself right about there okay all right let's go and do some work all right so here it is we have on create view so this point is pretty empty uh the good thing is that we're inflating our view so we're returning our view and i always like to actually say something like this view and take all of this like that i think it's the best way to do right now and then return our view it's easier that way because you'll see in a bit here we're going to be using this view object and get things from it and do all sort of things right so by now we have our view and of course it's going to complain a little bit give us a warning here because we haven't done anything with our view but we will so that's totally fine so at this point here when this bottom sheet fragment is called in our main activity that's part of a fragment is that it has to be hosted somewhere in this case we're going to be hosting it on our main activity what's going to happen is it's going to go ahead and inflate our bottom sheet this whole thing here in fact let me make sure that i have this back to what it was so that it's not visible so that we don't run into issues let's go back to right this whole group here has to be invisible or gone visibility say gone like this very good go back to that and so at least like i said we're going to be able to see this popping up okay so so far it's so good let's actually go to main activity and see how can we make that happen uh the other thing i need to have here one of the contracts we have with creating fragments is that we need to have an empty public constructor so i'm going to say public so i'm going to go ahead and create a constructor here like this so it's just going to be empty and that's one of the requirements that's very very important all right so let's go to our main activity here and set things up so at least we can see if we're able to invoke our bottom sheet fragment to show i'm going to say bottom sheet fragment call this bottom sheet fragment as such and then i'm going to instantiate it inside of main on create that is so i'm going to say bottom sheet is equal to new bottom sheet like this okay so we have now instantiated our bottom sheet the next thing we need to do we need to set up the actual bottom sheet meaning this whole view here because if you look at our bottom sheet xml you notice that we have this bottom sheet as the id get rid of that let's go actually go to code you'll notice that everything is enclosed inside of a constraint layout and we gave an id to this constraint layout which encloses all of the widgets so one thing we need to do with our bottom sheet is that we need to actually find that layout the constraint layout so that we can pass that through our bottom sheet behavior because all these bottom sheets they do have a behavior you can extend them you can do all sort of things okay that's one of the requirements we need to do in order for us to have a complete bottom sheet so i'm going to create a constraint type here constraint layout okay and then call this constraint layout i'm going to say it's equal to find by id r.id that bottom sheet so again we are getting this constrainable layout which is our entire bottom sheet okay so once i have that then then i can invoke the bottom sheet behavior such in this bottom sheet behavior we have to pass the type that is containing which is going to be a constraint layout okay and then i'm going to say bottom sheet behavior it's going to be equal set that to bottom sheet behavior like that say from and then we're going to pass our constraint layout that we passed there so now we have our bottom sheet behavior we can set up to what kind of behavior we want this to have okay so in this case here i can pass all sort of behavior so we're not interested in going through all these different behaviors we want to make sure though that the peak height right the highest extent of our bottom sheet we want to make sure that this is actually stays hidden okay so now i can say bottom sheet behavior and i'm going to say dot set peak height so if you notice there's a lot of things we can set on our bottom sheet behavior here okay so what we want to do here set the peak height we want to make sure that we can set to something that we can use so i'm going to say set peak height and in this case here i want this state to be hidden so all the time when this starts when we run this the state's going to be hidden which means the bottom sheet is always going to be hidden not extended always hidden so for that i can just say state start typing state and you can see bottom sheet has a few fields that are static that i can use so i want the hidden like that okay so we're setting up our bottom sheet behavior all right very good so when do we want to see our bottom sheet well what we want to do is we want to make sure that that happens when we click on the fab button so we're going to go ahead and just comment all of this out as such for now because we know that all works and i'm going to call here something like this show bottom sheet dialog such we haven't created that i'm going to create right now all right so what do we do to show our bottom sheet well here all i'm going to do i'm going to say bottom sheet fragment because remember we have instantiated our bottom sheet already at the top here okay i'm going to use that instantiation i'm going to say dot show and in order to show that i'm have to go to say get support fragment manager so the fragment manager is going to know exactly if we have multiple fragments it will know exactly all about the fragments that you have in your project and we'll know how to make all of that actually work for us okay so it's the manager we'll manage the actual fragment so we don't have to worry about anything all right that's the first argument we need to pass and the next one is going to be the actual bottom sheet fragment tag so let's say bottom sheet dot tags so the tag the get tag essentially is an id underlining id that is given to every fragment we create okay so this here we'll go ahead and show our fragment if all goes well so that's why we're calling that inside of on click of our fab button all right let's save this and run okay let's see if this works click and voila you can see we have indeed our bottom sheet fragment working there and the great thing about this is that we're given a lot of features so now to get rid of that i can just click anywhere outside it goes away we didn't have to write code that does that for us right so it comes all prepackaged because it is indeed a dialog so that means when we click somewhere out of our fragment here our bottom sheet it knows that you need to get out of it very handy and notice here how it darkens anything else beyond what we have in our bottom sheet here to signify this is where that tension should go to as users use this application and here you can click and start adding things and go from there okay you can get rid of that and voila very good so we're able to show our bottom sheet dialog of course it doesn't do anything because we haven't really written any code that will make our widgets inside of our bottom sheet dialog for instance we have this calendar we have all these other buttons so we haven't set them up yet which we're going to be doing in the next videos now we're going to set up all of these widgets inside of our fragment or in this case bottom sheet fragment so again the important place here is the view on create view right this is where we are inflating we're getting our layout bottom sheet that we passed here and make that an actual object which we can actually use to invoke or get as i've said before get all of these widgets that may be inside of our bottom sheet all right so how do we do that you've seen this before it's very easy we have our view so we can go through this view to set up our widgets so the first widget i want to go and get let's go back here now remember there's a few widgets there are hidden but we will get to those a little bit later so let's start with our edit text there we know this the id is entered to do et for edit text so we can start from there so i'm going to create instance variables here i think it's better i'm going to say private edit text like this call this enter to do i'm going to say private call this image button calendar button so another private here the image button this is going to priority buttons a private radio group priority radio group now i'm going to get a radio button say selected radio button these are all for some of the ones that we don't see they're kind of hidden all of these radio buttons there that's what we're doing there just and then private and selected this will make sense once we actually are working with these buttons let's get a private image button and this is going to be save button like this let's say private calendar view because we have that calendar view calendar view as such calendar view that's image button image button that's what i'm looking for let's say save button and then you can see that i've actually created you can see at the bottom here i did create that calendar group id this is the one that is hidden or invisible gun that contains all of these other buttons right remember the next week today and tomorrow so i put all of that in a widget called group because you can actually group together certain widgets so i did this because it's easier for me to just toggle that group for everything inside of that group to be either hidden or invisible etc okay and that's in itself it's a widget a group widget that's why it says calendar underscore group ah there we go so i'm gonna get that too because we're gonna use that to show or to actually make it visible or invisible depending on the case i'm gonna say group make sure we're getting the group android constraint layout like this and say calendar group all right so we have all of those instance variables so fields that we can actually use now here okay so first i'm going to say start with calendar group as such and say view have to go through our view and find by our data id that calendar group like that okay so now i have retrieved this calendar group i have a hold of it and then calendar view view dot find by that id calendar view calendar button same thing called today calendar button that was the title or id and then enter do the edit text view by say find by that is or that id enter to do this is very boring i do realize you can always fast forward okay save button view find by r.id that's save to do button priority button view find by or that id that to do button priority to do button there it is and last but not least priority and next priority radio group sql view fine are id that radio group priority very well now the reason why i made all of these fields of this class is because i want to be able to use them somewhere else or anywhere else in this class now there we have other widgets that we don't see here specifically we have this calendar so inside of our calendar group here we have other widgets like i showed you before so we have this next week chip tomorrow chip and today chip these are the main widgets that we also need to pull them through now because i won't be using them throughout this entire class so i don't need to put them as fields or instance variables so i can just instantiate them inside here so what i'm going to do i'm going to do the same i'm going to say chip okay make sure we get the chip from material android there say today chip user view find by our id dot today chip okay and then again let's say chip tomorrow chip okay these are going to be buttons find by r.id that tomorrow chip and then the last one chip this is going to be next week chip view dot find buy or our id that next week all right so we got that out of the way i know it's very tedious but we kind of have to do this so we have everything set up and ready to go so the first thing we want to do really is to make sure that we're able to pull or get information from our view or in this case our widget the enter to do right to get that information and save it because prior to that we're doing all of this here which essentially it's fine but that's not the goal the goal here is that when we click as you see we show the bottom sheet dialog and the bottom sheet dialog will take care of saving everything for us that's why we are pulling in this enter to do widget that it's an edit text so that we can get that information so really what we're going to do is remember we are inside of on create view and then we have another method here called on view created this is called as name implied the moment that the view is created everything is set up so we have set up all of our widgets and everything is good that's when this on view created is called so this would be the best place for us to start doing things like fetching information from our views and do all sort of things okay so in this case here all i'm going to do because i know that i have the save button there so what is this save button again it's going to be this save button here assuming that our bottom sheet is up so it's being shown we want users to add something and then hit this and then save that information so let's see how can we do that real quick so again i'm going to be inside on view created because the view at this point this is going to be called when the view is already created meaning all of this is set up so all of these widgets are ready to actually be used so here i'm going to just go ahead and say something like this enter to do now you understand why i made sure that some of these widgets are part of the class fields as opposed to inner fields because we want to be able to use them or get a hold of them anywhere from anywhere all right in this case actually it's not entered to do rather i'm going to say save button image button here that's set on click listener new on click listener and we know about lambda here to make our lives much simpler and there we go so what we're going to do here well we need to extract what we will get from our edit text which is this enter to do et there right which is our enter to do okay well i'm going to put on a string this is a string call this task is equal to enter to do that get text such i'm going to go ahead and say to string and then use the dream to get rid of all the unnecessary spaces and then i'm going to say if okay i'm going to use the text utils class text your tails class that is empty in this case if it's not empty because we have the not there okay uh pass the task that string is not empty okay for now and then i'm going to say if that's true then we're ready to rock and roll which means that i'm going to create a task object call this my task let's import our task class there i'm going to set it to new task and i'm going to use that constructor to construct a task so i'm going to pass our task string this one here okay because it's not empty at this point i'm gonna pass priority the priority for now i'm just gonna go ahead and say priority high because everything is priority high all right and then i'm gonna pass due date i'm just gonna go ahead and use the calendar object again java util dot get instance that get time that's fine and then do the same again for the other kind get time okay for i think this is going to be the enter or the the date created that is all right and then i'm going to say false for is done and there you have it so we've created again something similar that we had before we've created a new task here object so now it's time for us to do what to do well insert that task look how convenient this is i can go ahead and say the view model okay remember our task view model class and invoke the insert method which remember is a static method that's why we're able to say task view model dot and invoke the insert method and it needs yes a task object all right so it looks like we have oh not task that has to be my task there we go we're having an issue when we use just the tasks because it's going to say well this is a string and insert requires an actual task object all right and we have it there it is let's see if this works right so at this point we're going to be able to have a semi customized task let's save and go ahead and run okay let's try works perfectly let's go ahead and say something eat less every day okay enter like this and let's click on the save button this is a button actually so click that's true get rid of that and we should see that we have there eat less every day very good it is working so we're able to pull in the information we get from our edit text from our bottom sheet there and we're able to save that information into our database very good so it isn't it working obviously if you go to our database here let's go ahead and refresh we should be able to see there it is it last very high and we have our due date and created that and it's done of course it's a boolean 0 to say that this is indeed false which means it's not done if it was true this would have been one okay so it is working hello there i'm sorry to interrupt for a second here i hope you're enjoying this course this mini course and i hope you are finding it very helpful i just want to let you know that if you want to learn more about android development i'm talking about from not knowing anything to actually build applications such as the one that you are following along and building i have a course on udemy it's exclusively on udemy and i'm actually putting it out there on a huge discount so if you go click on the link below in the description there's a link you click on that that will take you to this full course now the course is loaded with a lot of information right now is about 46 or 48 hours of content so it's a lot of content of course you don't have to do it watch it in one sitting obviously but as you see it's the most comprehensive course on udemy on the topic of android development and i've just updated the course as well so you will get the fresh to newer information the newer tools that you need to build android applications okay so there's a link below go ahead and enroll in the course if you want to get more than just this mini course as you see here but let's go back to coding [Music] now let's look at this calendar button here because when we click on this button we want to be able to show the rest of the calendar view but we also want to be able to show the next week tomorrow today and so forth okay so all of these hidden group all right let's go ahead and see how we do that so we have our save button we still instead of on view created next let's go and get the calendar button there it is when it says dot set on click listener new on click listener such and let's make this lambda let's close this down for more space okay so here what we need to do again is to make sure that this calendar view is visible but it has to be depending on the situation so what i want to do is if you click first the first time if it's invisible of course it's going to become visible and if you click again we want to hide it okay so we're going to create some sort of a toggle functionality here so i'm going to go ahead and get the calendar group because we created that right we got it instantiated and everything so calendar group i'm going to invoke the dot set visibility and for that i need to pass of course the state okay now again because i wanted it to be a toggle situation meaning depending on the situation it's going to be visible otherwise it's going to be closed one click right so i'm going to use a ternary operator here i'm going to say calendar group this group that i'm talking about i'm going to say get visibility okay there is a method which returns true or false so in this case here let me go ahead and put a new line so we have the full picture so in this case what i'm going to do is going to say if that is true meaning if the view is gone indeed right then i want to make sure that i set the view to visible because that's going to be the whole idea again so if the view is gone and i click then we're going to show it if i click again i want to hide it so that's what i'm doing it and i'm going to say else with calling like this i'm going to say gone like that okay that's it so now if i run this i should hopefully get the results i'm looking for all right let's click there to open our sheet click aha and voila just like that you can see now the whole view is showing if i click back it hides like this and now that it's kind of extended i can also pull it up so you can see the entire calendar and other views or other widgets that we have there so all of these are feasible now and i can go through all of these months and i can also click and find the date all of that is given to us so the next thing we need to do really is to make sure that at least we have this calendar working meaning when we click we can then retrieve the date of the month the year and whatever else that we want okay click out of that index and there we go so how do we do that so we have the calendar view so it turns out the calendar view which we already have set up so i'm going to say calendar view notice it is indeed of type calendar view i can say that set on data change listener and then i'm going to pass here on calendar view data change listener like this and enter and this is what we have and of course we can always use our lambda it's always way easier and of course it gives us this very not interesting parameter not interesting parameters here that are being passed that's fine i know exactly which one are they the first one is going to be the calendar view and the second one is going to be the year and i'm going to just type it out and then the month and then the day of month and i can see that by putting something like a log d like this let's just say cal something like this and i can say month like that i can say plus let's put month like this and then i want day of month like that and i'm going to say day of month like that could put a year but this is fine all right so that means that if we click on any of those dates right we are going to be able to get the full date as we see here let's go ahead and run and see let's open our logcat make sure that it is indeed cal like this make it a little bit larger okay let's test there's the calendar i can pull it up a little bit and i'm going to say 14th i'm going to do that you don't see much right here but if we go to run sometimes we should be able to see let's say cal okay let's go ahead and run click let's go ahead and just click somewhere say click and let's just click one of these dates and you can see it says month zero day of month is one that's one click click the second one and there we go but why is it saying month zero well the reason why is because in the calendar everything starts counting by zero so the first month is january and it's going to be zero which is not necessarily helpful for us that's why we must actually say plus one so this month here i'm gonna put it inside of parentheses we're gonna say plus one like this to say that we're gonna start always instead of zero it's gonna go to one which is the correct one for us to see january let's run this one more time logcat let's open there another month just click you can see month two that's february day three make this a little bit bigger perhaps okay there it is so this is february let's go to march okay month three day four if i go to six there we go so every other month that we go to it's going to happen let's see and voila it's working june is indeed month six day three that's the one that we selected very good so we're getting the right information which is really good so now that we know that we are getting the correct dates and everything we need to then take this date that's inside here we want to take that information so when we click on this date wherever we go we want to be able to take that date object so that we can actually pass on to our to do our task right makes sense because we need that information for our due date how do we do that well the first thing we need to think about is okay where do we set up that due date well first of all we understand also go back to our app here that the due date can be taken or can be retrieved from different places because we can also not only get from our calendar view as you see here but we can also get that from these other buttons which we're going to look at next okay so it's important for us to create a variable an instance variable at the top here date type i'm going to say private it's a date make sure it is indeed a java util date okay i'm going to say do data such so we can set that up anywhere we want and retrieve that whenever we need that so we can set this due date anywhere we want could be inside of our calendar group could be inside of our calendar view or when we click either today tomorrow and next week does it make sense so let's start with our calendar here because this is the important part so far at least so the first thing we need to do here is that we need to create the actual date object which will then set to our date here now how do we get a date object or how to create a date such as today or tomorrow etc for that there is a calendar class that we can instantiate so i'm going to say calendar look at this this is calendar from java.util okay so it's not just related to android it's related to java in general so we can use that i'm going to call this calendar let's just go ahead and instantiate it right away let's say calendar dot get instance so this will give us a general calendar that will have the time the dates and so forth that's what we're going to be using to create the actual date or extrapolate the date from our calendar view and then transform that into an actual due date which then we're going to be passing to our database ah okay so the first thing we're going to do again we're now inside of the set on data change listener i'm going to say calendar the first thing you want to do remember it's a calendar class i'm going to go ahead and say clear just to make sure that we are have a clean slate and then i'm going to say calendar dot set the time and year in this case the actual date where do we get this date from well we get from our year month and day off and you can see here all these ins that we're supposed to enter so there's the year month and the date so in this case day of the month very good so we have that information already so what i do i say past year month day of month and voila now a calendar now has all the information needs to formulate to create an actual date which means now i can say due date and set that to our calendar that we just set up say dot get time so now we're going to have the actual date and due date that we need make sense so since we have our due date at this point here i can come back to our save here right right now we are hard coding everything for a due date i can go ahead and say pass the due date like this and i'm able to get the due date just like that because remember i made it an instance variable of this class very nice now to make sure that this doesn't crash we can also make this even better before we save our task i can say well the task should not be empty and due date should not be null for this to work okay so we passed due date and the date created still this generic now which makes sense because the moment we save this we're going to pass the instant when this item or this to do was created which is exactly what we want so that's totally fine but the due date now depends on what we chose all right let's see let's save this run all right moment of truth let's go ahead and i'm going to say newest item and then i'm going to click on the date button or the calendar and i'm going to just say on the second it's highlighted i'm going to save it's saved believe me i'm going to just go down here get rid of that and go there in newest item saturday january 2nd all right very good so it is indeed saving and when it saves it goes back of course we are retrieving all this information from our database it goes and does fetches all that data the item that we've added and of course the actual date and formats everything and shows there how cool is that all right and there we have it it is indeed working so now it's really easy because we have again this due date as an instance variable this entire class which means we can set it up with anything else that we have here in this case let's click done again here i can go ahead and click our calendar and extend that and then we have these three buttons that we can set up and then if you click click today and the due date is going to be today so we're going to transform that today into something that can be passed into our table which then we're going to save that and that is what we're going to be doing in the next video the other thing also you notice that the toggling actually works here if i go like this notice now that the calendar group is hidden click again it's showing all right very good i'll see in the next video now we are going to look at the chips these are let's click here you can see if let's add something gibberish here and then if you go to calendar we are going to set up this today tomorrow and next chips so these will represent dates like today tomorrow and next week so these will simplify give a user a more direct way to set up today tomorrow or next week for the task at hand okay very well so we're gonna get rid of that we do have the setup the chips set up here we are now going to attach on click listener to each one of them to simplify our lives i'm going to make sure that this entire class this entire bottom sheet fragment implements on click listener so on click click listener as such and click listener there we go which means then we have to implement a few methods namely the on click method there okay it's just a simple way to deal with this so in this case here for each one of these we'll say today chip that's set on click listener i'm gonna pass this to say that it's going to be looking at this on click listener register to this and click listener that we're implementing which means then we can retrieve or set up the events inside of one click here do the same to others tomorrow listen past this next week one click listener pass this as well all right now that we have those we can come down here the inside of on click view this is where we're going to look at each id that we are getting so that we know exactly which one is being clicked so i'm going to say int id set view dot get id so i'm going to put an if statement here let's say if id is equal to r id that today chip like that so if that is the case when we click on today chip what we want to do is set the date for yes today how do we do that well we can use the calendar object and so to get today as in when we click for today the current day what we can do is we can use calendar java util like such and then say day of year so this gives us the day of the year as in today and then we're going to pass zero for the next pass zero for the next parameter okay and then since now we have the today date what we need to do is we're gonna set that to our due date i'm gonna say due date it's gonna be equal to calendar again dot get time so now we're passing the current day today to our due date again keep in mind this due date is indeed what is needed when we save our to-do or task you see we have this to do or due date i should say very nice and because again we made it an instance variable for the entire class it's at the top here it's going to be recognized we can use throughout this class when we can set it anywhere we want in this class that's why we put it as an instance variable okay so do the same thing because we have other buttons here i'm going to say else if id is r.i.d that tomorrow chip that is one clicked we're gonna do the same thing so i'm gonna just copy all of this okay now the idea here is that i want tomorrow up to here this is today so to get tomorrow we add one to say that we want to add today plus one is going to be tomorrow that's why we pass one and then due date again calendar get time it's going to get tomorrow date so if today is the 12th tomorrow's gonna be the 13th that's why we add the one all right and our final one here else if id is equal to our id that next week chip like this if that's the case we're gonna do the same thing but now day of year it's gonna be plus seven right because it's gonna be seven days from today it makes sense that's why we passed seven there because up to here it's just today seven days from now it's going to be next week and we'll do the same thing due date calendar get time let's go ahead and put a few logs here log d let's say due date dot to string like this and let's say this is going to be time okay i'm going to copy this back that way when we click we can see that indeed we're getting the correct dates let's open our lock at time okay let's see let's pretend that we want to add something and get our date click today today is this date january 21st very good so tomorrow should be the 22nd that's very good and next week should be what 29 so we're getting the correct dates when we click all of these this is good because now the due date is going to be automatically be attached to the current task which is exactly what we want because if you go back then here the save button is going to have the due date it's not supposed to be null and we're going to get that due date depending on the circumstances be well and i'm going to say tomorrow so tomorrow's going to be january 22nd next week to 29th okay and go ahead and save i should be able to see be well 29th so make it a little bit bigger there we go be well on the 29th so it's working perfectly if i can go ahead and add maybe something new another task and i go to calendar and i want to actually use the calendar so i can pull this up like that and i want to say this is going to be a little bit farther out in my schedule how about the 16th april and then save you can see we should have another task friday april the 16th all right so it's working so what are we going to do next is we're going to go back to our recycle view adapter because that's where we actually see our rows being created in our cycle and so forth now the idea here is very simple is that we want to make sure we put together a way of responding to the on click event for each row and for that we're going to use an interface class because that will give us the flexibility we need to pass around that on click object throughout the application so ideally what we want to be able to do is to be able to click each row and then when that row is clicked we are able to actually pop up the bottom sheet fragment with an object that was clicked okay so for that we'll need a few things first of all we need to pass the current clicked object to the bottom sheet because remember these are two entities the bottom sheet is a fragment and what we're showing to the user in this case our recycle view adapter and so forth all of that is hosted inside of an activity okay so this activity is also hosting the bottom sheet fragment so we need to pass that current clicked object to the bottom sheet from activity to the bottom sheet and once we do that once we have that object clicked right the actual task we're going to allow users to edit that task and then save the changes in this case we're going to update that task so that is the main goal but first of course we're going to start small so really we want to make sure that we are able to click on that row and see something right and to do that we are going to create that on click interface class that we talked about all right let's go ahead and get started all right so first let's go and open our project tab let's go to our recycle view adapter so this is where all the fun really begins the idea again is very simple we want to make sure that we click on our button let's go back to our bottom we click on a row in this case let's go to our row to do row like such we're going to be able to click this row and see if we can pull in some information or at least see that we are able to invoke and capture the click event of that row all right so we're going to go to our recycle view let me go ahead and get rid of all other classes here that you probably don't need for now now how are we going to do this if you've been following us along you will recognize that we've had this issue or at least this situation before it's really easy we could have said something like this well we know that this item view indeed is this entire row already inflated we could just go ahead and say something like this at the bottom here say item view dot set on click listener and go from there but we're not going to do that because we want to do things the right way that's why we're going to use an interface to delegate this task of on click event so for that i'm going to go open again our project inside of our adapter here i'm going to right click and say new java class this is going to be an interface so i'm going to call this on to do click listener a rather long name but it's very descriptive and we're happy about that okay as we know an interface must have of course methods without a body because they will be implemented at a class level that's the whole idea so i'm going to say void on to do click as such okay but there are a few things we need to think about so went when onto do click is implemented at a class level doesn't matter where right what we want to do is we want to be able to pass the position of that row that was clicked that's very important so i'm going to say int i could say adapter position okay like that and if i want also i can say i'm going to pass a task object along let's go ahead and import our task like this all right now where are we going to implement this onto do click all right let's go back to our recycle view adapter for all this to work we need to pass along our own to do click listener when we create our cycle view adapter because remember that the row we are talking about it's part of the recycle view adapter in fact this is what we're doing here right we're using our layout inflator to inflate the to-do row to make that a view an object which we pass through our vholder class at the bottom here okay very good so that means then we are going to create here a field of own to do click listener so we can pass it around so i'm going to say private make this final because we're going to actually pass it through our constructor here and i'm going to say on to do click listener there we go call to do click listener section so you can see again it's going to complain because we need to pass that initialize that in our constructor so we're going to pass it along as well the on to do click listener i want to do click listener so that inside here i can go ahead and say own to do click listen like this so i'm going to say actually this to signify now setting up this to do clicks listener at the top here to whatever we're going to be passing there okay now the next thing we need to do is we need to go down here to the viewholder class there is a field holder class we need to do a few things here first of all this is where we have again our item view which is indeed this whole view that we're inflating right so what we're going to do actually here is first of all we're going to make this class implement the on click listener this is not our own click listener this is the on click listener that comes with the sdk make sense because we still need to listen to the on click event of that row and naturally we need to implement the methods which is on click inside here all right but this is now where we stop because think about it inside of our view holder here we have this on click so we have all these views we can attach all these views to this one click listener but what we really want to do is that pass along information onto the onto do click listener so our own click listener which you remember will have this onto the click and we're going to be able to pass adapter position and a task if we need to do so so that means then inside of our view holder class here we need also be able to pass the onto do click listener so i'm going to say on to do click listener on to do click listen like that now where do we set this up well inside of our view holder constructor because this is where we're setting everything up the widgets radio button the task view and the chip this is where we're going to set up our own to do click listener as well so i'm going to say this dot onto do click listener i'm talking about this one here we're passing there it's going to be set up to what it's gonna be set up to the to do click listener if you highlight that you're gonna see what it's gonna be highlighted is indeed and posted my column like that so this to do click listener view command and click is going to go ahead and point to the class level this main class level to do listener there all right very good so that is set up for us so now our own to do click listener is set up it should work fine all right there it is now the next thing we want to do of course let's set up the view this view here so make sure that when this is clicked something happens we can do that by doing the following in fact this is the way i started this video by saying this is not how we should do it right by saying item view dot set on click listener as such and passing the new oneclick listener so this is okay as long as we're not doing something like this new on click listener and then do the code that we need here right this is not what we want you will see in a second here so in fact i'm going to pass what here well because i know that this class here meaning this view holder implements the view and click listener right so i can just pass this to say that this is going to be actually connected register to our own click listener okay so we want to retrieve we want to uh keep track of those events that we get when we tap or click on this view row very well but now what really needs to happen well when this row is clicked what we want to do is we want to make sure that we receive that event and pass the information we want what does that mean well it means the following in this case here i'm going to just put int id the reason i'm doing this is because we're going to have different views that we're going to be clicking right so i'm going to just put the id inside of a variable so i'm going to say view that get id so i know exactly which view is being clicked again so far we only have one item that we setting on click listener but we're going to have another one so it's nice to have those ids so we can choose which one was clicked then i'm going to say if id right is equal to r.id dot to do row layout meaning this whole layout here see to do row layout that's the id this whole thing anywhere if this is clicked then we're gonna respond to that right how do we respond to that click well this is where we use the own to do click listener and set up the onto do click meaning this is where we're going to pass the information we need so that the interface can delegate that information to whoever is going to be implementing this method this is what i mean in this case here i'm going to say onto do click listener and say dot want to do click look at this and then i need to pass what i said we need to pass the adapter position how do we get an adapter position meaning knowing exactly that this is the row number or index are we clicking because we're inside of a recycle view adapter ladies and gentlemen we can go ahead and say get adapter position as such and also is expecting another argument here which is a task object well i can say here cur task like this we haven't created that well i can create that task how well i'm going to say task current task is going to be equal to task list that we have in our we created i should say here task list and inside here we populating the task list with whatever we are getting when we instantiate our recycleview adapter so this task list at this point should have a list of tasks from our database okay so we're going to use it to say dot get and pass what the get adapter position as such to get that object now what you could say well because we are passing the get a double position once we get that adapter position we can go wherever we go we can actually invoke that and get or do something similar like this and get the actual object why are we passing this object it's okay at this point you will see later why we even passing that object instead of just stopping here at the get adapter position because with the adapter position the position of the element we can always infer and get the object as you see here right and there you have it so now what's happening is this is very important to understand because it's a little bit of work and we can start feeling the pain why we're working so hard for something that we could have done the other way around which is the easiest way and actually the wrong way of doing this right so what's happening here again is very simple we created this onto do click listener and then we created this void on to do click which requires us to pass adapter position and task this is an interface right so what we do then in our recycle view because that is what governs everything that's where we have our recycle view right as well as our row so we can then attach in each of the rows inside of our view holder we are attaching a click listener this is a normal click listener just so as you see here view that on click listener we know this however when we want to override the on click event right because on click listener you have to override this on click method there that's where we then invoke the own to do click listener our own click listener we created and pass the adapter position current position of the row that was clicked and of course the current object that was clicked right this is really beautiful that means any class that will be implementing this onto do click listener will have the correct position of our object of our row and the correct task object just from clicking right check this out now if we go to our main activity and go at the top extends up compact and we are going to say implements look at this on to do click listener if you do that as you know we are now forced to implement methods in this case just one method look at this we have one to do click there it is that's what we want if you just click like that and look at that now since we're implementing that onto two click right this one here which incidentally this is one we're passing we're passing the get adapter position and the current object now we have access to the adapter position and the task right look at this i can say log d and right now let's say i want to just show the position i'm going to say adapter position to show you that indeed this works for the tag let's just put something more exciting i'm going to say item for the tag let's say click like this all right moment of truth is this going to work looks like i'm having issues ah there's the problem very well this is good well what's going on here well it turns out our recycle view is complaining because we changed a few things in our recycle view remember look at this in our cycle view adapter now we have two parameters instead of one we have this onto click listener and of course the task list that has changed and also the order in which we add these is very important that means then let's go back to our main activity here this no longer will work because we first need to pass something the first thing we need to pass is the tasks of course and then we need to pass this to signify look at this this signifies the on to do click listener the reason why we just can't pass this is because remember again the entire class implements onto to click listener so when we say this as you see here our id tells us exactly what this means it means on to do click listener so we're registering our own to do click listeners within our cycleview adapter so now it should work because it's going to go ahead and listen to this onto do click listener and run this very good so let's run this and see so each time we click we should see the adapter position of each row that we click so click this one you didn't see anything but let's go to our logcat i think i say click look i want to do click that was zero was clicked i'm gonna click again zero was clicked row one because remember the ids or positions starts are zero right just like an array and this one here three i can go all the way to the bottom let's see more that one looks like number fourteen and it's working with that i can also because we're passing because we're passing the task here object it should be an actual object so i can say task let's get the actual task how is that going to work let's run shift plus f10 for windows just look at the bottom here i'm going to click to do was clicked i have a lot of to-do so let's go ahead and find task 0. look at that the correct row is showing task 1. let's go all the way to the bottom another task click that and you can see another task is working all right so our system isn't it working what we set up is working and this is beneficial to us because now we're decoupling our code in such a way it's better and maintainable and this is the recommended way to always use interfaces whenever you want to pass around click events very very important and again i understand it's a little bit of work but we must do some work to get good results to write a good code it requires a little bit of work but most importantly i want you to start thinking in those terms instead of thinking well it's a lot of work why am i doing this you're doing this because you want to create solid applications that not only run but they actually run well and they're well constructed all right very good so making progress in that we're able to click on one item and get that object and so forth the beauty here is that we can do all sort of things now right imagine that you want for instance to pass this object onto the next screen let's say when we click any of these rows we want to go to a different screen well it's very easy now because we can just pass this object or parts of this object or we can just pass this adapter position and when we get there we can then show this task object and do all sorts of things all right so our strategy is going to be different from that as i have explained earlier is that we're actually going to figure out a way to pass this data back because remember we are inside of an activity which hosts a fragment right we can see it here but when we click there you can see that fragment is hosted there so we want to be able to when we click it lasts every day then we're going to be able to send over all that information onto this fragment here so we can edit that all right so okay making good progress let's continue in the next video so next we are going to continue with inside of our recycle view adapter because look what we did so i said earlier that the reason why we extracting the id and check for ids before we do anything in our own click is because we will have the opportunity to attach another widget to the set on click listener so now is the time what widget we want we want this radio button why because this radio button here is what we want to make sure that when clicked we are going to delete to say that this indeed is checked we've done this task so we are going to move on with our lives all right so naturally we'll have to say something like this radio button gonna set on click listener again and pass this okay very well that means then we are going to put another else if here if id is equal to r.id dot to do radio button okay if that's the case then we're going to do something else now what is that we're going to do well we still want to do the same thing we did with onto do click right because the mechanics are going to be still the same we want to be able to pass along information about this particular row that was clicked but more specifically the radio button that was clicked so we need to go back to our onto the click listener because we can add as many methods as we want here so i'm going to say void just differentiate i'm going to say onto do radio button click like this and for this one i'm just going to say int adapter position that's all okay all right so back to our recycle view i'm going to go ahead and say again same thing onto do click listener dot on to do radio button click and pass of course our get adapter position just like that now because i know that on to do radio button click when that is clicked we want to delete the entire row instead of passing the get adapter position i could very well just go ahead and say task here right because it's very possible just a task which means this no longer works that means i need to pass the actual task well how do i get that task well the same thing as we did here right so what i can do i can just extract this task outside still inside of our method there now depending which one was clicked we can go ahead and pass that exact task say current task in this case is equal to task list get and pass get adapter position like this this works fine in this case i'm going to just pass the current task like that and to even make it because we're creating these objects there i can just go ahead and create this object right here i'm just going to cut all of this and say it's equal at the top there so the moment that's clicked we create a task and we go from there because the task is going to be the same regardless makes sense so i'm just going to get rid of this so that just saves us from creating redundant code and objects and there we have it right so now again we're passing the task and going back to main activity here you notice we still have an issue here because we need to implement another method why does that happen well it's because of course onto do we just added this onto do radio button click listener right so that's why because we're implementing that's going to say hey there's one more method you need to implement because that is the contract so want to do radio there there it is so now we can do what we want to happen when the radio button of each one of these items is clicked so to show you i'm going to just copy this what i have here real quick so you can see exactly that this is happening i'm going to say on to do click get task sure that's fine i'm going to go ahead and run just open our logcat still click all right let's click this task i want to do click looks like it's showing right i just clicked i clicked the row let's click one of these radio buttons here let's make it look a little bigger let's start with this one click to do is showing there because that's what we're getting we're getting the task right let's go to this one task two you can see task two was clicked and on to do click and this is gonna be on let's say on radio button it's probably better that way so we can distinguish we know that this is happening but just to make sure you know that i'm not lying to you all right let's click this one on radio button task two was clicked i can go be well there it is this one and voila very good so the infrastructure is indeed working and again like i said the idea here is that when we click on to do radio button we're just going to go ahead and get rid altogether of this row as well as deleting this item because that will signify that we are indeed done with this item we've done it okay so in this case here look how easy it is i want to delete this task how do we do that well we say task view model dot delete for deleting a task we need what we need a task object where do we get a task conveniently that's exactly what's been passed there so say task voila that's great now usually when you want to delete something you probably need to ask the user whether or not this is exactly what they want to do okay so that's something you can do yourself create a pop-up all right so technically when you want to delete something you want to prompt the user to make sure they are aware that once they delete they no longer will be able to see this item okay so in this case here we're just going to do this way so i can show you that this exactly this works and once we've done that i can go ahead and say recycler view adapter and invoke the method called notify data set change that way it's going to go ahead and notify that hey something changed and we run or refresh the entire recycle view let's run this okay moment of truth let's see if this works so i'm going to click this one and that's gone it's kind of hard to see because we have a lot of to-do's but check this out notice the list is shrinking that's gone that's gone everything is gone we're deleting a lot of things look at this right we can continue let's delete this guy and if you wanna see go to database inspector it's gonna take a few seconds to actually pick up our device there we go i'm going to go to database let's go say select all from table task table run notice there we go and i'm going to make this live so that these updates you can actually see them being deleted i've been too kind i'm going to get rid of that voila it's leaving another task and that one as well okay very well we've just looked at radio button clicked meaning we were able now to click on each radio button and be able to actually delete that task and remove the row so that's really awesome the next thing we need to think about is how do we pass data between activity and fragment right because the idea here is very simple we'll have an activity in the forefront so we can see all of to do so that's an activity which encloses or hosts our bottom sheet fragment so we need to pass that information we have in a row onto these bottom sheet fragments so that we can then edit and save back or update that task again the question is how now there are a lot of different ways to do that and some of them are not recommended anymore therefore i'm not going to talk about them but now the best way for us to accomplish this which means communicate between activity and fragment in this case passing the data this object onto the fragment from an activity we're going to use view model okay so let's look at what the viewmodel is so it's actually a viewmodel sits in the middle of our activity and our fragment okay so the idea here is that the view model is the mechanism that will hold the actual data right independent of whether we are an activity or a fragment or any other kind of user interface entity or controller okay so think of it as something that is just sitting somewhere and we pass the information that we need and then whenever we want to retrieve that information right we just invoke it to get that information so if you model what it does it prepares the data needed to be shared with or between other activities or fragments in a life cycle conscious way this just means that the view model it's its own entity it doesn't care or doesn't really allow to be bothered with issues of life cycle as you know activities and fragments they do have their own life cycle so if the view model were to be attached to a life cycle that means if something changes in the life cycle it will have to be withdrawn or we would be able to or in this case we will lose that data so that's the beauty here of viewmodel anything that we attach to a viewmodel is just going to be some sort of a bundle that will be sitting out there we can communicate with to get and send out data to anybody that needs it without being compromised with lifecycle and that's we're going to be using to facilitate this data swapping or data sharing okay let's go ahead and take a look the first thing we're going to do let's get rid of some things that we no longer need here so that we don't have a cluttered workspace let's open our project and inside of our model we're going to right click and create another view model here so this is going to be i'm going to call this shared view model like that okay so for this to actually be a few model i'm going to say extends view model very nice there it is so this shared view model here essentially is going to hold this data that we need to pass it around but it will also allow us to create methods that will set the viewmodel and allow us to get that viewmodel in this case just the data the object that we want does that make sense so the first thing we are going to do here is we are going to create immutable live data so mutable means it can change depending on situation okay so mutable live data is just the live data that can actually easily change throughout the process of moving it around and do all sorts of things so i'm going to say private final mutable live data and it's going to be a task object like this i'm going to call this selected item i'm going to say it's equal to new mutable live data okay so now we have the selected item it's going to be immutable live data all right and so next i'm going to do getters and setters because we want to be able to say okay if we are inside of an activity we want to set these view model this data that we want to be able to hold as a few model so that then we can pass it around to whoever needs it okay so i'm going to say public i'm going to make them public because i want them accessible by anybody void i'm going to say select item okay and what item is this is just a task object like that and this will do what so i'm going to say selected item notices immutable live data right i'm going to say dot i'm going to set the value of that selected item to what well to our task so now when we say shared view model select item we will be expected to pass that object that we want to select to say okay now this is part of our mutable live data which indeed is a view model okay so we're setting that's it and now we want to be able to get it so i'm going to say public now i'm going to expect to receive a live data type right because that's the type that we want of task i'm going to say what get selected item we don't need to pass anything so return now i'm going to return a selected item like that now where do we instantiate first this view model this shared view model well we understand that we want to go to main activity because this is where all of this is being hosted it's showing right so that means then when we click one item this eat less every day that's the only thing we have we want to be able to set our view model to this object here because then we're going to be able to push that to our hidden fragment right the bottom sheet fragment so for us to initiate or instantiate our view model well i'm going to create a private here say item and say shared view model called this shared view model and then on create this is where we do pretty much everything i'm going to instantiate our view model you notice that we have this task view model here we created a long time ago right this task view model is the same concept as the view model that we are talking about here except this task view model here inherits from the android view model let's take a look so that you are refreshed remember so this concept is exactly the same we have these getters and setters we're setting things around okay the difference here is that this is android view model so it has more contextual android features whereas our shared view model it extends or inherits from view model class okay and if you hover over it says it class that it prepares responsible for preparing and managing the data from activity or fragment etc ah very good so all these mechanisms are put out there for us to actually use to facilitate our lives as developers you have to always think in these terms all right so now how do we instantiate that we're not going to instantiate the shared view model we created the way we did here it's a little bit different but not that different so really i'm going to say here shared view model it's going to be equal to new view model provider and then i'm going to pass the owner this right and i'm going to say dot get past the class it's going to be the shared if you model that class like that that's how we instantiate that now we can easily say something like the shared viewmodel. look at this set or select item we pass our task and so forth easily now where we want to do that right well we want to select the item when we click on this row where does that happen that happens whenever we call the onto do click make sense and you notice here we have the task already so we can use that by saying now we're creating the actual view model meaning we are establishing the viewmodel that is going to be able to pass that information along to anyone who wants that information in this case our bottom sheet fragment right so i'm going to say view model or shared view model okay dot select item and pass our task and really when i think about it this one to do we can change the signature here because this adapter position here is really isn't needed right we just want the task so what i can do let's go to our to-do click actually where it's yeah there and i'm going to get rid of that it's going to create a few problems there no problem let's go here i'm going to get rid of that this just makes our code more succinct and better anyway and then looks like doesn't like that of course i'm not passing that in a moment okay there we go it's much better we could have left as it was as you saw but i think this is just succinct because we just get the object and we done okay and there you have it so now when we say share review model that select item okay what this does simply is that anywhere else that we go ahead and implement share view model or call the shared view model we're going to be able to collect that information now where is exactly that we want to be able to retrieve this information that we've just saved in our shared view model well it's going to be inside of our bottom sheet so say bottom sheet fragment class there there it is so what do we need to think about here well first of all i'm going to create our view model and say private okay and shared view model share the view model like such now where do we go ahead and invoke and get that information it's not going to be on create view close this down because on create view this is where we're just creating the views inflating things and making sure that we actually get the view right we get inflated the object view that is right however the on view create this is called when all of this is done which means this is the place where we want to actually invoke the view model to get in this case the shared field model to see if it has any information that we need remember now we are inside of a bottom sheet fragment so inside of our main activity what we're doing here we created the share view model right we select item and pass the task object so now our view model has that task the information we need that means now we can retrieve it from anywhere so happens that we want to retrieve it from our bottom sheet fragment because we want to be able to pull that information in case the person wants to edit this new task or this task i should say and update it okay so what we do now somewhere here instead of on view created that's very important i'm going to say view model right i'm going to instantiate it actually share the field model okay the same thing we did before i'm going to say new view model provider and in this case here i can't say this because this makes no sense because we're not inside of an activity however because we know that bottom sheet fragment as a fragment is being hosted it must be hosted by an activity right so we can invoke the activity by saying require activity like that call that method and we can get that context and say dot get the class is going to be shared if you model that class so this shared view model is essentially the same as this shared view model here because remember this is sitting somewhere and will communicate it knows exactly if we select item if we set it up somewhere and then we're passing somewhere else it's going to have the same integral the same information ah very cool right so at this point here what i can do is because i have shared view model which means from main activity we have selected a task that means now i can come down here and say something like this i can say if the shared view model dot get look at this right we have this get method get selected item dot get value right because remember that in the shared view model here this selected item here we set the value because it's immutable live data right just think of it's just like a bundle we're wrapping this task into a live data that is mutable and that's why we can set value to that bundle that box so we're putting this task inside of this box selected item which is a mutable live data right when we say get selected we're returning the live data of task so the actual task that was put in as a multiple live data which also includes just live data right and we can remove that or retrieve that is right so i can check to make sure that there is indeed something so i'm gonna say it's not null okay if we have something selected right because sometimes we may not have anything it happens right in this case here then i can do something like this i have that information because i know that i have the object right i can even say something like this look at this i can say task task is equal to shared view model that gets selected that get value like this there we go so i have a text object in this case here i can say log d and say task dot get task let's say my like that so we can see the differences i'm going to save and before i go anywhere i want to make sure that whenever we say on to do click right whenever we click on our row i want to also call the show bottom sheet dialog it makes sense because that's when we're going to be able to go through this process of oncreate right this method here on creativeview is created to create everything and on view created also that's very important and that's the idea really is that when you click that will pop up and eventually we're going to pull in the information that we've clicked onto our edit text and go from there okay let's go ahead and run and let's go to our log cat and make sure we have i think it's my like this okay okay let's go ahead moment of truth click and voila it lasts every day has been passed around and this is coming from where on view created which is exactly here it may seem very trivial and insignificant but this is crucial what we've done here because of view model we're able to pass around this object okay so if we add let's go ahead i'm gonna add something new here new task okay and i'm gonna give i want this to be tomorrow and let's save that okay so all right there's new task okay i'm going to do it again this is a new task click look at it and went ahead and showed the new task very good so it's working that's for sure okay i'm going to click every day again and there we go every day now showing new task here is because our edit text is always showing what was there previously but we're going to fix that later but at least i want you to know that this is working perfectly this is huge because now that we have that information through our view model we can do sort of things one of which of course is to make sure that we populate our edit text as you saw there correctly and we can start doing all that and we can allow users to update this task very well so things are working great however let's check something out here okay so let's open again our logcat we have my okay the tag let's click here notice eat less every day everything is working however if i click out and click new task again we see new tasks working in our log here however our entry to do here because we're setting the task and we're setting to the same task object right but we still see here it less every day what's going on what's happening here is that we must make sure that the set text is actually cleared before we move forward but you will see that this strategy is also not going to work if we just go ahead and say for instance when we enter this if statement we can say enter to do that set text and give it an empty right so to clear it out let's see if that works all right i'm going to click the first one eat less that's fine but then i go away come back click this one it lasts still working and the same problem persists so the problem here is very simple really is that because we are inside on view created right this is the it's called once the view is created so this is not the place where all of the widgets are going to be uh refreshed so the widgets are not going to be refreshed actually because by this point the widgets have been created and they're set that's reason why this is futile it's not even doing anything the best place for us to put this code to actually see it working it would be on resume because on resume is going to be called when the fragment begin interacting with the user which means everything will be refreshed then doesn't make sense so check this out if i go back outside of this one view created and start typing something like on resume right for fragment here and i'm going to take this code all of this cut it out and put it there you will see that we won't have those issues anymore and we won't need to go ahead and set the text to empty because everything will be recreated every time this is called which will be called every time that we go out and back like that okay let's go ahead and run okay moment of truth of course the logcat is still open there then click there it lasts every day right that was clicked and i'm going to click new task if all goes well we should see this repopulating showing new tasks because that is what's clicked click outside click again new task is there all right if i click out and go back to eat less we should see less there this is good so this is where our if statement should be inside of resume so that we can get what we want in this case be able to update dynamically the edit text notice that the instantiation of our shared view model that happens still inside on view created because this has to happen when all the view have been created that's very important distinction here okay but because we have our shared view model as an instance variable of the entire class that's why we're able to call it whatever we want including inside of our own resume callback method lifecycle now the question is okay so we have everything set up like this so that means we are pulling in the correct object right new task there it is i can go up like this and click like that or click like this right and try to add something else and i can go to look at the date i can go ahead and update and save that so the idea is we want to be able to save a new copy because this will tell us that indeed we are trying to update or edit this current clicked row now how do we approach that because at this point here the way things are when we populate our fragment here bottom sheet fragment we have the correct object but now the idea is as follow how are we going to distinguish between saving and updating because if we click any of these so we got it to work so we pull in the correct object the correct do or task right like this if i go ahead and say for instance update like this right and i click here and i select tomorrow and i click update or save in this case it's not going to update it's going to go ahead and create a new copy as you see it says new task update and we still have the new task previously added so this is not updating so we need to find the mechanism that will work for us that will allow us to know okay if we click any of these that means now we are going to update or we are trying to edit the current row the current task that is updated not saved as a new instance of this object or of this task so for that there's different mechanisms to do so let me go ahead and delete this one okay we can do that using different mechanisms but i'm going to propose this other mechanism because we understand that item view model or a view model that we created in this case here shared view model this allows us to pass through messages okay from our activity to fragment and vice versa if we need to do so so what we're going to do is we're going to take advantage of that and create another instance variable here i'm going to say private it's going to be a boolean called is edited like this okay or maybe is edit so that we know is this going to be for editing or it's just a new item that we're trying to create because the mechanics are still the same in order to create a new item we have to pull in our show bottom sheet and add something right so we're using everything but we need to distinguish whether we're coming in to edit an item or we're coming in as to add a new item new task so let's create getters and setters here i'm going to say public void set is edit so to set is edit of course i'm going to pass a boolean is added like that and then i'm going to say this that is edit is equal to his edit we're setting that now we also create a getter here so i'm going to say public gonna be a volume but that's what we're trying to get get is edit no need to pass anything which is gonna go ahead and return is edit so when we set is added either true or false and then we can get that value okay so that means then we go back to main activity because that's where we are passing information through the on to do click because all of this is still going to happen when we click this item here so first one we shared we select the item the task and also what i'm going to do here i'm going to use share view model right same class i'm going to say set is edit to true because i want this to be true meaning if whenever they click the entire row i want to add it that's the whole idea all right so i'm going to send that information or at least i'm going to contain that information through our shared field holder that way i can pull it back in in our bottom sheet fragment so in this case here inside of our on resume because that's where we put everything right we moved everything to what i'm gonna do now actually at the top as an instance variable i'm going to say private boolean is edit such okay and then inside here where i know that we have something in our shared view model i'm going to say is edit i'm going to set it up here it's going to be equal to shared view model that get is added okay so now it should be true because we know that there's something that we're getting from our shared view model and we set it all up inside of our main activity to true because we want to edit so if that's the case then because that's what we're setting things up here we know is that it is true in this case we're going to go back to our save button here so we're going to do something different here in our save button what we need to do is we need to further look through the conditions because as it is right now it is set up so that we can create a new task right but that's not what i want to do so i want to make sure that we have this if statement all is good but also we want to make sure to check before we insert anything right so i'm going to add an if statement inside of this if statement say if okay if it's added it's true i can just say is edit right if it's true then we need to do something else right that means we need to update this item so i'm going to say task call this update task as such i'm going to set it to what i'm going to say view model i'm going to say shared view model that get selected item right going to get the value and it's going to be the task object actually has to be just the get value that's how we're going to get the actual object like this right so i'm going to say update task let's set task to what well to the task which is going to be this task here because at this point our enter to do will have the current task because we set that up just now right and then i'm going to say update task so i'm setting up this new object here i'm going to say set date created to get a date created i'm just going to say calendar that get instance that get time because this is when it's created right now as we when we update this right and then i'm going to say update task.set priority and for now we haven't set up our priority i'm just going to say priority hi okay we're going to change that later and then i'm going to say update task that's set due date right the due date is going to be the same because regardless of anything if we change the due date as we want to update or are editing this task we're going to still pull in the correct due date so we don't have to worry about that and then we're going to do something different instead of insert this is an update so we say task view model and then we say update well to update we need to pass the task which is this new loaded updated task this is how we do it and then i'm going to say else in any other case of course is going to be what we have here so i can put inside of curl braces but it's just one statement so this will work so let's test this out to see but before we do that let's make sure to think a little bit here so if is added is true that's when we actually know to update make sense but we also want to make sure that once we have updated we want to set is added to false okay just for a good measure so i'm going to say in this case share viewholder i'm going to set is added to false right away all right let's see if we can edit this one here click wonderful it's going to say update and we're going to change for tomorrow or any date really and let's see right nice it's updated now on the same token let's see if we can go ahead and create a new one let's delete all of this by the way just it's residual from previous of course and i'm going to say eat less so i should say next week that and click we should see eat less there we go and if we go ahead and if we wanted to also edit this one we can certainly do go back and date and we can say that's fine and save it's gonna give us the update okay so it is indeed working so we're making real good progress here but there are a few things uh i think we need to address before we move forward and work on other big things the first one is you notice something that is a little bit annoying here so if you click and want to add something let's say i want to add eat lots of bread as such and i say enter and then i go to the date and say tomorrow for instance and i go save the the keyboard persists here and it's not really great user interface or user experience but also i would be nice to make it so that whenever we save we'll collapse everything so at some point of course we hide the keyboard and secondly we also collapse our bottom sheet and we can see it lots of bread was added that's kind of handy all right so the first thing we need to do let's go to our utilities there we have this utilities i'm going to create a function here now this is going to be a utility function which will allow us to hide the soft keyboard so i'm going to say public let's close this down public static void hide soft keyboard such and then we can need to pass a view like this okay let's go ahead and import our view very well so what we're gonna do here first of all we're going to have to invoke the input method manager such call this im okay and let's cast this to input manager before we go forward i'm going to say view dot get context like this i'm going to say get system service and inside i'm going to use context and it's going to be input method service like that all right so we're getting a hold of our input method manager there so once i have a input manager i'm going to use that to say hide soft input from window as such and i'm going to say view that get window token and pass flag 0. so this is the code that really frankly speaking you should just have in your toolbox so that whenever you need it you can use it so this is how you hide the soft keyboard so where are we going to be calling that let's go back to our bottom sheet fragment and there are a few places where we could call it the first one is going to be when we click on the calendar button let's go find where we have our calendar button there we go so here we are setting the visibility of the calendar and it will be nice to go ahead and say utils dot hide soft keyboard and pass our view like this let's go ahead and organize this code better i think that's much better like that or even put that in one line all right okay let's add one let's be be happy when we do click notice now that we have this view showing our calendar as we said here now the keyboard is hidden so we can play around with what we want here okay okay i'm gonna go save okay all right let's add one item call this be happy click tab if you click we should see now it hide the soft keyboard i'm gonna say tomorrow i'm gonna go in save we should see that now be happy is indeed saved very well the other thing that we need to do here is that when we save we have an if statement the main if statement works fine but we forgot to add the else all right the else here what is it that we want to happen in this case here i'm just going to add a snack bar that make i'm going to pass a view i'm going to use the save button view and i'm going to say rdid that empty and i'm gonna say r dot string let empty field like this and then i'm gonna say snack bar length is going to be long and show so essentially this empty field we've created or came with our startup project which if you click it's going to say empty task which means we need to add something okay all right there we have it just a little defensive programming there now the other thing will also be nice to do is to dismiss right so if i click and add a new item here say hello world again and click there i can go get a nice different date and what i want to be able to do is whenever we say save we want to dismiss our bottom sheet fragment okay that'd be nice because then we will be taken to our main list here okay to do that because we're inside of course in our bottom sheet fragment we have a way of dismiss the fragment or in this case the bottom sheet completely so we can come down here inside of a save button right instead of our if statement all is good outside right about here after we have our else what i can do i can say if this being our bottom sheet fragment is visible right if it's visible at this point we've clicked enter we've clicked save we're going to say this that's a dismiss to run again let's give it a try newest item running out of things to add click there we go i can scroll up and i want this to be somewhere later and save you see now it's dismissed went down and you can see now we have a newest item added very well so it's working perfectly can go ahead and delete some of these old items if i wish to do so so we have a few items left there okay very good so very simple things but as you see these simple things usually make our user interface as well as the user experience even better so those little things are very important in the perspective of the user experience okay now let's look at the priority button which is hidden so any trick here if you are on mac say command e or control e for windows this will pop up so this is the most recent files that we've opened so it's very handy helps us not to have to click everywhere in our editor so i want to go to the bottom sheet there so i can show you exactly what we're going to be working on next and sometimes it takes a little while to initialize our views all right so let's close this down as you may remember that we have a lot of hidden things here one of which is of course the radio group priority so this is the one that will have all of the radio buttons let me go ahead and click here and change the visibility instead of gone let's make this visible so you can see there it is you see this is our radio group priority that we need to set up so the idea again is as we add this information right we add this item or this to do we have our date set up okay and we're able to save everything so far the priority we have hard coded it and we've saved the priorities hard coding yet which is not the best approach obviously we want to make sure that users have the possibility the capability of picking the priority so that's we're going to be working on now so the idea is when we click this button here this is the priority button depending on the situation depending whether this is already visible or not this will show so that they can pick and then we're going to save that priority and when we save this to do item that's what we're going to be doing all right before i forget let's put this back to gone like that very good so let's go to our code i'm going to get rid of this back to our code so this is inside of our bottom sheet fragment obviously because this is where we're setting everything up uh right below our calendar view i'm going to go ahead and add say priority button like such a set on click listener i'm going to pass new on click listener and we're gonna make this lambda to simplify our lives now you notice also there's a lot of view 13 view one the reason why because we're using lambda and we're inside of our class here there's a lot of views that we're passing in so this is an attempt from our editor to identify or to make unique ids okay so don't worry too much that's totally fine all right what is it that we want to happen well the first thing we actually want to happen here is we want to use utils and hide the soft key this will hide the soft keyboard key that we will have to deal with it okay so i'm going to pass our view 13 there very good now the main thing we want to do here is we want to set the visibility to our priority radio group right so command e again sheet should have left it open but oh well there we go so we want to make sure that this radio group here is on or off so visible or invisible in this case we want to make it visible so i'm going to say priority gradient group okay dot set visibility like that now in order for us to set visibility we need to get the current state of this priority ready group whether it's visible or not so i'm going to say priority group again dot get visibility okay let me put all of this perhaps in a new line so we can actually see what's going on so what i'm going to do is i'm going to say if the visibility is in this case gone so view that gun so if that's true then i want to make this visible because i've just clicked this and this has to be visible okay else i want to make this gun that's the logic here so we're toggling it again just like what we did with the date just like what we did with the calendar button the same idea here very good and once we have that done because remember this is a radio group which means it contains a few radio buttons inside so we need to go through and check which ones have been selected and if the first one was selected and then we pull that information that we want does it make sense so in this case here we can say priority ready group we can also set unchecked change listener so there is such event handler that we can pass in this case here so i'm going to say new uncheck event listener like that of course we can use lambda to facilitate our code in our lives it says here radio group and i this i am going to change this to just say checked id okay so that it's more uh readable all right so what's the idea here so we're gonna check to see which button was clicked again going back here we need to check which one was clicked so that we know exactly what is what was the selected priority if is it high medium or low how do we do that well first of all i'm going to say if again first we're going to say priority radio group i'm going to check the visibility get visibility if it's visible right if that's the case then i'm going to get the current checked id does that mean that's visible and i want to figure out okay which one is checked and i'm going to say selected button remember all these variables i've created we've created a long time ago when i said i'm going to create them now so that we can later use them okay remember selected button there so now we're setting them up and once i have that check button id that's when i can actually create or in this case go fetch the actual selected radio button so i'm going to say selected radio button notice it's a radio button type i'm going to say view dot find by instead of saying r dot id i can just say selected button id because that would have the id of the current selected button in this case i'm going to just create the actual selected button or in this case selected radio button i should say it makes sense and then i'm going to say if okay i'm checking in if selected radio button that get id remember this is gonna happen each time that we change remember this is going to run each time we are going back and forth meaning if we're clicking on this one that one and that one depending on which one we clicked we're going to fetch the correct radio button that's what we're doing here that's why we are retrieving the selected button id as things change as users may be clicking the first one second one or third one okay so now we're checking for the ids if the id is equal for instance r dot id that radio high so the high priority was selected then i'm going to set the priority say priority which we haven't created set to priority dot hi like this let's go ahead and create our priority type enum remember that it was an enum type so i'm going to say private priority priority like that and there we go so and you see priority now is priority high make sense because they've clicked the priority high else if right if selected radio button that get id is equal to our dot id that radio medium then priority it's going to be set to priority medium and you can guess else if selected radio button get id is equal to r.id dot radio low then priority is equal to priority low now because we like to do defensive programming i'm going to say else in any other case let's not forget our semicolon there in any other case what i'm going to do priority is going to be equal to priority low okay so if they haven't selected anything for some reason we're just going to make the priority low now because this priority low in any other case it's only going to work if of course our priority group is visible so what i want to do so click outside here also i'm going to add another else statement here that would say in any other case i'm going to make sure that priority is always priority low okay notice this is the main if statement so if the visibility is invisible meaning that they haven't even opened up or clicked the priority button i'm just going to go ahead and set the priority to low that way the users are able to go and still save their item okay very good so now that we have a priority because it's an instance variable of this class so it's visible anywhere i can use it here in our if statement right in our save button because we have that to make sure that we don't have an empty field and also due date is not null and i can continue and say and priority is not equal to null like that okay in this case here we have a full complete cycle which means now instead of passing priority like this because this is hard coded i can just pass priority like that and here too i'm gonna just pass priority and we're set all right let's go ahead and run okay very well let's see if this works i'm gonna create a new one finish app okay click very good it's working perfectly it can go up and i'm going to say the 23rd tomorrow and the moment i go ahead and click there and you notice nothing is working here what's going on it looks like right i made a mistake it does not get window visibility is get visibility ah little differences here so all right let's uh go ahead and rerun this there was a little mistake here that should be get visibility i think we should be good this time okay let's give it another try i'm gonna say finish and let's click there we go i'm gonna say tomorrow very good and when my click there are voila it's showing if i click back it's gonna hide this is very good and now i can say i want this priority to priority to be high and the moment i save voila there we go priority is high and of course we added our new item and i can edit that so click again and i can say finish up isn't going to be not really okay and let's go i'm going to change for next week and priority it's going to be medium save there we go it changed for next week january 30th that's very good and priority also change i think for to medium i'm going to go to inspector because we can't see visually the priorities yet but we'll do that in a bit and we should be able to see that everything is actually working perfectly all right there we go let's open up our database i'm going to say select all from table task table there we go and we should see there we go it changed to medium so everything is good all right congratulations so it is working perfectly with a little bit of coding there but nothing that we can't handle and congratulations for getting to this point so mistakes like that happen so make sure whenever you're invoking methods okay that you get the correct ones there are a lot of methods that look alike so it's easy to make that mistake as you saw me actually do here so there we go no problem we fix it and we're good to go we're really making good progress here there's a few things we need to do one of which is make it so that we have all these items color coded because you see our priorities don't really show visually so we do have all of them in our database you can see priority high high high and medium but there's no way for us to know visually or the users to know usually which one is high and so forth so to make this app even more let's say modern and so forth we're going to make it so that we have some elements of our user interface color coded okay to do that we need to open our recycle view adapter let's get rid of all of these other guys that we're not really interested in so where all that happened let's go to where are we setting everything up which is going to be inside of our own bind view holder that is so here is where we do the work so what are we trying to do here well first of all we're trying to make it so that this chip changes color as well as the actual task right so if this task's priority is high then we're going to change the color to a red right to signal the user that this is indeed high priority and they have to do it and if it's medium we change different color and so forth so how we're going to do this well first of all in order for us to change the colors of these widgets we need to use the color state list which is a class that we can pass a two-dimensional array with those colors and so forth so don't worry too much about the technicalities of this and i'll just show you how to do that okay so first thing we need to do i'm going to say color so i'm going to create here a color state list from android content called this color state and again it's going to be new color state list which will give us the states of different colors and you can see we have two parameters we need to pass the first one is going to be a two-dimensional array and the other one is going to be a mono-dimensional array okay so what are we going to do i'm going to say new and look at that even tells us exactly what we need to pass and then i'm gonna also pass here new int just for one like this all right let's finish like that all right so for our first two dimensional array there we're gonna create a new int inside and this one here is going to have these states and so for the first state here i'm going to say dash android dot r attribute enter like this and i'm going to say state enabled okay and the next let's say new again create another int array here this one is going to have the same thing let's say android r attribute okay this is internal android i'm going to say state also enable i could have said disable but i'm going to just make those enabled as well okay so in this case here i chose to make both of them enabled but i could have changed that so this is the state this is the default state of whatever widget we're going to be using this is going to be the default state for our color because remember every button or every widget we have they have a color state list because if you click for instance look closely here i'm going to make this a little bigger if you click you see there's that change in color there okay go back there so click again there's change of color that you see there so that's what we're talking about so every widget has that internally so what are we trying to do here is to override that color state so now this is where the fun begins all right so now next this is where we actually put the state that we want to override in this case here in our second array parameter i'm going to say color dot light gray what this means really is this is the disabled state so when we use this color state and attach it to a widget the disable state where it's not enabled right it's going to have this light gray okay now the important part here which is the enable part that means now when the item is enabled what is the color we want to pass in this case here this is where we're going to pass a different priority color depending on the task's priority so i'm going to say utils dot priority color i'm going to pass a task like this now we haven't created this yet so i'm going to go ahead and create that and explain what that really means okay so what are we going to be doing here is this priority color when we call we're going to pass the task and within the task we're going to check whether the priority is high and then we're going to create a color so what i'm going to do here is i'm going to create int color because the colors are actually saved as an integer so that's where we need to return and so here i'm going to say if the task that we're passing that get priority is equal to priority hi if that's the case then the color we're going to be returning i'm going to create a color by saying color using the color class dot a r g b okay so that contains alpha and i know the color is already that we want so for this case i want 200 for our alpha so it's going to be very bright right and then for int red the rgb numbers for red part i'm going to say 201 and then green is going to be 21. again these are pretty fine colors so i know them that's why i'm comfortable adding those there like there okay there we go and then else if if task get priority is equal to priority medium that's the case i'm going to say color construct another color here using rgb and say color rgb and the alpha is going to be the same one to make it visible and then red is going to be 155 179 and 0 like that and the last one is just an else we're just going to say color it's equal to color rgb still 200 and it's going to be 51 for red 181 for green and 129 for blue and of course when all is said and done we're going to go ahead and return our color okay so that's what we're doing here we just color coding depending on the priority for each one of these tasks so going back here that's what we're doing there okay so this is going to change depending now this has to be used somewhere okay this color state and we need to pass like this again because as i said every widget must have this color state class or object in order for it to know how to change its color state now what we want to do really so in our case here what are the things that we want to change color depending on the priority well first of all i'm going to say holder i'm going to go ahead and get the today chip my set text color okay see it wants me to pass an integer directly or a color state list of colors in this case i'm going to say utils just want to pass that color that priority color and pass or task like that so depending on the task the priority it's going to go ahead and fetch the correct color very nice i'm going to say holder that today chip and i'm going to set the chip icon tint there we go for the tint we actually need to go to pass the color state list which is exactly what we created i'm going to go ahead and say color state a list like this okay so this is what it's going to go ahead and call this directly all right so and then i'm going to say holder again dot radio button because i want this radio button also to have different color i'm going to say dot set button tint list i'm going to pass the color state like this all right and there we have it so now we're guaranteed that the colors will change for our radio button the text and as well as this background of our chip and the internal items inside especially actually the icon because that's what we did there as well as the text all right let's go ahead and save this and run and see aha and voila just like that it went ahead and called these utils and looked at the priority color right and looked to see if that task has priority high and then it's going to be all red okay very nice let's make this a little bigger so we can actually see the whole thing right and so if the priority is medium of course we have this green color there else it's going to be low we don't have a low but if i add a new here i'm going to click there and add a low one so low priority let's just say next week say low look at this if i save and voila you can see now low priority and and fetch those colors to give the user this visual representation of whether this is high priority medium or low and there you have it okay pretty neat so now the app looks much better it looks cleaner and has all the visuals that we need we can still go ahead and delete and do all sort of things and go from there so you can see that it does take a little bit of work obviously to create something decent but it is also fun and once you get used to this thought process it's not that bad right we've created here pretty solid application of course there's more things we can do there's more cleaning up we can do and so forth but the idea here is that i wanted to illustrate how fun really it is to build an application from scratch and a really good application in my humble opinion i should say that hopefully will give you some insights as to how to go about building a full-fledged application using room well i hope you enjoyed this mini course if you are watching me now wow congratulations and thank you for going through the entire course and i hope i really i really hope you enjoyed this course this mini course and i hope that you found a lot of value and if you want to learn more about android development and i'm talking about from not knowing anything to learning how to build apps such as this i would like to invite you to my full course which has more than 46 hours of content of course you don't have to watch that in one sitting but you can get the course exclusively on udemy and you can see the links below just go scroll there's a description below i put a link there and there is a sale going on right now so you can get the course at a fraction of what usually costs so it's a great opportunity as you see there's in this course i'm building a lot of different apps i'm showing you exactly how to build uh android apps the right way this course is as comprehensive as it comes and it has a lot of content a lot of good stuff so i really would like to invite you to the course and i would love to see you in a course thank you so much don't forget just in the description below there is a link to the course thank you again for your time i'll see you in class
Info
Channel: Build Apps With Paulo
Views: 10,744
Rating: undefined out of 5
Keywords: android, kotlin, room, app development course, android app development tutorial for beginners, android app development java, android app development course, kotlin vs java, android developer story, android developer stories, android studio, android developers, udemy, google io, google play console, android application development tutorial, best practices for app developers
Id: d-vdKSbXT4E
Channel Id: undefined
Length: 213min 31sec (12811 seconds)
Published: Tue Mar 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.