Make a Notes App in Android Studio | Room Database | Full Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to coding with evan and in today's video we're going to make a notepad application like this i've already made that app and if i show you if i open that up as you can see we got all the nodes that we created inside our application it's showing in a recycler view and if we want to add a new node we can simply click on this floating action button right here that will take us to a new activity where we have to input the title and some nodes so let me quickly add them i'll add the title as test title and for the notes i'll write this is a test note okay and we can save that note by clicking on the save icon up here as you can see our note has been saved and then if we want to edit any note like i want to add some items inside our grocery list so for this we have to just click on this item right here that will take us to the item that we created so now let's edit this let's remove x from here and add let's say beans and we can save that by clicking on here and as you can see your grocery list has been updated all right and also we can delete any item if we want so for this we just have to press and hold that particular item so let me delete this one so i'll just long press or press and hold this item and a menu will pop up from there we can click on this delete that will delete this item so let me click on that so item has been deleted okay and also as you can see there is another option for pin or unpin if we click on this a pin will be added to this item that means the item is now pinned and if we want to unpin that we can long press and click on pin and pin again that will remove the pin from here and as you can see our background color of each item is changing every time we are refreshing the layout so i'll also show you that and also we can search through these items like if i type in the search box about java that will show us the item that contains the word java let me test this again and this time we'll search for bread as you can see it's showing the particular item that contains the item bread all right so let's make this application as you can see here i have already opened a new project in my android studio i have named that as note app and i just created this with a simple empty activity all right and for the database we are going to use room database to store the data and to integrate room database we just have to add the dependencies for room database to our gradle so to get the dependencies we have to go to this website this is the official website of developers.android.com so i'll give the link of this website in the description from there you have to visit this and you have to just go down here and copy this dependency for the room database i'll just copy these two and i'll go back to my android studio and from here we have to go to inside our gradle scripts and we have to open the module level gradle file and inside here under these dependencies we have to paste those dependencies that we copied okay so i'll just paste it right here as you can see all right and here for the room version we have to add the current or updated version so we'll also get that from the website here as you can see the room version is 2.4.0 so i'll just copy this 2.4.0 and add that here like this all right and once we have added that we have to click on the sync now that will add the dependencies all right once it's done we can close this grill and we can start working so at first i'll make the model class for our note so for this i'll right click here and create a new package i'll name this as models and inside the models package we will create our model class so right click here create a new java class i will call this as notes all right and here after this class notes i'll make these implements serializable because we have to add the serializable because uh whenever we will try to move or send the nodes data from one activity to another we have to use serializable so here i'll write implements serializable okay and then as we are going to use this nodes as a table in our database for this we just have to add an annotation here so here i'll write at the red entity and here i'll pass the table name as let's say nodes okay remember what you are adding here as the table name because we will need that later in our data access object or our main database all right and now we can start working inside our nodes class so inside here we have to create some objects so at first we need a primary key for our database and our primary key will be the id of this node so i'll take a integer value i'll call this let's say id and i'll initialize it with zero okay and this integer id will be our primary key so for this we have to add a new annotation called primary key and we'll set this primary key to be generated automatically for this i'll add a bracket here and inside i'll write auto generate to true okay so that will automatically generate the id every time we add a new item okay and our second object for the note will be the title so i'll add a string called uh title okay i'll initialize it with an empty string all right and the column name for this title uh we have to set that so for this alright at the red column column info and here in the brackets we have to pass the name of that column that we want to set so i'll set the name as let's say title here all right and similarly we need another object or another item for the nodes so here again i add a string called nodes i'll initialize it with an empty string and here i'll give the annotation column info and i'll set the name of this column to let's say notes okay and after that we need another item to store the uh date of uh adding the note so here i'll take another string that will store the time and date of creating the note so here i'll write this as um let's say date empty string oops and i'll give this a column in for annotation and here for the name of this column i'll set this as date okay and lastly we'll need another value for the pin or unpin state so we'll just take a boolean value here i'll just call this as boolean [Music] pin and here i'll set this to false or i'll initialize this with false and i'll add the column name as let's say pinned okay and now we have added all our items for our note model and now we just need to generate the getter and setter method for all of this so i'll just simply right click here and click on generate and i'll click on getter and setter and i'll select all of this from here and i'll click on ok so here we got all the getter and setted objects for each of the items that we added here all right so our model class is now ready now we can uh create our data access object or the dow class so for creating this class uh let's actually do this inside a separate package so right click here and create a new package i'll call this as database okay and inside this database we'll create our data access object so i'll create a new java class here i'll call this as let's say main data access object okay and that will be an interface okay so click on this interface so here we got the main data access object class all right so here at first as this is our uh data access object we have to add a annotation here for the dao so here i'll write the annotation at the rate dao all right and then we can start working inside our data access object interface so here at first we'll create a method to insert the data inside our database for this i'll create a method the return type will be void and i'll call the method uh insert and here for the parameter of this method we'll pass a note that we want to add so that will be an object of the nodes model that we created right here okay so i'll write notes the object name is notes okay semicolon and as we want to add this item to our database we have to add an annotation as this is an insert operation so here i'll write at the red insert okay and while inserting we'll check for the conflict so i'll write on conflict we'll just replace the value replace okay and if we get any error like this we can simply hit alt enter that will import the class for replace and we'll solve that error all right so that is for our uh insert method and now we we have to create another method to get all the data so for this we'll create another method and here the return type will be a list of the nodes so i'll write a list of let's say nodes and i'll call this method name as get all i'll add the parameter as empty and here the annotation for this uh get all nodes or get all method will be a query and here i'll write at the rate query and here inside the parameter of this query annotation we have to pass the query so here i'll add a quotation and inside here i'll write the query as select star from the table name and the table name we will see inside our notes class as you can see inside this notes class we have defined our table name as nodes so we have to write these table name nodes inside here after the from so i'll write notes and we can order those values so what is happening here is it's selecting all the items this star means all items so we are selecting all items from this nodes table and we can perform another query uh to order those values and in this app i am going to order all this value with the id in a descending order because we want to show our latest added items on the top of the recycler view so that's why i'm doing this query if you don't want to do that you can leave this i'll write order by id and the order would be in a descending order okay so this query will return us all the items from this nodes table ordered by the id with a descending order all right so we got the method for getting all the data and then we need another method to update the items inside our database uh for this i'll create a method void update and here inside the parameter of the update method we have to pass the items that we need to update so here as you can see inside this notes class we have all of these items the id title nodes date pin etc so in this example i am going to pass only the id title and the nodes so here i'll pass integer id and then a string value for the title and again a string value for the uh let's say notes okay and also that will be a query so here i'll write annotation query so here inside the parameter of this query we have to write the query so i'll quickly do that here i'll add a quotation and inside here i'll write the query as update and after that we have to pass the table name so the table name was notes so write notes and inside the table we'll set some values like at first we'll set the um title so let's say title equals and then before passing this title that we are getting from the parameter we have to add a colon so i'll write colon title and then we have to also pass the notes here so the notes inside our class name was notes so here i'll write notes equals a colon and then we'll pass this nodes value that we are getting from this parameter which is also nodes okay and where we'll set this items where we'll match the id with this id value we are getting with the parameter so i'll write where id because the id is spelled like this inside our nodes class so here i'll write id equals colon id that we're getting from this parameter all right so that's the query for updating the values inside our database so here we got the method for creating reading and updating now we need another method to delete a value from the uh from the database so to do that we have to create a new method i will call this as delete okay and inside the parameter of this delete we have to pass a object of this nodes nodes nodes okay and then also we need to add a annotation here and for deleting an item from the database the annotation is just simply delete okay that will take care about the uh delete operation from our database all right so here we got all the methods that we need inside our main data access object so now we can create another abstract class for our room database so for this i'll just go here inside this database package and i'll right click here create a new java class i'll call this as um let's say room db and i'll hit enter so here we got our room db class so here at first we'll make this class as abstract and then um above this class we have to initialize it with the database so we have to add a new annotation here as database so here at first we have to pass the entities so the entity of our database is only these nodes class so here i will pass the entities as on notes dot class all right so we have defined our entities and then we have to add the version name of our um database so i'll write version equals let's say one initially and then let's add the export schema export schema to false all right and then we can work inside our abstract class body so inside the class body we have to create a an instance of our room database so i'll just write a private static room db i'll call this as uh database all right and then we have to create another um string variable for our database name we can also do that here so i'll write a private static string and i'll call this as database underscore name and i'll set the database name as let's say note app okay we'll use this database name later inside our uh inside while creating our database instance so after that we have to create a method to get the instance of the database so i'll just write a new method public synchronized static room db i'll call this method as get instance and here we'll pass the context context context okay and we can create this methods body and inside this methods body at first we'll check if our database instance is null or not so if it is null then we'll create the instance otherwise we'll just simply return the database instance so here i'll check if database equal equal null so if it is null then we have to create the instance so let's create that so i'll write database equals room dot database builder this one room.database builder and here inside the parameter at first we have to pass the context so i'll write context dot uh get application context okay i'll add a comma here and i'll go to the new line and here we'll pass the class for our room database so let uh room db dot class and then we have to pass the name of the database as you can see our name we have defined in a string right here the database name so i'll just write the database name string here okay and then we'll go after this bracket here and i'll go to a new line and i'll write dot [Music] allow main thread queries okay and then we'll add another method that uh will say the fallback to destructive migration and then we'll build that so red dot build okay we can add a semicolon right here and the room db dot plus is showing an error what's the issue here require type class d class room db okay so here we have to make this class extends the room database so let's quickly do that so here after defining the public abstract classroom db we have to make this class extends sorry extends room database this one all right so error is now gone as you can see so here after building this we can go outside of this if condition and here we'll just simply return our database so let's return database okay so that's it for this method all right and let's also create an instance of our data access object or the main dial that we created right here so here i'll write this as a public abstract um sorry main dao i'll call this as main doubt okay so that's it for our room db.java our database is now ready now we can start working in our main activity so before going to our main activity we will update the ui elements so for this i'll go inside the res folder inside the layout and here i'll go inside the activity main.xml and here i'll go into split view and inside here i'll just remove this constraint layout to a new let's say linear layout and i'll set its orientation here to vertical and inside here we have this text view i'll just simply remove this and here i'll add a new recycler view so i'll let recycler view i'll set its width to match parent and height to match parent okay i'll give this recycler v1 id i'll call this let's say recycler underscore home okay and i'll give this a recycler view a margin of 80p overall okay and okay let's not use the linear layout here we're going to use a relative layout okay and here i'll change the background of this layout to be color black here okay and here we got the recycler view and now we have to add a new let's say floating action button uh by clicking on which we will add our notes so here under this recycler view i'll create a new floating action button i'll set its width to wrap content and height to wrap content here let's close this tag and here i'll give this floating action button and id i'll call this as fab add and here i'll set the floating action button to this corner of the screen so for this i have to write here align parent bottom to true that will take the floating action button to the bottom and then we need to align this to the right corner so for this i'll write align parent end to true so it got here and then we can give this a margin so i'll write let's say margin to let's pass 24 db okay so it's now here and then we can add a new um icon or an source image for this floating action button so for this we have to create one inside the drawable so i'll write i'll just right click on this drawable folder create a new vector asset and here i'll click on this clipart and select a icon so let's search for plus add okay this one i'll click on this one and click on ok and i'll change the color of this clipper to let's say color black okay and i'll change its name to ic underscore add i'll keep the size as 24 dp and click on next and finish so here we got our ic underscore add we'll just add this to our floating action button for this here i'll write src equals at the red driver slash ic underscore add and as you can see the plus icon has been added here okay so that's it for the uh floating action button now we have to work on this recycler view but before that we have to create another layout for our recycler view items or the each individual items of the recycler view so for this i'll right click on this layout create a new layout resource file i'll call this as nodes underscore list and hit enter so here we'll go to the split view again and here i'll change the parent layout to a let's say card view and inside the card view i'll change the layout height to our wrap content here and here i'll add some more attributes like i'll set the card background color to color white at first okay and then i'll set a id for this card view so i'll write id um i'll call this as notes underscore uh container container okay and then i'll give this card view a corner radius so let's cut corner radius to 8 db and then i'll add a simple elevation so i write card elevation to 8 dp and also i'll give this card view a simple margin so i'll write layout margin as adb all right now we can start working inside our card view so inside the card view we'll create a simple linear layout here i'll set its width to match parent and the height to wrap content here and i'll set its orientation to be vertical so right orientation to vertical okay now inside this linear layout we have to add a text view uh to show the title a text view to show the data and also an image view to show the pin icon okay so let's quickly do that so to make the ui at first i'll add a new relative layout here i'll set it so to match parent and height to wrap content and inside this relative layout will add a text view to show the title and next to that text view will show the pin icon so here i'll create a new text view i'll set its width as match parent and the height as wrap content here let's close this tag and then i'll give this text view an id here i'll call this as text view underscore title let's set a dummy text here so i'll set the text as let's say title okay and then let's align this text view to the start of this relative layout so let align parent start to true now it's attached to the start of the screen and let's set the text size here because it's very tiny so like uh text size to um let's say 18 sp for the title okay and then i'll set the text color select text color to be color black then i'll change the text style to be bold for the title i'll give this text to a padding also of let's say hdp and we'll make this textview single line so i'll add a single line to true okay and as we were initializing this text view title to be single line so if the text is larger here we want to show a horizontally scrolling animation for this we have to add some more attributes like here i'll set the ellipse size to marquee and here i'll write the marquee repeat limit to marquee forever and then we write scroll horizontally to true okay so that's it for our title text view now after this title text let me align this okay so here is the title text view and after adding the title on this corner of the screen we want to show the pin or unpinned icon so for this i'll go below this text view here and here i'll create a new image view okay this one the image view and here for the width of this image view i'll pass let's say 20 dp and for the height i'll also pass 20 dp all right let's close this tag and here i'll give this image view an id of image view underscore pin and i'll align this image view to the end of the screen right here so i'll write align parent end to true okay and here for the pin icon we're going to use a new asset so here as you can see i have made that asset you can download these type of assets from the internet or i'll also give the download link of this one in the description from there you can download that so i'll just copy this asset here and i'll go to my driver folder of the android studio and i'll paste it right here inside the driver and click on ok ok so here it's name is ic underscore pin so if i add this here for example if i write src uh at the drawable slash ic pin you can see the pin icon right here okay so we won't add that here from the ui we'll add this dynamically on our adapter so i'll just leave it as it is and then under this uh title we need to add the nodes that we want to show so here i'll go after this relative layout let me just minimize this relative layout and below this relative layout will add a new text view to show the nodes so i'll write text view and i'll set the width to match parent and the height to wrap content here close this tag and i'll give this text even id i'll call this as textview underscore notes here let's add a dummy text i'll call this as notes okay let's set the text color for this under text color to be color black and then the text size text size will be for the nodes we'll give it a smaller text size i'll pass 14 sp here and let's change the text style to be normal here not bold and then i'll set a maximum lines for the nodes to be shown so here on the main screen we want to show all the items inside the nodes we'll show a maximum number of 10 lines here so i'll write max lines max lines to 10 so that will show only the first 10 lines of the nodes and then we'll give a padding to this so i'll give the padding as hdp all right so that's it for our text view notes now under this will also try to show the uh date of creating that note so we'll show that also inside a text view so for this we can simply just copy this text view once again i'll copy this and paste it under here and here we have to change some items like the id will be a text view underscore date and let's change the text to date and here uh the text color will be let's pass this as color gray okay we have to add the color gray so to add the color i'll simply go to a website called ui color picker and from here i'll select a color uh let's select this one i'll just click on this to copy go back to android studio and i'll go inside my values folder inside the colors from here i'll create a new color i'll change the color code to the color code that i copied right now and i'll change its color title to gray okay so that's it now we can go to our notes list item again and here i'll pass the text color as color slash gray all right and then uh as our uh date time could be a large text we can show also a horizontal scrolling animation here so for this i'll do the same like we did for the title text view i'll write at first single line true then ellipse size would be marquee then the marquee repeat limit will be marquee forever and then we'll set the uh scroll horizontally to true all right so that's it so that's it for the um custom recyclerview items so now we can close this and we can start working in our main activity okay but before working in our main activity i think we should create the adapter class for our recycler view so to create the adapter we can simply do that here or also let's create a new package for this also that will look more cleaner so i'll write the package name as adapters hit enter and here inside these adapters i'll create a new java class i'll call this as nodes list adapter and hit enter so this is the class for the nodes list adapter and outside of this class we have to create another class for the viewholder of this nodes adapter so here i'll create a new class i'll call this as a nodes view holder okay and i'll make this class extends the default viewholder class of recyclerview so i'll write extends recyclerview.viewholder it's showing an error if we click on this red bulb it will ask to create constructor matching super if we click on that our error will be gone and here we got a constructor for the nodes view holder so before or above this uh constructor we have to create some objects i have some objects of the view items that we added in our custom list this notes list dot xml so from here as you can see our first item was the card view that contains all the items and its id is notes underscore container okay so i just copy this id from here and go into notes list adapter and here the class name is card view for the card view and i'll pass or paste the name of the card view or id of the card view so similarly i'll add all the other objects like here we got the text view for the title so just copy the id and here i'll create a text view object and i'll set the object name to text view title so similarly we have another text view for the text view notes i'll copy the id and paste it here we have another text view for the date so copy this and paste it right here and add semicolon and lastly we had an image view here to show the pin icon so for this i'll just copy this id of this image view and i'll go into our notes list adapter here and here i'll create a object for the image view and i'll paste the image view pin here all right so that's it now we have to initialize all these items inside our constructor so for this let's start with this nodes container and copy this paste it here again equals item view dot find view by id r dot id dot uh notes container and if you get an error like this just click on this r and hit alt enter so our error is now gone and at the end we have to add a semicolon and now let's copy this uh one two three four more times one two three four and we'll just um replace these titles that we copied so copy this paste it here paste it here copy paste paste copy paste paste copy paste and paste okay so our all the view items are now initialized now we can start working inside our notes list adapter class here so at first we have to make this notes list adapter class extends the default adapter class of recyclerview so we have to write extends recycler view dot adapter and here on the angle brackets we have to pass the view holder name for this recycler view so we have created this nodes view holder class so just copy the name here and paste it here okay and that's showing an error because we have to implement the methods so we'll just right click on this red bulb here and click on implement methods and we have to select all these three and click on ok so here we got these methods the oncreateviewholder onbindviewholder and the getitemcount so let's start working so at first we have to create an object for the context so i'll write context i'll call this context and then we have to pass a list item that we want to show so the list will be a list of notes and call this as list and after that we have to create another object for a listener and this is mainly for the on click listener of the recyclerview items so let me quickly create a separate listener for this so i'll just right click on this package create a new java class i'll call this as let's say notes click listener and that will be an interface okay so here we got this interface nodes click listener and inside this nodes click listener we have to create two uh methods so first the method will be on click where we'll just simply pass the note item so notes notes okay and another one will be for the on long click okay so let's go ahead on long click so when the user will long click on the uh item we have to pass the notes object also we have to pass the main view object so as you can see inside this nodes item nodes list.xml file the container of this node items is the card view as we have defined the id for this also the nodes container so we have to pass the object of a card view here so here i'll write a card view i'll call this as card view all right so our notes click listener class is now done we can close that and we have to define a new object of this notes click listener in our notes list adapter here so i'll write nodes click listener i'll call this as listener okay and then we have to create a constructor for this nodes click listener so i'll just right click here click on generate click on constructor and i'll select all of these three okay and i'll click on ok so here we got the constructor this nodes list adapter constructor here all right so that's now done now we can start working inside our oncreateviewholder so we'll just simply cut this null and we'll create a new nodes view holder i'll call the layout inflator layout inflator dot from the context dot inflate r dot layout dot uh the nodes list dot xml that we created so notes list and the same parameter is the parent that we got from here and the third one is the false okay so we have done with our oncreateviewholder now inside the onbindviewholder we have to bind all the data with these views but before that we have to work on this gate item count so here instead of returning 0 will return list dot size okay this is the list that we got from here all right and now inside this onbindviewholder will bind all the data with the view so let's quickly do that at first we'll set the title to the title text view so for this i'll write holder dot text view title dot set text and here we call the list dot get from position this position is we are getting from the parameter here so get position dot get title all right and as we want to give this uh title text view a horizontal scrolling effect for this we have to write holder dot text view title dot set selected to true okay so that's it for the title text view now for the uh text view notes we'll write holder dot text view notes dot set text and here i'll call list dot get from position dot get notes all right and thirdly we have to set the date to the text view date so for this i'll write holder dot text view date dot set text and here i'll write list dot get from position dot get date okay and also we have to add the horizontal scrolling effect for the text view date so i'll do the same folder dot text view date dot set selected to true and then we have another work to do for the pin items so as you can see if i show you inside these models inside the nodes we have another object for the pin status which is a boolean object and so uh let me go into the adapter so here at first we'll check if the boolean value pin is true or false so if it is true then we have to show the pin icon to our item and if it is false then we won't show that pin icon so let's quickly do that for this i'll write a simple if condition so i'll write if uh list dot get from position dot is pinned that means if the item is pinned in that case we have to attach the um image ic underscore pin to our image view so for this i'll write holder dot image view pin dot set image resource and here i'll write r dot drawable dot ic underscore pin all right so that will add the pin icon if the uh pinned value is true and if it is false in that case we'll just simply copy this into here and instead of adding this ic underscore pin we will pass zero okay so that will do the job and now as you can see inside our application inside the list items every item got a different color and it changes every time we reload or refresh the layout okay so let's quickly do some coding for this option also so for this we have to create a separate method to get random colors so here i'll go outside of this onbindviewholder and here i'll create a new method i'll call this as private integer um get random color okay and here inside the method body we have to create a list of integer items for the color codes so i will create a new list of integer i'll call this as a color code and i'll initialize it with a new error list okay so now here what i'm going to do is i'm going to create a list of integer color codes and from there we'll pick a random color code every time and we'll attach this to our list item container okay so let's add some colors so for the colors we have to create some color inside the colors.xml so let me quickly do that ctrl d to copy this let's copy five of them okay and let's change its color to color change its title to color 1 and this one for color 2 this one for color 3 this one for color 4 and this one for color five and you can use any color that you want here you can change the colors from clicking on this icon here you can pick any color you want okay so let me pick this one from here at first for the second one i'll pick let's say uh this one for the third one i'll pick this one for the fourth one i'm gonna pick okay this one and for the fifth i'll pick um let's pick a light purple one okay so these are the colors that you that i want to use so now i'll just go back to the adapter and here i'll add some colors so i'll just call this color code here color code dot add and here i'll pass the color code from the resource r dot color dot color one okay and similarly i'll add the rest for color code add color 2 then color 3 color 4 and color five okay so these are the colors that we got and now we have to pick a random color every time so for this i'll call a object of the random class so i will write random random equals new random here and here we have to create a random number between the size of this color code list so here i'll write a integer value i'll call this as random underscore color equals random this is the object that we created here random dot next end and here insert the parameter i'll pass the color code list dot size select color code dot size all right so that will return us a random number between these five and then we just have to return this color code so i'll write return random color okay so that will return us the random color with this method so now what we're going to do is we're going to add the color to the uh notes container okay so here at first we'll get the random color from this get random color method so here i'll take a integer value i'll call this as color underscore code equals get random color so that will return us a random color code from this to here inside the color code and now we have to set the color code to our node container so for this i write holder dot nodes container dot set card background color and as this notes container is a card view as you can see here we have to use this set card background color if it's in your case if it is any other layout you can simply use set background color okay so here i'm adding state card background color and inside the parameter i'll pass holder dot item view dot get resources dot get color and here at first we have to pass the color code that we got from here inside the inside this integer value so i'll pass here the color code and then for the theme value i'll pass null okay so that will do the job and after that we can manage our on click listener and on long click listener of our uh recycler view items so for this here i'll write holder dot nodes container dot set on click listener on click listener and create a new view dot on click listener here okay so here inside this on click method we will simply call our listener that we created up here this one which is the object of the notes click listener so i'll call the listener dot on click and here i'll pass the note item from the list position select list dot get and insert the get parameter we won't pass directly the position we'll write holder dot get adapter position all right so that's it for the on click listener and now for the on long click listener we'll write holder dot nodes container dot set on long click listener new view dot on long click listener and here inside this on long click method at first we will return the listener dot on long click previously we use the on click and here we are using the on long click and here we will pass at first we have to pass the note item so here i get the note item from the list so list dot get from holder dot get adapter position and the second parameter is the oh no not here okay so the same parameter here is the view or the card view so for the card view will simply pass the holder dot notes container all right and here after this instead of returning false here will return true okay so that's it for our note list adapter now we can close this and we can start working in our main activity so here at first inside our main activity class i'll create some objects for our recycler view and our adapter also so here i'll create a new object for the recycler view i'll call this as resector view and then i'll create another object for the notes notes list adapter that we created so write notes list adapter this is the notes list adapter you created here so notes list adapter i'll call this as a notes list adapter and then let's create a dummy list for storing the notes so here i'll make a list of notes i'll call this as let's say notes i'll initialize this with a new error list here okay and we'll also create a object of the room db so i'll write room db so room db is the database that we created here the class that we created here room db i'll call this as database all right and also inside our activity main uh with the resector view we had another uh view which is uh a floating action button so we'll just add this here floating sorry floating action button i'll call this as let's say fab underscore add okay all right so here now we'll go inside the oncreate method and we'll initialize the views which is the resector view and the floating action button so at first i will call the recycler view equals find view by id r dot id dot the id of the recycler view was recycler underscore home i'll copy this and paste it here okay and similarly for the floating action button the fab add equals find v by id r dot id dot its id was a fab underscore ad okay i'll paste it here all right and then we'll initialize our database object that we created here so to initialize that i'll write database equals room db dot get instance and here we have to pass the context so for the context i'll pass this okay and now we can access all the nodes from this database object so now what i'm going to do is i'll um get all the nodes from the database and i'll store that inside this nodes list that we created up here so i'll write nodes equals database dot main dao we have to call the main dial and from this main dial we can call the method get all as you can see here we got all the methods the get all delete insert update okay so i'll just call gate all here all right and now we can update these nodes to our recycler view so let's do that but for this we will create a method here so i'll write update recycler and here we'll pass the notes all right and as you can see it's showing an error because this method is not created yet so for this i'll just click on this red bulb here and click on create method so we have got our method here and our method should be outside of the oncreate method okay so here inside this method i'm going to um populate our recyclerview so at first i'll write recyclerview.org has fixed size to true and then i'll initialize our note list adapter so i'll call our note list adapter that we created up here this note list adapter will initialize this with a new note list adapter and here for the note list adapters constructor we have to pass a context a list of nodes and a nodes click listener okay so let's do that at first we'll pass the context so for the context i'm going to pass main activity dot this as our activity name is main activity so maintenance dot this and the second parameter is the list so the list we are getting from this parameter which is called nodes so here i'll pass nodes here and the third parameter is the nodes click listeners object so let us quickly create one so here i'll write private final [Music] notes click listener i'll call this as notes click listener equals new notes click listener okay and here we got the on click and on long click method and here it's showing an error because we have to add a semicolon here okay so now we can pass this notes click listener to the parameter of this nodes list adapter so i'll write nose sorry notes click listener this one okay all right so that's it now we can attach this adapter this nodes list adapter to a recycler view so i'll write a recyclerview.adapter and here i'll pass this notes list adapter notes list adapter okay but here we have to do one more thing so here before initializing our adapter let's do one thing we have to um we have to set a layout manager for our executor view so here i'll write recyclerview.org [Music] layout manager and here as you can see inside our app if i show you our layout is something like this it is uh somewhere like a grid layout but the um items each of the items have their different sizes okay so for this we have to use this staggered layout so let's quickly do that so here inside the set layout manager i'll create a new staggered grid layout okay so i'll call this staggered grid layout manager and here in the parameter at first we have to pass this pan count like in my example we have only uh two spans here this is the first span this is the second span so i'll just pass here two if you want to use three span you can pass three or if you want one you can pass one here okay so i am passing the span count two and the second parameter is the orientation so here i'll write linear layout manager dot vertical because i want this to be vertical okay so that's it so that will show the items in our recycler view but we don't have any item here to show because we have to add the items by clicking on this floating action button so let's quickly do this also so here uh what i'll do is after here update recycler i'll call our fab underscore ad so fab underscore add dot set on click listener new view dot on click listener and here on this loading action buttons on click i'll start a new activity where i'll um enter the title and the description and i'll save it and that will be added to our uh database and will show inside our receptor view okay so let's try to do that for this at first we will create a new activity let me minimize all this so i'll just right click here on the package and create a new activity i'll go for the empty activity and here i'll name this as [Music] notes taker activity okay and i'll click on finish here so it's creating the activity let's wait some time okay so now it's done so now if we go back to our main activity and here on the floating action buttons on click event uh i'm going to call the new activity with intent so you create a object of intent i call this intent equals new intent and here i'll start from main activity dot this from [Music] note sticker activity dot class okay add a semicolon here go to the new line and here we will use start activity for result so let's start activity for result and here for the first parameter i'll pass the intent that we created right here so i will pass the intent and the second parameter is the result code so for the result code i'll here directly pass 1 0 1 sorry it's not the result code it's the request code okay i'm passing the request code as 1 0 1 because um we are adding the data that's why i am passing this you can press any value that you want i am passing 1 0 uh what i am planning is for uh adding a note i am going to pass one zero one and for editing a note i will pass one zero two so that's why i have added one zero one here okay so that's it now let's go to our note sticker activity here and from here uh actually let's let's first design the ui okay so i'll go to the split view of this uh activity note sticker dot xml and here i'll change the parent layout to a linear layout all right and i'll set its orientation to be vertical and i'll change the background color of this to color black okay and inside this linear layout we'll add a custom toolbar but before that we have to remove our default toolbar or the default action bar from this note sticker activity so for this we have to go to our theme section here inside the rest folder inside the fellows inside the themes we have to go to this themes.xml from here and here what i'm going to do is i'll just copy this style once again i'll paste it under this here okay and i'll change its name to from note app to theme dot note app to theme dot node theme dot no tab uh no action bar okay and i'll set the action bar here from dark action bar to no action bar okay and similarly i'll do it for the themes xml knight also i'll copy this i'll paste it under here and i'll call this as no tab dot no sorry no action bar oops no action bar and i'll change the direction bar to no action bar all right and then okay i made a mistake here there will be no tab dot no action bar okay so that's it now we can close all this themes.xml and we will go to our manifest file androidmanifest.xml and inside here as you can see for the activity and notes taker activity after this i'll write theme and here i'll write at the rate style slash theme dot no tab dot no action bar okay that's it now we can go back to our activity notes taker dot xml and here inside this linear layout i'll create a toolbar right toolbar and i'll set its width to match parent and height to question mark attr slash action bar size so that will take the default height of a action bar and let me minimize this okay now i'll give an id for this toolbar i'll write as toolbar underscore notes okay and here let's do one thing let's give this a title of the app name and the title text color will be color white okay here we got the app name all right and then insert this uh notes okay we won't need that actually let's remove this okay now inside this toolbar we have to add some attributes but we shouldn't close like this i'll close it in a regular way and then inside this stool uh inside this toolbar tag i'll create a new image view here i'll set its width to question mark 80 tr slash action bar size and for the height i'll also pass the same i'll copy this and paste it here okay you can close the tag here and for the image view i'll give this an id i'll call this as image view underscore save okay and here for the background of this image view i'll pass transparent color so i'll write android color slash transparent okay and we have to align this to the end of the parent so for this let's put this image view inside the relative layout so inside the toolbar i'll create a new relative layout i'll set its width to match parent and height to let's say wrap content okay and okay don't close like this will close like this okay so inside this will inside this relative layout i'll just copy this and paste it here so here we got our image view so we need to align this to the end of the uh toolbar so for this here i'll write align parent end to true okay and here we need to show a image or a save image so let's quickly create that so i'll right click on this drawable folder and create a new vector asset and i'll search for let's say save yes we can select this one this one okay i see underscore save i'll set its color to white this time and i'll click on next and finish so here we got our ic underscore save dot xml file and now here inside this image view i'll pass the source image from android column src and here i'll pass drawable slash ic underscore save okay so here we got this save icon here okay we can give this a small padding uh padding of let's say 4dp let's pass hdp okay looks better so our image view is now complete for the save so now we can work on the edit text to enter the title and the notes so for this i'll just minimize this whole toolbar here and we can work under this toolbar so here i'll create a new edit text i'll set its width to match parent and height to let's say wrap content here all right and close this tag and here for the edit text i'll pass its id as edit text underscore title and here i'll set a hint of let's say title okay and i'll change the uh hint text color so i'll write text color hint and i'll pass the color let's say white okay and then i'll give this a padding of hdp i'll set the text style for this edit text to be bold as this is the title and will also set the text size to be let's say 20 sp here okay i'll also pass the text color for this to be color white as our background is black so we have to use the text color white and also for the title i'll make this in a single line so i'll write a single line to be true all right so that's it for the title edit text now we'll do this for the nodes edit text so i'll write edit text i'll set its width to match parent and height to let's say wrap content here okay and i'll add a new id for this edit text so i'll call this as edit text underscore notes and here i'll give this a hint of let's write add notes okay and i'll also add a text color hint to color white here i'll give this a padding of 8 db here and it'll also change the text color to be color white also and i'll set the text size here to be 18 sp and let's set the text style to be normal here okay so that's it for this edit text so actually that's it for the whole layout file for the activity notes taker now we can go to our notes taker activity.java file and here we'll start working at first we'll initialize our views so as you can see at first we have we have here a a new edit text so i'll just copy the id of this edit text i'll go into our notes taker activity and here i'll create a new edit text object i'll call this edit text under edit text underscore title and for the second one i'll copy this again paste it here okay and thirdly we have this image view inside the toolbar okay so i'll just copy this image views id and i'll go to the note sticker activity and here i will create a new object for image view and i'll set its name as image view underscore save and here also we'll need a object for the notes so i will create a sorry create a notes object i'll call this as notes okay so now here at first the user will input something in the uh edit texts right here and whenever they will click on this save image our note should be saved into our database so for this what i'm going to do is instead of not sticker activity here i'll check a on click listener for this image view save so i'll write image view save equals find vy id at first r dot id dot image view save okay and after uh initializing this with the find view id we can add the on click listener so let image view save dot set on click listener new view dot on click listener here so now inside this on click method we will get the title and the description from the edit text and we'll send it to the main activity to add to our database so at first let's take a string i'll call this as title equals which is okay but before that we have to initialize this edit text also otherwise we will get errors so here as you can see we have the edittext title and the edittext notes so i'll just copy this edittext title here under and already text title equals find view by id r dot id dot edit text title here and similarly for these edit text notes i'll write edit text notes equals find vy id r dot id dot edit text notes okay we have initialized all of this and now here for the string title i'll write uh string title equals edit text title dot get text dot tostring okay and i'll take another string i'll call this as a description equals edit text notes dot get text dot uh tostring okay so we got the title and description from the edit text right here and here at first we'll check if our title or accurate title could be empty let's check for the description only so i'll write if the description dot is empty in that case we'll just show a toast message where for the context i'll pass the notes taker activity dot this and for the message i'll pass please add some notes okay and after this toast message i'll just simply return from this if it if the description is empty and if it is not empty in that case we'll [Music] add the data so here for the nodes object we got the title and the description we need the date of creating this note also so for this we can create the date so to create the date i'll call the simple date format i'll call this formatter equals new simple date format and here inside a string quotation we have to pass the pattern uh by which we want to get the date okay i'll give a link of an article where you can find various types of patterns for various type of date and time outputs so in this example i'm going to use a simple pattern so here at first we'll take the weekday and then we will get the uh date and after the date we will get the month name just three letters and then we'll get the year the full four digit year and after that we will get the uh hour so for getting the hour we'll uh we'll just write uh 2h for the hours and then a colon for the minutes i'll let m m and again another colon and now we can ok let's not take the seconds we'll just get the am or pm state okay i'll write a okay so here the uh three e stands for the weekday the d is for the date m is for the month y is for the year h is for the hour m is for the minute and this a is for the am or pm state okay so i'll just close this with a semicolon here and then we'll take a date object so i'll write date date equals new date okay so now we got the title the description and the date also now we can populate our object that we created up here this nodes but as you can see we haven't initialized our nodes object so to initialize that we can go under this and here i'll write um notes equals new nodes okay and then we can set the attributes so at first i will set the nodes dot set title and for the title i'll pass this title here so i'll write title and then notes dot set notes and for the notes i'll pass the description here so i'll write description and then notes dot set date and for the date i'll pass this on the this date object so date okay uh we have to pass actually we can't directly pass the date here uh we have to call the formatter first here so formatter dot format so this formatter we called here dot format and here we have to pass a date object which is this one so write date okay so now that we have uh created a object of the notes we can now send this data back to our main activity so for this we will create a new intent alright intent intent equals new in intent and here i'll make this intent intent dot put extra and here with the extra i'll name this as note and the value of this extra value will be this uh notes object so i'll write notes and remember one thing uh whenever you are passing a java class with this intend.put extra you have to make this class uh serializable like inside this note class as you can see i have made this implement serializable if you haven't added that you will get an error inside here inside this line okay so make sure you do this you should make sure you implement the serializable inside the model class so now um after putting extra to our intent we can set back the data so for this i'll write set result and here i'll pass the result okay the result code which is so activity dot uh result okay this one okay and then we'll pass the intent that he created so we'll pass the intent here okay finally after passing the data we can finish this note sticker activity so for this i'll write just finish okay so that will do this so now what happening here is whenever the user is clicking on this floating action button on the main activity it's sending the user to the note sticker activity and here the user is typing some data inside this edit text and when they click on this save button it creates a notes object with the details that the user typed and then it sends back to the main activity so now what we need to do is we need to uh receive the data inside our main activity so for this we'll just go here okay let's go here and here we'll create or override a new method which is on activity result okay so you have to override this method uh on activityresult all right so here at first we'll go after this supercall and here we'll check for the request code so i'll write if uh request code so we're getting this request code from this parameter and we'll match this with the request code that we sent with the floating action buttons on click which is one zero one so we'll search for if it if it matches with one zero one then it's uh for the uh save notes okay so if it is one zero one then we'll again check for if the result code equals equals uh activity dot result okay so if the result code is resolved okay then we can store the data to our database so for this we will create another object the notes object i'll call this new underscore nodes equals data dot get serializable extra and here we'll pass the name that was sent from the notes taker activity as you can see the extras name was not not so we'll just uh make the same spelling here nrt okay and put a semicolon here and it's showing an error because we need to cast the value to the model nodes so click on that and this line will be added and your error is now gone so now we can add these new nodes to our database so for this i'll write database dot main dao dot insert and here for the parameter of insert we have to pass the object of the nodes which is this one the new notes so i'll just pass here the new underscore notes okay and after that we can clear the notes list that we created above as you can see this one this notes which is a list of notes so i'll just write notes dot clear and then again i will call notes dot add all and here i'll pass the database dot main.get all okay so that will return all the new nodes and that will add it to the nodes list and after that we will call the notify dataset changed for our nodes list adapter so i'll write notes list adapter dot notify data set changed okay this one all right so now let's test our application to see if we are able to add some notes or not okay so for this i'll just run this here is my phone it's installing all right so as you can see our app is now installed and the main screen is now empty because we don't have any notes here so if i click on this uh floating action button right here it took me to the new activity where i need to uh write the title and the notes so let's write something for the title i'll write let's say test title and for the notes i'll write this is the first note okay and then we can click on this save icon to see if it saves or not so let's click on that it got crashed okay so insert the run let's see why it got crashed so it says notes list adapter 58 exception resource id get color okay okay 58 what's inside the 58 so we got an error inside this cat color so the color we are getting here this color code is from get random color so if we go to the get random color random colors random color i don't know okay so here we made a silly mistake this random color is just a simple number from a simple integer number that's not a color code so we can't actually uh set this to our card background so for this here instead of returning random color we have to write this color code color code dot get and here for the position we have to pass this random color integer number so let's random color here okay so that was a silly mistake but okay happens so let's run this app again okay so as you can see now our data has been added so let's try to add a new data so for the title i'll pass test sorry it got minimized test two and for the nodes i'll write this is my note okay and let's click on this save button here and our data is now saved okay so our note insertion and note reading is done now we can try to perform the delete operation and search operation from here and also we'll work on using on adding pin and unpinning the items so let's also do that okay so i'll minimize this close this and i'll go to our main activity from here let's close all the other ones so at first let's try to add the option to edit any node that we want so for this we have to maintain the on click listener of the items so as you can see inside our uh adapter we have managed the holder dot nodes container dot on click listener that's passed inside this listeners on click method so we got this listener inside our main activity here this nodes click listener so we have to work on this on click listener right here okay so here on the on click method i'll create a new intent here i'll call this intent equals new intent from main activity dot this to the note sticker activity dot class okay and we'll add some extra values like the selected item so for this i'll write intent dot put extra and here will [Music] whenever the user will click on any item will pass this particular item to the new activity so that the user can edit that item okay so for this we have to pass that item so how we can do that we have to pass the name of that data so i'll call this as old note old underscore node and for the data the selected data is being passed with this on clicks parameter these nodes so i'll just simply pass this nodes okay that's it and then we can also do the start activity for result here so let's start activity for result and here i'll pass the intent and this time the request code will be 102 because if you see above when we were adding a new data our request code was 101 and this time we are editing a data which is previously added so that's why we are passing a different request code okay i'm passing 102 here all right so we have to now maintain this editing on our um note sticker activity so here as we are passing a data with the intended put extra we have to grab that inside our note stacker activity so i'll do that inside the oncreate method so here after this views initialization i'll go here and here i'll do one thing i'll write get intent dot get get serializable extra and here for the string name it was old underscore note so i'll just copy this and paste it here so that there is no mistake and let's do one thing up here we got these notes and we previously initialized that on the save button click okay but in that case we have to initialize it before so here i'll initialize it for now we have to maintain something uh just keep watching so here i'll write notes equal new notes okay and here i will pass the notes equals get intended get serializable extra old note add a semicolon here and it's still showing error maybe it's for the casting issue yes so click on this cast to no tap models notes so it's been casted and now the error is gone so so what's happening here is whenever our node sticker activity is launched it will search for a get serializable extra for the name old note so it will find the old note if we are launching the note sticker activity for the for editing a previously created note but when we launch it for adding a new note this won't work and it will crash so to avoid that what i'm going to do is i'll add a try block here and insert this try block i'll just [Music] paste it this notes get intent dot serializable extra so that don't give us any error so here i'll also add a cache block with exception e and i'll write e dot print stack trace okay so now if we go inside this uh try block as you can see we got the nodes here so we can also set the text of the nodes to our edit text so let's do that so i'll write edit text title dot set text and here we'll pass the title text from this notes object so i'll write notes dot get title okay and similarly i'll do it for the notes so write edit text notes dot uh set text and here i'll pass notes dot uh set sorry not start get notes okay so that will set the uh details of these notes from this all right and here we need to do one more thing we are initializing these nodes twice once we are initializing this here and then we are doing this here also so to avoid that scenario we we can create a new boolean object so i'll write boolean is all not equals false okay so what is happening here is at first we are defining this boolean is all node to false but when we will get these nodes from the intent that means this is a old node in that case we'll make this is all not value true here so i'll write is all not equal true all right so now we'll go down here where we defined or initialized our nodes again so here before that we will check if is old note is false add a false sign here before and if ease or not is false that means it's a new note in that case we'll initialize it here okay so that's a simple uh simple logic all right so okay our task for the notes takeractivity.java is now done let's go to mainactivity.java here and here we have to capture the data also so for this again i'll go inside this on activity result and this time we'll check for the request code 102 right okay so let's do one thing i'll take elsif request code equally call 102 and if result code equal equal activity dot result okay and here similarly we will create a note object notes new underscore notes equals um data dot get serializable extra and here the data name was as you can see the data name was note so i'll copy this and paste it here okay showing an error because you need to cast this okay it's been casted okay and now we have to update uh this particular node so for this i'll write database database dot main.update and here for the parameter of update we have to pass the id the title and the nodes okay so for the id what i'm going to do is i'll get this new nodes notes new notes dot get id and then for the title again new notes dot get title and for the notes new notes dot get notes okay that's it and now similarly i'll clear the notes so notes dot clear and then notes dot add all and here i'll pass the database sorry database dot maindo dot get all okay and again i'll call our notes list adapter dot notify data set changed okay so that will do the job for editing a note so now let's run our application and see how it goes okay so our app is launched and let's add another note here to see so i'll write test 3 i'll write something as my name is yvonne okay and let's save it so our note is now saved okay this one test three my name is evan okay and now let's try to edit this so to edit that we have to click on this so i have click on this and as you can see we got the title and then uh notes that we typed earlier on this node if we go to another one let's go to this one we got the details okay so now let's try to update this one and this test three my name is evan and here i'll update this with and i am a programmer okay now let's try to uh now let's try to save this so click on this save icon as you can see our data has been updated my name is evan and i am a programmer nice so our update option is also working so now let's work on um the search view okay so for this uh at first we have to go to our main activities xml file activity underscore main.xml and here at first above this recycler view we'll add a search view so i'll add a search view this android x search view i'll set its width to match parent and height to the same size of action bar so question mark attr slash action bar size all right and as it is a relative layout we have to adjust the alignments so before that i'll give its id so i'll call this as search view underscore home then i'll align its to align this to align parent top to true okay [Music] that's it and for the recycler view i'll set this to below the search view so copy the search views id and paste it here okay so it's now aligned under the search view okay let's do one thing let's give this search view a margin of let's say 16 dp and let's give its background to color gray all right it looks good now and let's give this a query hint to search notes dot dot but the query is not showing here because we have to edit another attribute which is it was iconified by default we have to make this false okay so here we got the query search notes all right so our ui part is now done now let's copy this id of the search view search view underscore home i'll copy this and go to our main activity.java here and here above we will initialize our search view so i'll write search view i'll paste the id here and i'll initialize this under here so let's search view underscore home equals find view by id r dot id dot search view underscore home all right so now our search view has been initialized so we can now uh write some method here to perform the queries so here i'll write our search view underscore home dot set on query text listener i'll create a new view new uh okay search view dot on query text listener all right so here we got uh two methods the on query text submit and one query text change so we want to work on the onquery text submit will try to match the data every time the user types a new single word new single alphabet inside the search view okay so let's quickly do that so for this at first we will go inside this on query text change and here i'll call a new method filter where i'll pass this new text that is typed on the search view so i'll pass the new text here new text all right and then we have to return true instead of returning false make sure you change that to true and the fielder is showing an error because we haven't created that method so i'll just click on this red bulb and click on this create method filter and we'll click on this main activity here so it will be created separately here so now we have to work on this method filter we have to link this to our uh notes notes list adapter okay so inside here at first i'll create a new list so i'll create a list of notes i'll call this the filtered list equals new arraylist here and then we'll check for all the nodes inside the list nodes or inside the list that we created previously this one this globally assigned nodes so we'll check for [Music] sorry notes i'll call this as single note in notes so i'll check for all the single nodes inside the globally assigned nodes and i'll match the uh all the description of each of the nodes with the string typed inside the search view so for this i'll write if um single note dot get title dot to lower case i'm using two lowercase to avoid any case sensitive issues so two lowercase dot contains and here inside the parameter of contents will pass the new text trust the new text and i'll also add a dot to lowercase to here to lowercase this one okay we want to use this local root only the two lowercase okay so single node get title uh two lowercase or contents okay fine and also i'll go to the new line and we'll add a or operator here and here i'll check again for a single node dot this time we'll check for the nodes so get notes dot uh to lower case dot contains i'll pass the new text dot to lower case okay so if it is then what we'll do so now if the new text that the user typed on the search view matches with any one of the title or the words inside the notes then will add that single node to the filtered list so i'll just do a filtered list dot add and i'll pass the single node here this one the single note okay so after a full iteration we'll get all the notes that contains the string that is typed inside the search view and those notes are stored inside this filtered list so now we can pass this filtered list to our uh resetter views adapter to show that inside our recycler view so for this we have to create a new method inside our note list adapter here so if i zoom in and here we'll do one thing i'll just go after any of the method like let's go after this get item count here and here i'll create a new method i'll call this as a public void filter list and here for the parameter i'll take a new filtered list so i'll write list of nodes and i'll name it as uh filtered sorry filtered list okay we have to define the method body here so i have created a new method called filtered list where for the parameter i'm taking a list of nodes and here what i'm going to do is i'll just replace the globally assigned list that we defined here with the list that we are getting from the parameter so i'll write list equals filtered list okay so that will now swipe the filtered list with the global list and then we'll call the notify dataset changed okay so now let's go back to our main activity here and here after the for loop we'll just call the sorry notes list adapter dot filter list and here we'll pass this filtered list that we got so filtered list all right so i guess that should work now now let's try to run our application again and see the changes so our app has been installed and here we got our search view so let's try to search something so here in all of these three items inside this one we have my name so let's try to write my name e v a and as you can see it's showing only this one so let's try another one let's try with uh this okay so only these two contains this okay so already h i s all right so as you can see our search option is also working fine that's nice so now let's do one thing let's work on pinning unpinning and deleting any item okay that's the only method left so i'll do that so for this we have to work on the on long click method right here okay so let's do that so here for performing this we need to create another uh note object so let's globally design that i'll go here and create a new single notes object i'll call this as selected note all right and then we have to go inside this on long click method of this nodes click listener and here i'll initialize the selected node equals new nodes okay and then i'll uh pass this nodes that we got from the parameter to the selected node globally so the selected node equal nodes that we got from the parameter okay and then we can um show a message or show a pop-up dialogue so i'll write show pop-up show pop-up okay and here for the parameter i'll just pass the view where that we want to show the message okay so here i'm going to pass this card view so in your card view okay and it's showing an error because we haven't created this method so i'll just click on this red bulb and click on create method show pop-up click on this main activity here we got this method show pop-up and here we will create our pop-up menu okay so let's create some menu items so for this if you have any menu item in your res folder you can okay with that but if you don't have any menu option like me then you have to create one okay for this you have to right click on the rest folder create a new android resource directory and from here the resource type i'll change this to menu and click on ok and here we got the menu folder so right click on the menu folder create a new menu resource file and the file name i'll make this as pop-up underscore menu okay and we'll click on ok go to this split view and here inside the menu i'll create a new single item i will pass its id as let's say pin i'll give this also a title i will name this as pin slash unpin okay and similarly i'll create another one i'll just copy this and paste it here and this time i'll change its id to delete and also the text i'll change this to delete okay so these are the two menus that we want to create so our pop-up menu xml is now done we can close this and now insert this main activity inside this show pop-up method that we created i'll create a new object of pop-up menu pop-up menu i'll call this as a pop-up menu equals new pop-up menu and here in the parameter we have to pass the context and then the view that we where we want to show the menu so i'll pass this card view here so after this i'll pass the card view all right and then i'll write the pop-up menu this one the pop-up menu uh dot set on menu item click listener and here let's pass this it will show an error because we haven't uh implemented the method we'll do that later after some time and here i'll write a pop-up menu dot inflate let's inflate the menu that we created right now the power panel xml so here i'll write pop-up menu dot inflate and here i'll pass the menu id r dot menu dot pop-up menu that we created okay and finally we can show the menu by writing pop-up menu dot show okay so now it's showing an error that's because we have to um we have to implement the on click listener for the on menu item click listener okay so i just go above here so here public class main activity extends app compat activity here i'll make this implements popup menu dot on menu item click listener okay hit enter implements popup menu dot on menu item click listener that will show an error because we have to implement the methods so click on implement method click on the first one click on ok so here we got the on menu item click method and as you can see your error is now gone inside this line so here we will mainly work with the item ids so as we got the menu item with the parameter we will go inside this method and i'll write a new switch case so i'll write switch for item dot get item id and inside the body at first i'll check for the menu id r dot id dot pin okay as you can see inside the pop-up menu we had this id pin and delete so at first i'm checking for the id pin so if the uh item id that clicked is pin in that case we'll check one thing that if the selected node dot is pinned so if it is already pinned then we just need to unpin this and if it is not pinned then we have to just pin this okay so it's very simple so we'll just go to the if conditions body and if it is pinned here then for this we have to perform a query and we don't have that query inside our data access object so we have to just add that so i'll go inside the database inside our main dial and here we need to add the query so here after this delete i'll create another method which is void pin and here the parameter of this pin will be the int id and a boolean value which is pin okay and it's also should be a query so i'll add a annotation of query so here we need to write the query so i'll just quickly add that inside the quotation i'll write update the table name is notes and here we need to set the value called pinned and i'll save this to the boolean value that's passed here inside the parameter so i have to write colon pin okay and where we need to change this we will match this with the id so i'll write where id equals colon id that passed here all right so that's the query so now we can go to our uh main activity and inside here for the selected node dot is pinned i'll call the uh sorry the database dot main dow dot pin and here we need to pass the id so for this i'll write selected node dot get id and the second parameter is as in this condition it is pinned so we have to pass false okay and i'll show a toast message here which will be for the main activity dot this and the message will be [Applause] unpinned okay and similarly i'll write a else condition where i'll call database dot maindo dot pin selected item dot get id and here for the second parameter i'll pass true and then i'll show a text message and the main activity dot this sorry this and the message will be pinned okay so now we have to go outside of this e fields block and here we need to clear the nodes so red nodes dot clear and then i'll write notes dot add all and i will call the database dot main dow dot get all all right and finally we will call the notify data set change of our nodes list adapter so write notes list adapter dot notify data set changed okay and finally here we'll return true return true don't forget to add this return true otherwise you will get errors okay so now before working on our delete option let's try this spinner unpin method so i'll just run this application from here as you can see your app is now installed and if i try to pin any of the items let's click on this one the test tool let's click and hold on this so the menu has been popped up pin run pin delete so let's click on the spinner unpin and as you can see a pin icon is added to this item let's try to pin this one also it's also pinned and if we want to unpin anything like let's try to unpin this one click on pin and pin again the pin is now removed okay so our p9 pin option is also working fine now let's try to work on the delete option so for this i'll go outside of this case so i'll write another case which is r dot id dot delete okay so inside this case we will write our coding for deleting an item from the database that's actually quite simple for this we have to just write database dot main dow dot uh delete and here for the parameter we just have to pass the selected note so i'll write selected note that's it and then we can also remove the node from the list so i'll write notes dot remove and here we'll pass the selected node all right and then we call the notify data set changed of our adapter so i'll write uh notes list adapter dot notify data set changed okay and also we'll show a toast message on the main activity dot this and the message will be item sorry let's write note deleted okay we just have to add the return true here also so i'll write return true okay so here outside of this uh switch case we have this return false we won't return this return false here we'll just remove this from here and we'll create a new default case here default and here will return false all right okay so now let's run our application for the final time to see the changes of the delete option so i'll run this as you can see your app is now installed these are the three menus so let's try to delete this one so long click on this and click on delete as you can see the item is now deleted let's unpin this one it's got unpinned let's delete this one it got deleted okay so that's it for the video i hope you guys enjoyed it and if you want me to add more options and features to this notepad app you can comment down below i'll definitely try to add them and create a new video on that and thank you so much for watching
Info
Channel: Coding With Evan
Views: 83,017
Rating: undefined out of 5
Keywords: Make Note Taking Android App From Scratch | Easy Android Studio Projects | Android Studio Tutorials, Notes Application Android Studio Tutorial | 2021, How to Create Notes App/ Diary App in Android Studio, Notes App in Android Studio Using Firebase, Android App Development | Notes App | Android Studio, How to make #Notes app #Android studio Room #MVVM Live data in (Hindi), Build a Note App with Android Studio, Java and Permanent Storage, how to create Notes app in Android studio
Id: Shh0N45S4hE
Channel Id: undefined
Length: 132min 39sec (7959 seconds)
Published: Sun Dec 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.