Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
in this video we are going to create a
recyclerview to show our books inside
our own books activity but before that I
am going to create a model for my book
so in my package I can say new Java
class let's name it both for this book
item I'm going to have an ID first of
all let's say private int ID this ID is
going to be unique for every book and
later on we will identify our books by
this ID after that we need a name
private string name also we need the
author private a string author I'm also
going to define an integer for the
number of pages so let's say private in
pages a string maybe for the image URL
also I'm going to define two more fields
one is called short description and the
other one is called long description
we are going to show different versions
of this description in different places
okay let's quickly create a constructor
I'm using alt + insert let's select all
of our fields and also down below this
constructor let's quickly create getters
and setters I'm also going to create a
two string method I believe it will be
useful Alt + insert just to drink let's
select all of the fields okay this is
sometimes we named these kind of classes
as poles pojo stands for plain old Java
object in which the class has some
constructor and some getters and setters
method ok let's move on and let's
implement the recyclerview dependency
into our project so if I open the layout
file let's go to the layout folder
inside this activity all books that
examine if we switch to design view you
can see that we have this recycler view
option in here and we can drag that in
our layout let's switch to a split view
and let's change the beads on high to
match parent so inside this activity we
are only going to have a recycle of view
let's also give it an ID and let's
initialize it in our Java file let's say
let's open all boats activity dot Java
class and let's initialize our recycler
view private recycler view let's name it
brokes recycler view and down in here
inside on create method let's a box
recycler view is equal to find view by
ID r dot ID dot box recycler view after
that it's time to create the adapter for
this recyclerview let's quickly do that
so in my package i can right click let's
say new Java class I'm going to name
this class pop recycler view adapter
first of all let's create our view
holder class let's say public class view
holder I'm going to name it let's say
extent recycler view dot view holder
this class after that we need a
constructor we can do that by pressing
Alt + insert and selecting constructor
before going further we need to create a
layout file for every item in our
psychology certain our layout folder
let's say new layout resource file I'm
going to name this list item book let's
minimize this project pane and let's
switch to a split view first of all I'm
going to change this constraint layer to
a card view so this fold layout file is
going to be a card view and in here I'm
going to use the material card view I
think the style is a little bit
different also let's change the beats
and height to wrap content let's also
give an ID to discard view later on you
are going to need that let's name it
parent inside our card view first of all
I'm going to create a relative layout
let's say match parent and mesh pattern
for the vitsin height and inside this
relative layout let's have some items
for example I'm going to define an image
view for the beats I believe 130 would
be fine 130 DP also for the height I am
going to say 200 TV that seems a little
bit large let's decrease that to 150
let's be with an ID I'm going to name it
IMG book let's also have a placeholder
let's say source and let's pass our I
see launcher icon after this image view
let's quickly define a text view for the
name of our book we're going to say wrap
content and wrap content for the text
style of this text by same text style
I'm going to pass bold in here beside
that let's increase the size text size I
let's move EQ below our image view let's
say layout below our ID IMG book beside
horizontally so that's a center
horizontal true I think it's better to
that's a margin top 5 DP or 7 DP maybe
that's almost it for our layout file in
here I'm just going to add two more
first of all let's say cart corner
please mount cart corner radius
let's pass 7 DP beside that let's add a
cart elevation once again I'm going to
say 7 DP in here you can see that we are
only showing the image view and also the
book name but if you remember when we
have created our book model we had more
information we are going to show those
information in two different places in
our application first of all by clicking
on every book we are going to navigate
the user to another activity and beside
that we are going to show lesser
information when the user clicks on some
arrow button on our cart view to be
precise later on we are going to create
an expandable card view which means that
for the first time that the user sees
this activity we are going to show only
the image and the book name but if the
user wants to see some more information
he or she can click on some arrow button
in here and expand our card view later
on we will create that also I forgot to
add a margin in here let's quickly do
that let's say margin and I believe 5 DP
would be fine ok now that we have
initialize our UI elements inside this
view holder class for that in here I'm
going to say private card view for the
parent let's name it parent beside that
once again I'm going to name is IMG book
and also let's say private text view
let's say txt name I believe we did not
define an ID for this text view that's
so in here for this text view let's pass
on ID let's say txt book me okay back in
our adapter class inside the constructor
of this view holder class after the
super statement let's initialize our UI
elements we have done this before
we can say parent is equal to item view
let's pass our dot ID does parent
so after creating this inner class we
can extend the recyclerview class in
here and we can pass this view holder as
the datatype of our adapter so I can say
recycler view dot adapter and I can pass
my view holder class make sure to import
the one that comes from your package
work that may put that my library dot
block recycler view adapting you'll know
that in here right now we need three
let's press ctrl + v and let's import
our methods before doing anything inside
these methods first of all above in here
I'm going to define an array list let's
say private ArrayList of different books
let's name this array list books is
equal to new ArrayList so this array
list is going to be the list of all of
the books that we are going to show in
our activity beside these books
ArrayList I'm also going to need a
context because if you remember when we
are going to use glide in our project
client library needed the context to
show the image so let's say private
context let's name it in context after
that let's create our constructor in
here I'm going to receive only the
context for the books aerialist we are
going to create a setter method let's
create that method down in here by
pressing Alt + insert once again but
select setter and let's create a setter
method for our books are released you
know that when you set your books this
way you also need to add a method in
here and that method is notify data set
changed we are adding this method
because we are going to refresh the data
in our recycler view okay let's move on
from this part and let's start working
on this get item count method but before
that I'm going to minimize this view
holder class this get item count method
as I said before is just returning the
size of items or the size of least in
our recycler view so in here i can say
nothing special is going to happen
inside this uncreate view holder method
we need to create an instance of our
view holder and we need to return that
probably all the times the code inside
this on create view holder method is
going to be the same in all cases so you
can save the copy and paste but I'm
writing in here so I can save you
let's name it view is equal to layout
inflator if you remember let's say that
from we need to pass a context in here
and we can pass the context by using
this view group let's say parents that
get context after that we can call the
Tottenham flight method in which first
of all we need to pass our layout I can
say our that layout dot list item book
after that we need the view group itself
in order to attach our layout file this
list item block to its parent and after
that I am going to pass false in order
to avoid redundancy after creating this
view object you can create your view
holder quest let's say view holder let's
name it holder is equal to new view
holder and let's pass our view object
after that I can return my holder also
this method in here can be simplified
right now you can see that this local
variable is redundant it means that you
can directly return it instead of
creating an instance of that holder
class so in here what I can say is that
I can say return new view holder and I
can pass my view once again in most
cases this is exactly the code that you
need to put inside this one create new
holder method after that down below
inside this unbound view holder method
first of all I'm going to lock something
indicating that this method has been
called in order to add a lock a bug in
here first of all I need to add a
constant after that inside on point view
holder method I'm going to say lock D
let's just say called first of all let's
set the name for our book I'm going to
say holder dot txt name that's at
let's say books that get let's fast
position let's say that get named after
that we need to show our image in which
for that we need to collide library for
that I'm going to search the internet
for Glide dependency the first link
which is a github link is probably our
first of all let's add these two
dependencies in our project inside
grading scripts inside this built at
Gradle modular inside these dependencies
I can paste them let's also check that
if we have this maven central and Google
repositories in our built at grader
project one it seems like we need to add
the maven repositories let's copy it
from here maven central and let's paste
ok let's sync our project ok it seems
dependency successfully let's switch
back to our adapter class and inside
this unbound view holder method I'm
going to say flight dot with here we
need our context which was named in
context after that let's say as bitmap
dot load we need an image URL in here
which I'm going to say it pops that yet
let's pass our position let's say that
get image run and after that we need our
image view let's say that into our
holder dot my mg book ok this way we can
show the name and image of our book I'm
also going to set an onclicklistener for
the card view parent for the time being
by clicking on this card view we are
going toast message but later on we are
going to create a book activity and we
are going to navigate the user to that
activity so for now let's say holder dot
parent dot set on click listener let's
say in the onclick listener and let's
show our toast message let's receive the
name of our book I'm going to say oops
that get let's pass our position that
okay I believe that's all we need for
now inside this book recyclerview
adapter later on we are going to change
this a lot but for now let's initialize
it inside our all books activity and
let's set it as the adapter for this box
recyclerview I'm going to define the
adapter above in here as a member
valuable so that we can have access to
this method inside different methods I
think we are going to need that let's
say private books recyclable your
adapter let's name it adapter down in
here before initializing our recycler
view adapter I'm going to say adapter is
equal to new books recycler view adapter
and let's pass our context once again
because we are inside an activity we can
pass this after that I'm going to set
this adapter as the adapter of my
recycler view let's say books recycler
view dad set adapter let's pass our
adapter also for the layout manager I'm
going to pass a grid layout manager
let's say box recycler view that set
layout manager new grid layout manager
this one first of all we need the
context let's pass this after that we
need the number of columns which I'm
going to say to after setting the
adapter we need to set the data I mean
the books area list for the time being
for testing purposes let's just create a
dummy ArrayList and let's pass it to
this adapter let's say array list of
different books let's name it pops is
equal to new ArrayList after that let's
say books that add and let's add a book
let's say new book for the idea of this
book I'm going to save on for the name
let's say 1q84 for the author let's say
Harlequin Murakami after that let's
I believe it's 1350 pages let's quickly
copy some image URL let's search for
1q84 and I think this one is good let's
paste it in here just make sure that
your images and weights that jpg or dot
PNG let's go to the next line let's see
what else do we need we need a short
description and also a long description
for the short description I'm going to
say a work of madelung variants
I believe it's about the Time magazine
for the long description I'm just going
to pass along description because we are
not going to show that yet okay after
adding this book I'm going to say
adapter that sets books let's pass our
books ArrayList before testing the
application let's quickly add the
internet permission because we are going
to load the images from Internet we need
that permission in our manifest file
above the application tag I can say
users permission and I can pass internet
permission okay let's test our
application and let's see if we can see
this put in our activity let's click on
this see all box button as you can see
we are getting a card view for our book
but we are not seeing the image if you
remember I mentioned this problem in
previous videos the reason that we are
not seeing this image is because we have
changed our manifest file we have added
the permission and because we have
changed our manifest we need to
uninstall the application and install it
once again we will do that in a minute
but before that you can see that we have
this ugly background in here I'm going
to get rid of that and beside that I'm
going to add a padding for my recycler
view right now discard view is too close
to the edges of our screen let's quickly
fix those issues so inside our activity
first of all let's delete this
background color after that in our
recycler view item I'm going to add a
margin similarly you can add the padding
on your constraint layers I'm just going
to add the margin in my recycler view
let's pass 15 I believe that's good okay
before testing the application I'm going
to uninstall it from the device so if we
in the apps and notification we can see
this my library application let's
uninstall it and let's run our
application once again this time if we
click on this CL box button after a time
we should see our image it takes some
time to load the image but after all we
can see that let's also check the
on our card here so if we click on our
card view you can see that we are seeing
this toast message which seems to be
working fine okay I think that's enough
for this video in the next video we are
going to work on that expandable card
view that I was talking about so
basically right now we are only seeing
the name and image of this book in the
next video we are going to add a little
arrow button in here in which by
clicking on that the user can see more
details about our book just before I
finish off this video once again I'm
going to say that if for any reason you
need to check the source code I will
upload the source code at the links that
you can see on the screen at make up dot
or feel free to check that if you need
and also I would love to hear your
feedback on the code that I just write
in this video we are going to work on
expandable card view that means that we
are going to show this simple card view
plus an arrow button in here an arrow
icon in which if the user clicks on this
arrow we are going to expand our card
view and we are going to show some more
information for example in this case we
are going to show the name of the author
of this book mas we are going to show
the description the short description of
this book in this video we are going to
see how we can implement that but I need
to say that this way of implementing and
expandable card view inside our
is not going to work with our grid
layouts manager it works perfect with
linear layout manager but for grid
layout manager you need some more
information we will talk about how to
implement an expandable card view in
grid layout manager later on in the
course so because of that first of all
inside my own books activity I'm going
to change the layout manager for my
recycler view to a linear layout manager
and also I am going to increase the size
of this card view so let's switch to all
books activity this one and then in here
you can see that we are using a grid
layout manager let's quickly change that
to linear layout manager we need a
context which I'm going to pass this
let's close this file for now we will be
coming back to this file and let's
switch to a split view and let's write
expandable view for this card view so
here is what we are going to do we are
going to create another relative layout
then below this one that we already have
and we are going to change its
visibility to run later on in the code
inside the recyclerview adapter when the
user clicks on our arrow bottom we are
going to change the visibility of our
relative layout to visible plus we will
add some animation for adding another
relative layout in here I am going to
add one above in here as a single child
of this court view and I'm going to put
this relative layout that we currently
have inside that so let's say relative
layout for its bits I'm going to say 200
DP and for the height I'm going to say
wrap content also I'm going to change
imageview to 200 TP if you remember I
said that because we are no longer using
a great layout manager I'm going to
change the size of this card here so
let's say 200 DP and for the height
let's say 220 I believe is fine now we
let's finish creating our relative
layout and let's move this one to inside
the first relative layout so I'm going
to copy everything but I think it's
better to minimize this relative layout
and copy everything or cut everything to
be precise and place living here you can
see that now our chord view has this
strange shape that we can fix that so if
we open this relative layout you can see
that for the height we have said match
parent I'm going to change that to wrap
wanted as soon as I do that you can see
in the preview that the height has been
okay let's minimize this one after this
relative layout I'm going to create
another relative layout which is going
to be the expanded version of our chord
view so let's say relative layout for
the width I'm going to say match parent
for the height let's say wrap content
I'm going to move it to below the first
relative layout so let's give an ID to
our first relative layout let's name
this bond collapsed relative layout once
again let's minimize it now in here I
can say below our collapsed relative
layout inside this relative layout as I
said I'm going to define some text views
I'm going to show the name of the author
of this book plus I'm going to show the
short description so first of all let's
say text view wrap content and wrap
content for its text I'm going to say
author let's give it an ID let's just
say author text I'm not going to change
the style or anything after that let's
and define another text view for the
Viets that's a wrap content similarly
for the height wrap content for its text
I'm going to put the name I'm going to
put my name for its ID I am going to say
TX the author this is the one that we
are going to change later on in the code
let's move it below our author text
let's say layout below text author or it
was called author text let's add another
text view in here text view wrap content
and web content for its text I'm going
to save short description and let's also
give it an ID that's a txt short
description that's also move it to below
our author name let's say layout below
TX the author I think it's better to add
a margin let's say margin top I'm also
going to need two icons let's quickly
add them in our project and we'll see
why I'm adding these two so in our
driver folder we can say a new image
let's select action bar and have icons
for the icons I'm going to search for
arrow the first one I'm going to add
this one this down arrow let's change
its name to I see down arrow and also
let's change its color to a black color
let's add another one new image acid
once again this time I'm going to search
for an up arrow this one in here I think
is fine let's change its name to up
arrow by CEO arrow and let's add it into
our project now in here after this
description text I'm going to add my up
arrow so if the user decides to collapse
this current view your she would be able
to do that by clicking on our up arrow
so let's say image view graph content
and wrap content for its source let's
up arrow this one let's move it to below
our description text to X the
description and also let's move it to
the end of our card here for that we can
say alloy in finance and and we can pass
through let's give it an ID let's say DT
an up arrow okay that's it for my
expanded view now that I have defined it
I'm going to change its visibility to
gone but before that I'm going to give
an ID to this relativelayout because we
our adapter let's say ID this time I'm
going to name it expanded relativelayout
and let's change its visibility
visibility to gone also let's minimize
this relative layout so now you can see
that we have two relative layouts the
first one is the collapsed relative
layout which the user is going to see
when he or she opens our activity and if
the user clicks on the arrow that I'm
going to add in a minute we are going to
show this second relative layout
let's quickly add that arrow button so
down in here I'm going to say image view
wrap content and wrap content parse
again for the source let's say down
arrow this time I see down arrow let's
move it to below our image that's a
layout below IMG block also I'm going to
add the same margin that I added for
this text name so let's say margin top
and let's pass 70 P you can see that for
this textview we have the 70 PS back ok
let's give it an ID let's say BTN down
let's also move it to the end of our
layout online parent and let's say
through I think it's better to change
the position of name text to the
beginning of our card view now that we
have added this icon let's quickly do
that for this text view I'm going to
remove this Center horizontal attribute
I don't think we need anything else
let's switch to our adapter and let's
see how we can work on this down and up
arrow buttons before everything before I
go to my adapter I am going to open my
model my book model and I am going to
add another field so inside our package
we had a book class if you remember I'm
going to add another field in here let's
say private Bruneian let's name it is
expanded I'm not going to receive this
boolean via the constructor instead
inside the constructor initially I'm
going to set it to false so whenever we
create our book object I'm going to set
this value to false let's say is
expanded is Dakota farms let's also
create a getter and setter for this is
we are going to need them getter and
setter let's create them okay now let's
switch to our adapter class and let's
see how we can implement this logic poke
recycle every adapter first of all
inside this inner viewholder class
inside the constructor I'm going to set
an onclicklistener for that and down
arrow button but in order to have access
to that I need to initialize that in
here so let's say private image view
down arrow and pop arrow let's also
define the other UI elements for example
we had a relative layout let's say
private relative layout expanded
relative layout we had two new text
views private text view and TX the
author and txt description let's quickly
initialize them in our constructor now
that we have initialized these items I
am going to set an onclick listener for
this down arrow button let's say down
arrow that's set onclicklistener neon
click listener inside this unclick
method first of all I'm going to get the
current book that we are looking into in
previous videos we have seen how we can
get that book inside unbind view holder
method with the help of that position
but it is also possible to get it inside
this view holder class we can get it
like this we can say work let's name it
Talk is equal to our books ArrayList
that get this time for the position I
can pass get adapter position so this
way we are going to get the book that we
are currently creating a view holder for
that after getting this book we are
going to change the field that we just
defined I'm talking about that is
expanded boolean so whenever the user
clicks on this down arrow we are going
to change that is expanded field of our
book I'm going to say book dot set
expanded let's say the inverted value of
our book dot get expanded or I believe
it was is expanded instead of this line
of code in here and you can write an if
statement but I've simplified my code to
this way so basically we are inverting
the is expanded value of airport
there is just one last thing that we
need to do in here and that is to tell
the adapter that we have changed some
item in our dataset in previous videos
we have seen at this notified data set
this one but in here because I'm only
changing one item I'm going to say
notify item changed and of course in
here once again I need the position
which I can pass this get adapter
position so this way we are going to
update our recyclerview adapter later on
in our on point view holder method we
are going to take care of the expanding
and collapsing our cart view beside this
onclicklistener for this off arrow let's
quickly write that as well let's say off
arrow dot set one click listener me on
click listener and in here the logic is
the same once again we are going to
invert the value of our books is
expanded field so basically I can copy
and paste these three lines of code
let's face them in here so by clicking
on our down arrow or off arrow we are
inverting this is expanded field and
that's all we need to do inside this
view holder class let's see what do we
need to do inside on brined view holder
method so in here first of all I'm going
to check that if these expanded is true
or not let's say if Bob start get let's
pass our position but is expanded it
means that if we do need to expand our
card view then we are going to do that
and we can do that like this we can say
holder that expanded relative layout
that set visibility let's change it to
visible we also need to make our down
arrow icon invisible for that I can say
holder dot down arrow that set
set visibility let's say gone let's also
write the n space in the else case we
are going to change the visibility of
this expanded relative layout to gone
and this down arrow to visible so let's
copy these two lines of code and let's
change their values this in here is
going to work fine but if you remember I
said that we are going to also add some
sort of animation to this expanding and
for that before changing the visibility
of our UI elements I'm going to call
this transition manager I'm not sure
that if you have noticed but it's coming
from this Android extra transition make
sure to import the right one let's say
dot begin delayed transition for this
method in here we need to pass our route
in this case the route is our card view
so we can say holder dot parent if you
remember we gave an ID to our card view
which was parent and that's all we need
to do let's also do the same thing
inside the else case so by this point we
have changed the visibility of our
relative layout but also we need to set
the data for the author's name and also
for the short description I can do that
before this leaf case I can say holder
dot txt author dot set text two books
but yet let's pass position don't get
author similarly for the short
description let's run the application
expandable card view let's go to our
outputs activity you can see that right
now we are seeing the collapsed version
of our card view also the size is bigger
we also need to move this recycler view
discord view to the center of our layout
we will fix that but right now if we
click on this down arrow in here you can
see that we are seeing the expanded
version of our card we once again if we
click on this up arrow we can see the
collapsed version of our card view it
seems like our logic is working fine
let's quickly Center discord view and
also let's add another item into our
and let's see if they're collapsing and
expanding is working the same or not so
first of all I'm going to go to the
layout of my old books activity in my
app folder inside the resources layout
activity all books that XML I'm going to
change it sweets to wrap content and
also I'm going to Center it horizontally
but in here we are using this constraint
layout so for that I need to constraint
that seems better let's quickly add one
in our java file in our books activity
go to Java file down in here I'm going
to add another book I'm going to cut the
process of adding a new book okay now
that I've added another book let's
quickly run the application and let's
see if our expandable card view is
working the same let's go to our books
activity this time you can see that we
are seeing two books if I click on the
down arrow of the first one you can see
that we are seeing the short description
and also the author name but the second
book is still in the collapsed version
we can do that for the second book as
well let's quickly change the linear
layout manager to a grid layout manager
and show the problem with grid layout
manager so in here we need to pass a
column number which I'm going to fast
too let's run the application once again
so in here we have two books in our
layout file in our recycler view if I
click on this down arrow which you can
hardly see we can see that the layout
file for both of these card views
changed so this way of implementing an
expandable card view is not going to
work for our grid layout manager for
implementing the expanded court view in
our grid layout manager we need to know
a bit more about the circular views for
example we need to know that in
recyclable adapter we can have multiple
view holder classes and handling two
different view holders in recycler view
adapter is out of the scope of this
section we will cover that later on in
the course but for now we are just going
to stay with a linear layout manager our
work with expandable card views are not
done yet later on in the course we will
add some action to this expandable view
for example some buttons to delete this
book from a specific list in our
application but for now I think that's
enough and also I think that's enough
for this video in the next video we are
going to create our book activity in
which if we click on one of these card
views instead of showing this toast
message we are going to navigate the
user to another activity in order to
show some more detail about our book and
also beside in a showing more details we
to give the user some options for
example to add book to some of the lists
okay see you in the next video
in this video we are going to create a
book activity and we are going to
navigate the user when he or she clicks
on one of our books in our recyclerview
for that in my package I can right click
once again by selecting new empty
activity I can create my activity let's
name this one book activity I am going
to generate a layout file so I'm going
to leave this checkbox checked
let's close this drama file and let's
start working on our layout so our
layout is right now a constraint layout
first of all I'm going to add an image
viewing here for the image of my book
for the sample data let's select an
avatar after that I'm going to add four
buttons in here we will talk about the
purpose of these buttons for now let's
just add them after that I'm going to
add some text views in here for the name
author and description of my book I'm
going to give some ID to these UI
elements and beside that I'm going to
set some initial data for them because
we have done this before I'm going to
record the process of doing that I've
added the initial data for each one of
these items plus I gave them some ID you
can see the ID for example for this
first button which is a bt an add to
currently reading list so basically by
clicking on this button I'm going to add
this book to the currently reading list
it means that I'm currently reading it
you can guess the others this is for
adding to the visual is this is for the
books that I already read and the last
one is for adding two favorite books
also down in here we can see the books
name books author books pages and also
the books description and this image is
going to be the image of our book okay
now let's quickly add some constraints
for this one I am going to constraint it
to top of my screen and also to the left
of my screen beside that I'm going to
add a vertical guideline in here that's
right click amperes add vertical
guideline and let's move it to somewhere
around here and let's constraint our
I think 50 would be fine let's quickly
constraint our buttons for this one I'm
going to constraint it to top let's say
15 as the margin top or 50 let's quickly
constraint it to the edge of our screen
and also this vertical guideline I'm
going to do the same thing for my
buttons for the other buttons just for
the margin top I think I'm going to stay
with 24 let's add a horizontal guideline
and let's constraint our text views to
that horizontal guideline so let's say
add horizontal guideline let's move it
to down below somewhere around here
let's also add another vertical
guideline we will see its usage in a
minute add vertical guideline
I think it's place is fine and I'm going
to constraint these text views to this
guideline in here let's do the same
thing for the other text views
also I'm going to add another vertical
guideline let's say add vertical point
let's move this vertical guideline to
somewhere around here and let's
constraint these three texts to this
vertical guideline you can see that
guidelines are extremely useful when you
are designing your layout files with
constraint layout for the top of this
text view I'm going to constraint it to
the top of my book name also the same
thing for the button you can also use
the baseline but in here I've used the
top and bottom constraints okay I think
that's all of the constraints that I
need I just want to change the width and
height of this image view to somewhere
around 150 DP because different books
can have different widths and height
okay now let's quickly initialize all of
these items in our Java file let's
switch to report activity that's Java
file and let's initialize that I'm going
as you can see I've initialized all of
my UI elements now it's time to set some
values for each one of these books for
now I'm just going to create a simple
plug-in here and after that depending on
the books value I'm going to change the
value of all of these UI elements but we
shouldn't do it this way because we are
going to change the values of these UI
elements depending on the item that we
click on our recyclerview we will see
that how we can get item that we have
clicked on later on but in here just to
test our UI I'm going to create a book I
think I have a book in my old books
activity so I'm going to copy it from
there after creating this book I'm going
to create another method let's name it
set data also I'm going to pass my book
object to this method let's quickly
create this method let's say private
void set data and let's receive a book
before I do anything inside this method
I am going to add a to doing here so
that we don't forget to get the incoming
data from the recycler view okay inside
the set data method I'm going to say txd
botany that set text let's say book that
get me and similarly for the other three
text views for these pages in here
because we have set the pages as an
integer in our book model we need to
cast it into a string in here and here
is how we can do that I can say a string
with capital S dot value of let's say
book dot pages or that get pages this
way we can cast this integer to a string
and after that we can pass it to this
set text method for the description
let's do the same let's say that's a
text book that get knowing description
let's also show our image I'm going to
use glide collide that with this is an
activity so I'm going to pass this as
bitmap dot load book that image URL or
get image URL dot into our pot image so
this way we can set the data in our you
elements in order to test this
application I'm going to go to my
recyclerview adapter and from there I'm
going to create an intent so right now
inside this folder that parents
onclicklistener we are sharing a toast
message and let's remove this toast
message and let's create an intent
intent is the call to mere intent for
the context in here if you remember we
are receiving a context where the
constructor of this class so I can use
that I'm going to say M context for the
destination that's a book activity dot
class after that usually if we were
inside an activity we could have used a
start activity method but in here you
can see that we don't have that option
because this start activity method is
specific to activities if I want to use
that I can say M context dot start
activity so with the help of our context
we can have this start activity method
and after that we need to pass our
intent so right now no matter on what
book we click we are going to navigate
to book activity and in there we are
going to show the details of this 1q84
book I'm doing this because I'm going to
test my UI let's run the application and
let's see if everything works fine let's
go to our all books activity and in here
if we click on this book for example you
can see that we are seeing the details
of this book but right now our layout
has some problems for example we need to
increase the width of our buttons
because they don't fit in one line right
now and also I'm going to increase the
size of this image view let's quickly
fix these two issues in our activity
book if I click on this book image I'm
going to change this font this width and
height to 200 T P answer for these
buttons let's increase the beats from
150 to I believe 170 would be fine let's
run the application one more time and
once again let's go to our book activity
this time it's somehow better also we
need to be careful about this long
because if this description don't fit on
the screen and be probably and I
that I'm going to add a scrollview into
my layout fight we haven't seen a
scrollview but its purpose is to create
a view that we can scroll let's quickly
see how we can add that in our
constraint layout so basically what I'm
going to do is that I'm going to change
these parents to a relative layout after
that I'm going to create a scroll view
inside that scroll view I'm going to
create a constraint layout and I'm going
to move all of these items into that
constraint layout let's see what I'm
so as I said I'm going to create a
scroll view for the width and height
let's say match parent inside this
scroll view I'm going to create a
constraint layout once again for the
vision height let's say match parent
after that I'm going to move all of my
elements to inside this constraint
layout and after everything I'm going to
change this constraint layer to a
relative layout so let's select all of
these items and after everything as I
said let's change the parent to a
relative layer let's enable the split
view and let's see if we have a good
layout it seems like we have done a good
job let's run our application let's go
to our book activity and we can see the
same result let's quickly add a longer
description and that's if you can scroll
our view I've copied the text in my book
activity I'm going to set that text as
the long description of this book but
before that I'm going to create a string
in here let's say long description is
equal to this text that I'm going to
now that we have this text let's set it
as the long description of our book
let's run the application once again and
let's see if we can scroll our view
let's go to block activity and in here
you can see that you have a long
description that does not fit on our
screen but as you can see we can scroll
this view ok I think that's enough for
this video this was a short video just
to create a layout file for our book
activity in the next video we will see
that how we can pass our book from our
recycler view to this activity and
beside that we will work on the logic of
these buttons see you in the next video
in this video we are going to write the
logic for these buttons we are going to
add our books two different categories
by clicking on each one of these buttons
but before I do that I need a data
storage to save these books to different
lists in a normal application in this
situation you may want to use a database
a database is a place that you persist
your data it can be a local database on
your smart phone or it can be an online
database on a web server in here we
haven't talked about any of those we
haven't talked about databases yet we
are going to talk about them later on in
the course in fact we have a section
dedicated to databases but in here we
are going to use an alternative we are
going to use some of the capabilities of
java language in previous videos in java
section if you remember I talked about a
static keyword I said that when you
define a variable or an object as a
static that variable will have the same
value in all of your application
regardless of where you have initialized
it in here we are going to take
advantage of that and we are going to
define our different lists of books as
static variables this way we have the
same lists when we are working with our
application in different activities but
there is a downside to this way of using
a static values your data would not be
persisted it means that if you close
your application you lose all of your
data at the end of this section I will
introduce another alternative in which
you can use to produce your data but in
here for now let's just use a static
variables for that I am going to create
a utility class in my package and we
will see in action why I am creating
this class so in my package let's
right-click new Java class I'm going to
name it utils in a real world
application this utils class is probably
your database class but of course the
name should be different but in here we
are not using databases so I named it
utils the first thing that I'm going to
do inside this class is that
to make this class Singleton so I'm
going to implement singleton pattern in
here inside this class if you remember
from the Java section whenever we
implement a singleton pattern for a
class we can have only one instance of
that class in our entire application I'm
making this class singleton because I
want only one instance of this class
basically this is the class that we are
going to interact with our aesthetic
and for that I am going to be sure that
I have only one instance of this class
ok let's quickly see how we can
implement singleton pattern first of all
I'm going to create a constructor for
this class it's an empty constructor and
right now we don't have anything after
that I am going to define a static
instance of this class let's say private
a static utils the type of our class
let's name it instance beside that I'm
going to need a getter for this instance
you remember all of these from the Java
section so I'm not explaining them let's
say getter for the instance first thing
I am going to change the modifier of
this constructor to a private let's do
that and inside this get instance method
I am going to write the logic to make
this class a singleton so I'm going to
say if now is not equal to my instance
then return the instance but in the else
case in case if the instance is now
let's say instance is equal to new you
tools and after that we can return our
instance let's say I return the instance
because we are returning in each one of
these cases we no longer need this
returned instance down in here in the
Java section we also have used the
synchronized keyboard if you remember we
in there I said that the synchronized
keyboard will make your method to be
thread safe it means that if different
threads at the same time call this
method this synchronized the keyboard
will cause them to call this method one
by one and not at the same time but in
this application we are not going to use
threading we are going to have only one
thread called the main thread so for
that I'm not worried about trading issue
in here and I can safely delete this
synchronized keyboard so by now this
utils class is a singleton class now we
can create our aesthetic lists so for
example in here I can say private static
let's say ArrayList of different books
let's call this phone all books as you
can guess this one is going to represent
all of our books in our application but
where do we initialize this all books
ArrayList we know for sure that when we
create an instance of our utyos' class
we are going to call this constructor so
the perfect place to initialize these
all books ArrayList is inside this
constructor let's move it to down below
this all book so that we can follow the
logic better and inside this constructor
let's say if null is not equal to our
we don't want to reinitialize our
ArrayList we just want to initialize it
one time when we create our application
or better than that would be to say if
null is equal to our old books I'm going
to say all books is equal to new
ArrayList and after that I'm going to
create another method in order to add
some initial data to this all books
ArrayList so let's say in its data let's
go to create this method by pressing Alt
+ Enter you can create your method for
now I'm just going to add a to doing
here and later on we will complete this
method let's say add initial data in
case if you have forgot about these two
do's and these are some sort of comments
that you live for yourself or for your
colleagues so that we don't forget
anything later on and when you want to
go in production you can check these two
and you can see the works that you left
for the future okay let's move on from
this part beside all books are released
I'm also going to define other ArrayList
let's say private a static array list of
different book let's name it already
read book let's define three more
one for currently reading books one for
one to reading books or wish list books
and another one for favorite books
similar to our old books aerialist we
need to initialize this ArrayList inside
our constructor as well so down in here
once again I can say if null is equal to
my already read books then I'm going to
initialize it let's say already read
books is equal to new ArrayList but I'm
not going to add any initial data
because we are going to leave that
option for the user later on when he or
she uses the application let's do the
same thing for the other three array
we now have initialized all of our lists
now let's see how we can get the values
or the items from our lists for that I'm
going to create get our methods for
example for this all books I released if
you want you can create a getter let's
quickly create that down in here by
pressing Alt + insert you can create
getters for all of your lists let's add
some initial data to our all books
ArrayList and that's an test this UTS
class and let's see if everything works
fine so if you remember in all box
activity we have created this array list
manually after that we have passed these
energies to our recyclerview adapter I'm
going to cut the initializing of our
array list from here and I'm going to
move it to my utils class right now we
have an error we will fix that so inside
utils class inside this in its data
method after this to do I'm going to
paste my ArrayList this time instead of
saying books that add which is this
array list I'm going to say all books
that add this one in here so this way we
are adding two books to our all books
ArrayList I can safely delete this one
as well remember that we are calling
this init data method from inside our
constructor when we are creating our all
so whenever we run our application and
we call this utils class this utils that
get instance method we have some data in
our all books ArrayList now that we have
some data in our all books activity
instead of these books we can say utils
with this capsule you don't get instance
if you run our application if everything
inside our util class works fine we
should see two books in our all books
activity let's quickly test that if we
go to our all books activity you can see
that we are seeing two books it seems
like our users class is working fine we
ton of more codes inside this UTS class
but more on that later on for now let's
see how we can pass a book from our
recyclerview adapter to the book
activity for example right now if I
click on this the myth of sisyphus you
can see that we are seeing this 1q84 it
means that we didn't pass our book to
this book activity in fact in our book
activity we are defining the book itself
and we are not getting any data from the
recyclerview let's see how we can pass a
book from our recyclerview to this book
activity so if you remember inside the
book recyclerview adapter inside this
holder dot parent thoughts at
onclicklistener inside the onclick
method we have created this intent the
use of this intent is to navigate the
user to the book activity the good thing
is that with this intent you can also
pass some sort of data let's quickly see
that as well so for example in here I
can say intent dot put extra you can see
that we have a ton of these different
methods for all sort of variables and
all sorts of data let's use one of them
for example I can pass the book ID in
here you can see that in here we need
two arguments a string and after that
data that we want the string is always
the key or the name of your value in
here for example if I say book ID later
on inside my book activity with this
book ID I can get the book ID so the
values inside your intents are key value
pairs for the idea of this book I can
use this books I release that we have
inside this adapter I can say books that
get let's pass position let's say target
ID so this way we are passing the ID of
our book - the book activity later on in
our book activity we will get our book
by its ID if you remember I said that
the ID for each book is going to be
unique it's worth mentioning that you
can also pass the book itself you can
that instead of passing an integer we
can pass a course a little bit let's
quickly see that you can say intents
that put extra and then in here if you
take a look one of the arguments in here
is this part of the bill we will talk
about cars available later on in the
course but basically it's an interface
that we will not serialize your class at
the time of transferring it from one
activity to another activity and at the
deserialize your object don't worry
about these two words that I am using
basically using parsable is really easy
we will take a look at that later on in
the course but for now we are just going
to pass an integer beside that you can
also set multiple extras for your intent
for example if I want to pass the book
name in here once again I can say that
put extra for the key I'm going to say
for the name and if I want I can say
oops that get position do not get me of
course we are not going to need this in
here but I just wanted to say that you
can pass multiple extras with your
intent okay now that we are passing some
data with our intent how we can retrieve
that data in our book activity before
that let's quickly remove this one to
have a reviewing here first of all we
are creating our intent the context is
this M context which we are receiving
with the constructor of this adapter the
destination of this intent is book
activity dot class we are calling this
pot extra method on our intent and with
that we are passing some integer in this
case we are passing the book ID after
that with the help of our context we are
starting to activity which in this case
is this pub activity dot class so now we
can continue our code from inside the
book activity inside on create method
let's close this one also I'm going to
close this one utils class my layout
file and let's open book activity
so inside this on create method in here
if you remember we are creating a book
and we are setting its value to our UI
elements you can see that we also have a
to-do in here get the data from circular
view in here so if we want to get the
data in this case the book ID from our
recyclerview we can say something like
this we can say intent let's call it
intent is equal to this time instead of
saying new intent I can say get intent
so if you use this get intent method you
are going to receive the incoming intent
the intent that caused this activity to
be launched but you also need to be
careful about non values of this intent
because this intent can be null at some
times so for that I can say if null is
not equal to my intent I'm going to
continue in here after that I'm going to
receive the book ID I'm going to say int
book ID is equal to intent dot get int
extra you can see that we have a lot of
this get extras method for different
kind of data for example if you are
going to get an integer we can call this
get into extra we can see that the
method in here is waiting for two things
you can press down the control key and
you can see the documentation first of
all we need the name of our variable
which if you remember was book ID and
the second one is a default value so in
case if for any reason this get into
extra wouldn't be able to get any data
from our intent we are going to assign a
default value for the name let's pass
pop ID and for the default value I'm
going to pass negative one because I
know for sure that and none of my books
in my list are going to have negative
one as their ID also about this name
this key t spoke ID in here you need to
be extra careful about this the
characters the upper cases and their
cases need to be exactly the same if you
put anything wrong in here you are not
going to retrieve your data there is a
better option than hard-coding your keys
passing your kids manually you can
create constants for example above in
here I can say public static final
string the convention is to name your
constants in upper cases for example in
here I can say book ID key I can also
assign a value to this one for example I
can say book ID now that I have defined
this constant in here I can pass it in
two places for example the first time I
can pass it as a key I can say book ID
key and also inside my adapter class
instead of hard-coding the key in here I
you can see that we need to import this
by pressing Alt + insert we can import
this constant into our class if you take
a look at above in here in the imports
we are importing this constant into this
class so this way by using different
constants you make sure that the keys
are the same in two different places
okay let's switch back to our book
activity so by this time after this line
of code we either have a negative one
value or we have the book ID we need to
create an if statement for this negative
one value let's say if our work ID is
equal to negative one or the better
logic would be - if it's not equal to
negative one let's pursue the code so by
this time inside this if statement we
have our plot ID how do we get the POC
by its ID if you remember at the
beginning of this video I said that we
interactions with our lists inside the
UTS class so for that once again I'm
going to go to my utils class I'm
pressing double shift in order to pop up
this search menu if you are wondering
anywhere in Android studio you can press
shift two times and you can get this
menu in which you can easily type and go
to your class for example inside this
utils class after everything I'm going
to create a method I'm going to say
public book is going to be the return
type of this method let's say get book
by ID we also need to get the ID let's
say int ID after that inside this method
I'm going to login to my all books
activity and I'm going to return the pot
by its ID for that I'm going to create a
for loop for each loop to be precise
let's say for book be inside our all
books are released in case if B dot get
ID with equal to the ID that we received
then we are going to return B
so if we have a book with this idea we
are going to return it but after the
four do if we didn't return anything we
are going to return now by returning
null in here we are acknowledging that
we may not found a book with this ID in
our list so whenever we call this get
put by ID method we also need to be
careful about the null values because
the result can be known for example
inside the pot activity in here I'm
going to say book let's name it
incoming work is equal to our utils
class that get instance first of all dot
let's say get book by ID after that we
can pass our book ID after this line of
code we need to make sure that this
incoming book is not known so let's say
if null is not equal to our incoming
book then we can call our set data
method which is this method I can
account this and I can paste it inside
this if statement you can see that
before getting our book and before
passing it to this set data method we
are doing three security checks first of
all we are checking that our intent is
not now after that we are making sure
that we have received some data from our
after that we are checking that if we
have such a book with this ID in our
list in a normal scenario in a good
situation none of this would be
necessary and you can simply call these
get books by ID method and you can get
your book but a lot of things can happen
when you call your activity and when you
pass your data your activity may be
destroyed your activity may be
interrupted by some call that user
receives your activity also may be
called from different places and also
for other security reasons you always
need to check for the intent for the
incoming data and for the data itself
inside your data storage sorry I need to
change this book to incoming booking
in there okay now that we are setting
our incoming book we can safely delete
the spot above in here let's comment all
of this let's test our application and
let's see if we can successfully
navigate the user to book activity and
show the appropriate book let's go to
all books activity 1q84 we are saying
1q84 if we click on the myth of sisyphus
you can see that now we are seeing the
meetups is the first it seems like we
are passing the data from our recycler
view from inside the all books activity
to the bot activity successfully okay I
think that's enough for this video I did
want to talk about the logic for these
buttons but I think that's better to be
left off for the next video okay see you
going to take care of these buttons we
are going to add the book to different
lists by clicking on each one of these
buttons beside that we are going to
disable the button if the book already
exists in one of these lists for example
if the book exists in the users wish
list we are going to disable this one to
read button let's go to book activity
and let's see how we can do that so
inside this book activity insight is on
create method I am going to call another
method but I am going to do that inside
this if statement after I made sure that
I have an incoming book so let's name
this method and a handle already read
I'm also going to pass the incoming book
itself let's quickly create this method
down in here let's say private void and
let's also receive a book inside this
method first of all I'm going to check
that if this incoming book exists in our
already read books so for that I'm going
to say ArrayList of book let's name it
is equal to you Till's not get instance
this method after that I'm going to
create a for each loop let's say for
book be inside our already read books
also before this for loop I'm going to
define a boolean and its purpose will be
so that's a boolean let's name it exist
let's initialize it to false down in
here inside the for-loop I'm going to
say that if B that get ID is equal to
our incoming pour target ID if that's
the case we are going to change the
value of this boolean let's say exists
in already read books is equal to true
so after this for loop we know that if
the pop exist in our already read books
ArrayList and we can use this program we
can say if existing already read books
is equal to true then we are going to
disable our button because we don't want
to add this book one more time in the
already read books ArrayList so let's
say BTN already read or between add to
already read that set enabled this
method and let's pass false so this way
we can disable a button in the else case
I'm going to set an onclicklistener for
this button and I'm going to add it to
my already read books ArrayList so let's
say B thean add to already read that set
onclicklistener and inside this onclick
method we need to add it to our list but
because I'm going to keep all of my
interactions to my lists inside the
utils class I'm going to create that
method inside the utils class so let's
go to utils class and let's minimize all
the extra unnecessary method for now
down in here I'm going to say public for
the return type of this method I am
going to say boolean and we will see why
public boolean bad to already read let's
also receive a book and inside this
method let's say return already read
list already read books dot add and
let's pass our book so if you remember
from the Java section when we use this
add method on our ArrayList this add
method is going to return a boolean the
boolean is going to be the indicator
that if we have added the book
if you first down the control key or if
you are on a Mac system if you press
down the command key by clicking on this
ad you can see the documentation in here
you can see that if we successfully add
this element which is the generic data
type in Java at the end of this method
we are returning true it means that we
have added elements into our an
ArrayList successfully so in here we are
returning death boolean electron in our
book activity we are going to check for
this boolean and we are going to make
sure that we have added this block item
into our list successfully that's all we
need to do inside this ad to already
read and we can switch back to our book
activity so once again inside this
onclick method of this BTN add to
already read we are going to check that
if you teams target instance dot add to
already read with the incoming book if
that's true we are going to show a toast
message let's draw a book added but in
the else case I am going to show a
different toast message let's just say
something wrong happened try one more
also in the if case in case if we add
the book successfully I'm going to
navigate the user to another activity to
be precise I am going to create an
activity called already read books and
in that activity we are going to show
the list of books that the user has
already read but first of all I need to
create that activity in here I'm just
going to put a to do right now so let's
say navigate the user once again let's
review what we are doing inside this
method first of all we are getting there
already read books from this utils class
after that we are checking that if our
book exists inside this already read
books if it does we are disabling our
button but if it doesn't we are setting
an onclicklistener for this button in
which by clicking on that button we are
adding the book to already read books
ArrayList if that process goes well we
are going to show a toast message and
after that we are going to navigate the
user to already read book activity but
in case if something wrong happens we
are just going to show a toast message
this else case would never happen but I
just put it for the security reason we
don't want to gamble on anything that
can happen in our application ok by now
application and check if we can
successfully add a book to our already
read book ArrayList let's run the
application let's go to our book
activity by clicking on one of these
books and in here you can see that this
button is not disabled and that's
because this book does not exist in our
already read book ArrayList if we click
on this already read button you can see
that the book has been added to our
ArrayList successfully I said that after
this we need to navigate the user to
already read book activity but for now
let's go back to levels and once again
let's go to our book activity and let's
see if that button is still available
which is not it seems like we have wrote
the logic perfectly ok now let's quickly
create the already read book activity
and let's navigate the user there
but before that I'm just going to warn
you about something I don't usually
comment my coat and that's because I'm
lazy I had that son execuse but you
don't follow this behavior you put
comments wherever you feel necessary in
your code for example before the
declaration of this method we can put a
comment and in here we can specify the
purpose of this method for example I'm
going to say enable and disable button
besides that we are going to add the
using comments in your code might seem
some minor thing but believe me if you
come back to your code one month later
probably you don't remember any aspect
of your code and only thing that you
have that will help you to understand
your code are these comments also beside
that you may work with a team on a
project and these comments can be
so not only at the declaration of your
methods but wherever else you feel like
you need comments please do use comments
ok let's move on from this part and
let's create our already read book
activity so in my project I can right
click on the package and by saying new
activity empty activity we can create
our activity I'm going to name this one
to generate the layout fine so let's
leave this checkbox and I checked the
layout file for this activity is going
to be really simple basically we just
need a recycler view in that recycler
view we are going to show our books so
let's switch to our layout file and
let's drag a recycler view in our layout
for the vid some height I am going to
say match parent and also let's give it
an ID I'm going to say pork recycle of
you beside that let's add some margin in
our split view I believe that would be
better let's add a margin in here margin
I believe that NDP would be fine okay
now back in our Java file we can
initialize our recycle view for example
down in here I'm going to say recycler
view let's name it recycler view is
equal to find view by ID our that ID
that books recycle view I don't think we
had s let's remove that so that we don't
get a nullpointerexception after that
I'm going to create my adapter and here
is where the recycler view is coming
very handy we have created a book
recycler view adapter in our project
previously and the good thing is that we
can reuse our adapter as many times as
we want so once we have used that in our
all books activity we can also use it in
here for example I can say box recycler
view adapter let's name it adapter is
equal to new box recycler view adapter
we also needed a context let's pass this
let's say recycler view touch set
adapter and let's pass our adapter
beside that we can set a layout manager
for our recycler view I'm going to say
recycler view that set layout manager
once again I'm going to pass linear
layout manager let's pass our context
for the data I am going to say adapter
set box I'm going to pass newton's
target instance that get already read
this method in here also we are keep
seeing this highlighting here please
highlight says that you can also use
this get already red box method without
instantiating your UTS class let's do
that we can do this because this get
already red box method is a static
method so for example before getting an
instance I can say that get already red
box you can see that it's working
but there is a potential bug in here
right now the first place that we are
calling our greetings class is inside
the all Box activity I'm talking about
that UTS that get instance method so
before that we don't have any instance
of this utils class in our application
so before going to add all Box activity
we don't have any ArrayList called
already red box because if you remember
we are initializing our array lists
inside the constructor of this utils
class what happens if the user decides
to come to this already read books
activity first and don't go to the out
box activity I mean that hits this gate
already red box method is going to
return null and that's where the
potential bug can happen inside the
adapter of our recycle of view we are
calling some functions on our books
ArrayList and we know that calling a
function on a null reference can cause
null pointer exceptions what can we do
to avoid this now enter exception we can
call youtube's target instance inside
the main activity we don't meet any
functionalities from UTS class in main
activity but we are just going to do
that in order to avoid null pointer
exceptions and to initialize all of our
lists so let's go to main activity and
initializing all of our buttons I'm
going to say utils target instance this
way we are calling the constructor of
this utils class and we are initializing
our lists also now that we are here
let's quickly set an onclicklistener for
this beauty and already read I'm going
to say BTN already read that's at
onclicklistener me onclick listener and
the user to this activity to already
read books activity so let's create an
intent intent is equal to new intent for
the context let's pass main activity
that this and for the destination
already read book activity third class
after that we can say start activity and
we can pass our intent there is one more
place that we need to navigate the user
to already read books activity and
that's where we have added the book into
our already read books list let's copy
this intent and let's go to our book
activity if you remember we had a to-do
somewhere down in here in which we are
saying to navigate the user after this
to do I'm going to paste my intent but
of course I need to change this context
let's change it to this or because we
are inside this interface we need to
pass book activity that this now that we
are navigating the user we no longer
need these to do let's remove that and
let's run our application once again and
let's see if everything works perfect
first of all let's go to already read
books activity we can see that right now
it's empty it's also better to show
something in case if we don't have
anything in our list we will take care
of that in future videos but for now
let's go back and let's go to our book
you can see that this already red box
button is enabled you may be wondering
why this one is enabled because
previously we have added this book into
our already read books ArrayList well
that's because we are not persisting our
data we are not using any database or
anything we are just simply using a
static array lists because of that the
life of our data storage is only
whenever we close our application in
future videos in this section we will
use another option to persist our data
but for now we just lose our data
whenever we close our application
ok let's click on this already red pop
successfully navigating the user to
already read book activity if we go back
we are once again inside the book
activity but still this already red
button is enabled this is because of an
entirely new concept that's because of
the activity lifecycle whenever we add
this book to our already read book list
and we navigate the user to already read
book activity and from there when we
press this back button we are not
calling the oncreate method of our
activity we are calling some other
methods in which we are going to talk
about them in future videos in activity
and fragment section so we are not going
to talk about this problem right now but
if we press this back button once again
and if we go to our book activity you
can see that now it is already read and
button is disabled but the thing that we
are going to talk about in here is that
whenever we add a book to our already
read book I released and whenever we
navigate the user to and already read
book activity by pressing this back
button we are going to navigate the user
back to the main activity and not this
pop activity we are going to talk about
how we can fix that for example if I go
back and add this other book to my
already read book array list let's
quickly do that whenever we are
navigated to this already read book
activity by pressing this back button we
are going to navigate the user back to
main activity and not to the book
activity we will talk about that
later on let's also go back to main
activity and from there let's click on
this already read books once again you
can see that we have two books in our
already read books ArrayList there is
just one minor problem in here and that
we need to Center our recycler we will
fix that ok let's fix these problems one
by one first of all let's enter this
recyclerview and after that let's take
care of this back button so for this
recycler view I can go to my layout file
activity already read book and in here I
can change the width and height to wrap
content and after that I can constraint
my recycle review because we are inside
a constraint layout or I think this
button constraint wouldn't be necessary
okay it seems like fix this let's go to
our already read book activity that Java
fine and let's see how we can take care
of the black button for the back button
we have few options the easiest one is
to override some method called on back
pressed if you press ctrl o somewhere
inside your activity if you search for
on back pressed we can see this method
in here we are going to override this
method this method is going to be
triggered whenever the user click on the
back button so inside this method
instead of the super statement we can
simply create an intent and navigate the
user back to main activity let's see how
we can do that we can say intent intent
is equal to new intent let's pass this
as the context and main activity that
class as the destination after that
let's say start activity and let's pass
let's run the application and let's see
if we are going to navigate the user
successfully back to main activity let's
add one book to our already read books
are in list for example this one we are
navigating the user to already read book
activity if we press back you can see
that now we are inside the main activity
but there is one more problem in here if
you press this back button we will be
navigated back to already read books
activity and that's because of something
called back snack in programming so
whenever you are going to an activity
some snack will be added to your
application basically the hundred system
is going to keep a history of where the
user visits in your application whenever
you go to a new activity in you a stack
or to be personalized a new task will be
added to your back stack you can deal
with that back stack if you want for
example you can clear that back a stack
or whenever you are defining a new job
for example whenever you are firing an
intent we can say that this is an
entirely new task and you don't want to
visit in your application in order to
define your intent as a new task you can
add some flags to your intent for
example after defining your intent you
can say intent set flags this method in
here and you can pass two flags you can
say intent dot let's say clear task you
can see this flag activity clear tasks
in here and also I'm going to add one
more flag you can add one more by using
this pipeline let's say intent dot new
task you have this flag activity new
task so by using these two flags on your
intent you are saying that we want to
clear your back stack and also you are
defining this intent as a new task so
now if we run our application if we add
a book to our already read books
ArrayList for example this one now if I
press this back button I will be
navigated to the main activity but my
back slack is now clear it means that if
I press this back button we are going to
quit the application so this way by
using these flags and you can define a
new task ok let's move on from this part
and let's work on the other buttons in
our POC activity if you remember we had
three more buttons so once again I'm
going to go to the uncreate method after
getting the incoming book I'm going to
call another method called handle one to
once again I am going to pass my
incoming book the logic for this button
and the other two buttons are going to
be exactly the same so I'm going to call
two more methods in here hand our
currently reading book and handle
let's quickly create these methods
the logic from inside this method let's
copy them and we will change them if it
was necessary I'm going to copy this
whole method and I'm going to paste it
inside this handle one two reports we
need to change if you think first of all
we need to make this book final because
we are using it inside this onclick
listener after that I'm going to change
the name of this already read books
ArrayList I'm not sure if I talked about
this previously but basically whenever
you want to change the name of a
variable or some object in your
application that you are using in
multiple places you can press Shift + f6
and in here you can refactor the name of
your variable for example in here I am
going to save one to read books you can
see that when I change the name of this
array list it will be changed in all of
its usages so let's say I want to read
I also need to change this method in
here let's say get one to read books I
need to change the name of this boolean
after that I need to disable another
button beaten add to one to read this
onclicklistener for this with an ativan
to read so let's change that I also need
to create another method inside my UTS
class so that I add this book to my
wishlist so let's go to utils and then
in here let's create a similar method
you can see that the method is basically
the same we are returning a boolean once
again and we are adding the book into
our bond to read books ArrayList now
inside our book activity instead of this
add to already read book I can say add
to want to read there is just one more
thing that I need to take care in here
and that's navigating the user to want
to read books activity and not already
read book activity so let's quickly
create that other activity once again in
our package we can say new activity
the layout file is going to be exactly
the same so I'm going to fast forward
the process of adding the recyclerview
don't worry if you need to check the
source code I will upload the source
code at the end of every video but
basically these are and just copy and
pasting okay let's switch back to our
one to report activity and let's
initialize our recycling once again I
think I can copy some of the logic from
the already read web activity let's copy
but this time instead of this get
already read books I'm going to say get
one to read works this one let's also
take care of the back stack I'm going to
copy this whole method from the already
read pot activity you may be wondering
why I am copying and pasting why I have
created four different lists later on we
will see some minor differences between
these four different lists okay now that
we have created this activity let's
navigate the user to want to read
activity from inside our POC activity
that all we need to do inside this
handle want to read box method let's
quickly create the other tool in order
to save your time because it files just
copy and pasting I cut a part that I
wrote these two methods but let's
quickly take a look at them so first of
all this handle favorite books you can
see that the book is final we are
getting the ArrayList we are checking
that this book exists in our favorite
books after that if it does exist we are
going to deactivate our button but if it
onclicklistener if adding the book into
our favorites ArrayList is successful we
are showing a toast message and we are
navigating the user to favorite activity
I have ensured that in a minute but in
case if something wrong happens we are
sharing a toast message just like we did
for already read books beside that and
the same thing happens inside this
handle currently reading book you can
see that basically the logic is the same
we just changed the name also beside
that I've added two more methods inside
my utils class this add two currently
reading book and add to favorite once
again you can see that we are returning
a boolean beside that I've created two
new activities this currently reading
activity which once again we are
initializing every circle view and it's
adapter and we are setting the data in
our adapter by using utils that that
method that we have in our utils class
we are also taking care of this unpack
first button beside that we have created
this favorite activity which you can see
the code is a lot like each other
and also for the layout fonts for these
two activities if you take a look at
them I only have a recyclerview in each
one of them I just caught the process to
save your time but if you want to check
the source code please do that I will
upload the source code at the end of
every video in this section on the links
that you can see on the screen so feel
free to check that if you need but in
here let's quickly run our application
and let's see if everything works fine
let's go to our block activity and let's
add this book to one of our array lists
for example let's add this one to our
favorites the book has been added we are
navigating to the favorite activity if
you press this back button we are
navigating back to the main activity
let's see our favorites from here
but I didn't set down quick listeners
for these buttons I will do that let's
see our books once again and let's add
this 1q84 to our I don't know currently
reading books it seems like we have
okay I think that's enough for this
video in the next video we are going to
work on showing our card views I'm
talking about this book card view
differently in different activities so
for example inside this all books
activity we can see this card view we
can expand that in different lists for
example inside the favorite books list
we are going to add some actions for
example two text views so that by
clicking on them we remove the book from
the list we are going to show that for
the books that are inside our lists but
we are not going to show that inside all
books activity and at the same time we
are going to use only one adapter so
that's one thing that we are going to
inside that if you had time and we will
work on this about in here basically we
are going to show some dialog message
and we will see how we can show a dialog
okay see you in the next video
in this video we are going to create a
delete button in our current view
English will be available in all
activities beside the old books activity
let's see how we can implement that
functionality I'm going to add my delete
button inside this expanded version
inside the second relativelayout inside
the list item pop that XML file so if I
expand this relative layout and change
its visibility to visible for now I'm
going to add the delete button somewhere
beside this up arrow I'm going to place
it in here so for that down in here
after my image view after this bitten
off arrow I am going to say text view
wrap content and wrap content for the
width and height for the text I'm going
to say delete let's move it to below our
short description let's say layout below
and let's address our txt short
description after that I'm going to move
it to the left of this up arrow button
so let's say to start off I DBT and our
pyro let's quickly add a margin let's
say margin end and I believe 70 P would
be fine let's also change its color to a
red color let's quickly define a color
in our color resources inside this color
resources let's copy this one and let's
create a new one I'm going to change its
color to red and for its value and let's
select one of these two I'm going to
select this one but I think this one is
a pink color so let's change the color
to pink next in our list item book that
XML file in our text view I'm going to
say text color and I'm going to change
it to my pink color let's also give it
an ID I'm going to say BT and delete
beside that let's change its visibility
to argon later on in our adapter we will
change this visibility if it was
necessary okay we are almost done with
our card view let's just change the
visibility of this expanded view to gone
once again so in here I'm going to
change this one to gone and we are done
with our layout file let's go to our
adapter and let's pursue the code from
there so in our books recycle we are
if you remember inside on point
viewholder method we are checking that
if you should expand the card view or
not I'm talking about this if statement
if pops get that is expanded so inside
is if case I'm going to check that if
I'm inside all box activity or not if I
am inside the all box activity I'm not
going to show the delete button
but in other activities I am going to
show that for that I'm going to add one
more variable inside this adapter class
above in here after the context I am
going to add a string let's say private
a string I'm going to name this string
parent activity I'm going to receive
this string by the constructor so I'm
going to delete this constructor and
create it once again this time let's
select the context and this drink at the
time of initializing my recyclerview
adapter in different activities I'm
going to say that which activity is
calling this adapter for example if it's
the old books activity I'm going to pass
all work if it's others I am going to
pass the correspondent text after
receiving this string down in here
inside the unbound view holder method
I'm talking about this if case in here
I'm going to check that what that text
is so I'm going to say if parent
activity dot equals let's pass some text
in here for example let's say all books
it's better to use constants in here but
just for the sake of simplicity I'm
going to pass my text if the parent
activity that calling this adapter is
the all box activity we are going to
change the visibility of our delete text
to gone so let's say holder but I
believe we have it instantiated our
tillu text view in our view holder class
let's quickly do that so down in here
inside this view holder I'm going to
define my daily text so let's say
private text view let's name it VT and
delete and also after that inside the
constructor let's instantiate it for
example down in here I'm going to say
BTN delete is equal to item view dot
ordered ID dot retain delete now that we
have this element in our view holder
have access to it inside OnPoint
viewholder method so in our if statement
I am going to say holder dot BT and
let's icon let's write the other cases
for example let's say else if if our
parent activity is equal to already read
books if that's the case we are going to
delete this book from the already read
ArrayList instead of this if statement
you can also create a switch statement
first of all I'm going to write the
whole if statement and after that we
will write the logic for each case let's
say else if and activity that equals
let's say one to read the other case
would be as if and activity is equal to
currently reading we have one more case
let's say else the else case would be in
a favorite activity now let's write the
logic for each one of these cases for
example if it's the already read pop
onclicklistener for this bit intimate so
let's say holder that BTN delete dot set
on click listener let's say me on click
listener and inside this onclick method
I need to delete the poop from my
ArrayList once again I'm going to keep
all my interactions to my aesthetic
array lists inside the utils class so
I'm going to write another method inside
that with us class let's quickly go to
that class and down in here after
everything let's say public boolean once
again you can see that the return type
of this method is a boolean I will talk
let's say remove from already read let's
also receive a book and after that
let's return already read puts dot
remove and let's pass our book like the
add method on our ArrayList this remove
will also return a boolean if you want
you can check the documentation by
pressing down the control key and
clicking on this remove you can see the
guideline in case if we remove this item
from our array list successfully we are
returning true but otherwise we are
returning false we can use this boolean
remove the book successfully let's go
back to our adapter and in here let's
say if you teams that get instance to
add remove from already red book which
we need to pass our report by saying
boots target position if that was
successful we are going to show a toast
message let's say book removed but
instead of the book I'm going to pass
the name of the book which I can say box
that get position target name let's save
close removed also after that we need to
notify that the data set has been
changed because we want to refresh our
recyclerview so we can say notify data
set changed but in the else case let's
just show a toast message let's say
something wrong happened clear sorry
again we have write the logic for
removing a book in case if the planet
activity is already read but now that we
have changed the constructor of this
adapter probably we are going to see
some errors in our application wherever
we have instantiated this adapter we are
going to see some read errors for
example if we go to our all books
activity you can see that in here we
have an error and that's because now our
constructor needs two elements the
context and also the parent activity
which is a string for example inside
this all works activity I can say all
works let's see other places that we
have instantiated our adapter for
example already read books activity
notice that these strings should be
exactly the strings that you have passed
inside your if cases okay let's go to
currently reading activity and let's
pass one in here as well let's go to do
the same thing for the other two
ok let's close all of these extra files
and let's test our application and
that's if we can successfully to meet
the books from different activities
remember that we have written the case
only for this already read and the
others shouldn't work before I test my
application I'm going to add one more
line in here because it seems like I
have forgot something so in case if the
parent activity is already read activity
first of all we need to change the
visibility of our BTN delete I can say
holder that between delete let's say
that set visibility and let's pass
visible let's run our application let's
go to our all works activity in here if
we click on this down arrow we shouldn't
see this delete text let's try that as
you can see we cannot see any delete
that seems to be good let's go to our
activity and let's try adding this book
to our already read list it seems like
it has been added successfully if we
click on this drop down we can see that
this time we can see the delete button
let's try clicking on that it seems like
we are getting a crash let's open our
locket and let's see what the crash is
it seems like I've lost the error let's
run the application once again and let's
try everything one more time see all
boats put activity add to already read
list and let's try deleting it once
again it seems like the air is happening
online 78 of our recyclerview adapter
yes the problem is that first of all we
are removing this book from our list and
after that we are trying to get the book
from our list for that we are getting an
index out-of-band exception because this
book no longer exists in our books
ArrayList what I can do is that I can
save the name of book before this button
before this include listener or
alternatively in here I'm just going to
say port removed but still it is and in
here let's say broke removed let's try
running the application one more time
and let's see if you still see the
exception all box activity book activity
to already read list this time let's try
to leting this book it seems like we
have removed the book successfully also
we have refreshed our recycler
this empty screen is not a good user
experience we probably should show some
text message in case if we don't have
any book in our list we will fix that
later on in future videos but for now
let's go back I also don't like this
behavior that we are instantly removing
the book for example in here if we try
to add a book the user might press this
delete unwanted leaf for example by
accident before deleting the book we
also need to get some confirmation from
the user I think that creates a better
user experience so in here after
clicking on this delete button I'm going
to show a dialog message to the user and
I'm going to ask that if he or she is
sure of deleting this book let's see how
we can create a dialog so in here before
I'm going to delete this whole if
statement and here is how you can create
the simplest alert dialog you can say
alert dialog this one that comes from
and with x-dot app compat let's say that
builder let's name this builder is equal
to new other dialogue that builder this
builder needs a context in which I can't
ask my am context after that I can set
some options for this builder for
example I can set the text let's say
builder that set message this option and
in here we need to provide some message
for example let's say are you sure you
want to delete this book after that I
can also set some buttons for my other
Tyler for example I can say builder that
set positive button you can see that we
have negative button and positive button
for example for the positive button
that's a yes we also need an onclick
listener let's say new unclick the snap
this dialog interface that on click
listener and what would happen if the
user press on this yes and here is the
place that we need to remove the pot
from our ArrayList I can say if UTL
start get instance that remove from
already read let's also pass our book
groups that get with an index of
position if that was successful let's
show a toast message let's say Pope
after this toast message we also need to
notify that the data set has been
changed let's also set a negative button
for our dialog for example I can say
builder that set negative button let's
onclicklistener dialog interface that
inside this onclick method I am NOT
going to do anything because I want to
dismiss the dialog in case if the user
so we don't need to do anything we just
need to override this one click method
let's finish setting a negative button
after creating your builder after
setting the message the positive and
negative button you can create and show
your builder for example you can say
builder dot create dot show let's test
the application and let's see if
everything works fine let's go to our
all Box activity let's add a book to our
already read list and let's try removing
this book so if I press on this delete
you can see that we are seeing this
alert dialog are you sure you want to
delete 1q84 let's say no as you can see
even though we didn't write any code
inside this on click method we have
dismissed the dialog successfully let's
press the delete once again and let's
say yes this time as you can see the
book has been removed successfully that
alert Allah creates a much more better
user experience ok now that we have
rightt logic for already read books list
and let's quickly write for the others I
think I can copy and paste a lot of
these codes before copying and pasting
we need to go to our you to its class
and add some methods for example for
removing from different lists let's say
public boolean remove from one to read
let's also receive a book you can see
that the logic is exactly the same I'm
going to cut the process of writing the
logic for the other two lists you can
see that it's exactly the same ok now
let's go back to our adapter class and
let's copy the logic for T's already
read case I'm going to paste it for the
as you can see the code is exactly the
same we just changed the name of this
remove from favorites remove from
currently reading and remove from one to
read beside that I'm going to create an
onclicklistener for my different buttons
in main activity because I'm going to
navigate the user to each one of the
activities by clicking on the buttons
okay let's test our application let's go
to our all box activity let's add this
one to a currently reading list we are
navigating to the currently reading list
let's try removing this let's say delete
once again you can see that we are
seeing the dialogue let's say no this
time let's say yes it seems like the
book has been removed successfully if we
go back and if we check out currently
reading books you can see that the list
is a still empty let's quickly try for
others one to read added successfully
delete sorry I clicked on the current
view and not on the delete button let's
go back and let's try deleting it yes it
has been deleted successfully see all
books let's add it to our favorites
let's try to looking it let's say yes it
seems like we have an error when we try
to delete a book from our favorite lists
if we open our locket you can see that
something wrong is happening inside our
UTS class at 9 116 and yes this is the
error in here instead of removing the
I have recalled this method sorry about
that let's say favorites don't remove
let's try it once again let's add a book
to our favorites list and let's try
removing it let's say yes it seems like
it has been deleted successfully okay I
think that's enough for this video in
the next video we are going to see how
we can show a website in Android
applications ok see you in the next
in this video we are going to work on
this about button by clicking on this
button we are going to show some
dialogue in which inside that dialogue
we are going to show some detail about
the developer of this application and
also the application itself beside that
we are going to give the offer to check
our website by clicking on a button the
user will be navigated to our website
let's quickly see how we can implement
that for showing the dialog like the
previous video we can use the alert
dialog we are going to use that in here
but I also need to say that you have
other options as well for example you
can create your own customized dialog we
will talk about customized dialogue
later on in the course when we talk
about fragments but in here we are just
going to show a simple alert dialog okay
let's go to main activity and let's
implement that functionality that I was
just talking about so down in here
inside on create method I can say
let's say neon click listener we need to
create our alert dialog that builder
object in here once again I'm using the
Android X artifacts so import the right
one let's say other dialog dot builder
let's name it builder is equal to neon a
dialog that builder you know that we
need to pass a context in here because
we are inside the main activity inside
this interface we can pass main activity
after that we can set a title for our
other dialogue builder we can say
builder dot set title I believe is the
name of the method let's pass the name
of our application I can say get the
strings let's say our dot string dot add
name after that I can say builder touch
set message and let's provide a message
that's going to be our text and after
that we are going to set two buttons for
our dialog so let's say builder that's
at negative button for the negative
button I am going to say dismiss and
after that we need an onclick listener
so I have a toy point here let's say
dialog interface that include listener
once again inside this onclick method we
are not going to do anything because we
are going to dismiss the dialog we just
implement this onclick method after that
let's set a positive button for our
dialog let's say builder that said
positive button let's say visit neon
click listener and by clicking on this
visit text we are going to navigate the
user to our website for now let's just
add it to do in here we will do that
later on after creating the dialog we
can show it we can say Bueller that
creates that job also before I run my
application I am going to say that you
have all sorts of functionalities with
this builder for example if you take a
look at the available methods you can
see that you have all sorts of options
for example you can set an icon for your
dialog beside that you can set that if
your dialogue is cancelable or not by
saying set cancelable let's say that
cancelable this one you can pass a
boolean if the dialogue is not
cancelable the user cannot dismiss the
dialog unless and he or she clicks on
one of our buttons I'm not going to talk
more about these functionalities in here
because when we talk about customized
dialogue later on in the course you can
functionalities ok let's run the
application and let's see if we can see
this toyline let's click on this about
button right now I can only see this
visit button and that's because I've
made a mistake in here both of my
unclick listeners are for this set
negative button I need to change this
one to set positive button sorry about
that let's run the application one more
time once again let's click on this
about button this time you can see both
of the options also if you don't get my
point about being cancelable cancelable
means that if you click on anywhere
outside of the dialog for example in
here your dialogue will be cancelled if
you set your dialogue to not to be
cancelable you cannot cancel it by
clicking on anywhere on the screen you
just need to click on the dismiss button
ok let's also try this dismiss in here
we can dismiss our dialogue successfully
and by clicking on this visit button
because right now we don't have any code
once again our dialogue should be
ok let's write the code to navigate the
user to our website in order to navigate
the user to my website I'm going to
create another activity in my package in
Java folder in my package let's say new
activity empty activity let's name this
activity website activity in the layout
file of this activity I am going to put
only one element and that element is
going to be a webview let's search for
webview in here you can see that we have
this webview option let's drag that and
let's change its bits and height to a
match parent let's also give it an ID
I'm going to simply call it webview
that's all we need in here let's switch
back to our Java file and in here let's
initialize our webview I'm going to
define my web view as a member variable
as a private member variable inside this
activity and you will see why I am doing
this let's say private that view let's
name it web view down in here inside on
create method let's initialize that
let's say webview is equal to find V by
ID or that ID dot webview after that we
can pass a URL to this web view for
example if we want to show Google we can
save web view dot load load URL and we
can pass our URL let's pass the URL of
google.com and from the positive button
of our dialog let's navigate to this
website activity in here I can say
intent intent is equal to new intent for
the context let's fast main activity
that this for the destination website
activity dot class and we don't meet any
flag in here let's just say start
activity and let's pass our intent let's
run the application and that's if we can
load Google successfully let's click on
this visit as you can see we are
navigating to another activity but we
are not seeing google.com and that's
because in the URL we need to say HTTPS
because Google has SSL certification so
let's quickly modify our URL in our
website activity instead of HTTP I'm
going to say HTTPS let's run the
let's click on this about let's try
as you can see another application has
been opened in this case we are opening
Chrome because Chrome is the default
browser in this emulator this is good
but you can do better you can open the
website in your own application and
don't send the user to another browser
in order to do that you need to set a
webview client on your webview let's
quickly see how we can do that so in
here I can save fu that's that webview
client this one and we need to pass a
new webview client near webview client
this time if we run our application we
should open the google.com in our
application and not in another browser
about visit you can see that this time
we are inside our own application you
can see the name of application above in
here let's try searching something in
here for example let's try searching for
Android if we try to go back one page
right now if you press on this back
button we are going to go back to the
main activity this is not a good user
experience you may want to navigate the
user one page back and not destroy the
whole activity if you want to do that
you can overwrite the on back rest
method in here by pressing ctrl or and
searching for on back press we have seen
this method in previous videos
let's overwrite that and we need to do
some modifications in here and here is
the reason why I've defined this webview
as a member variable because I want to
have access to this method inside both
on back press method and also on create
method so what I can do in here is that
I can say if webview dot can go back
this method you can see that the return
type is a boolean if our web you can go
back let's say webview dot go back but
in the else case we are going to do the
super statement so I'm going to cut this
one and I'm going to put it inside this
else case this in here means that
whenever we are pressing the back button
if our web you can go back one page then
go back but if not do whatever you are
doing with your back button for example
destroyed activity and go to your main
activity let's try this let's visit
and let's search for something for
example may code this time if we go back
you can see that we are going back one
page and we are not destroying the whole
activity there is also one more thing
that you can enable on your webview you
can enable JavaScript if you want on
your webview as well most modern web
sites use javascript and you can enable
it in here if you want you can save web
view dot get settings this one dot set
JavaScript enabled this method you can
pass true or false in here for example
let's pass true but you can see that as
soon as I pass through I'm getting a
warning the warning says that enabling
JavaScript is a security issue and it
can cause vulnerabilities to your
application so enable it carefully okay
now that we have enabled JavaScript
let's try running our application one
more time and let's see if we can see
any difference we shouldn't see any
difference about visit you can see
Google in here and you can search for
whatever you want when you are using
webview in your application you have all
sorts of functionalities in fact a lot
of applications are based on web view
they are basically some website and they
are showing some webview in their
application so if you want to dig into
that area make sure to learn webview
well you'll have all sorts of options
when you are working with webview but in
this course we are going to use a web
view this much and we are not going to
go further in here I'm going to make my
website activity reusable because I may
want to show different websites in my
application for that I'm going to get an
intent in here and I'm going to receive
the URL with that intent so that I can
show multiple websites so before
instantiating it let's get the intent
let's say intent is equal to get intent
after that let's make sure that our
intent is not null so let's say if not
is not equal to our intent let's receive
the URL let's say a string URL is equal
to intent that get a string extra this
method and let's pass a key for example
let's say URL let's move this logic to
then I'm going to pass this URL to this
load URL method so instead of this
hard-coded text I'm going to pass URL
next in different part of my application
when I create an intent and navigate the
user to this website activity I am going
to also provide a URL for example in
here inside this onclick method of our
dialog before starting the activity I'm
going to say intent dot set extra or put
extra was the name of the method let's
say URL for the key and let's pass URL
let's remove this to do from here as a
suggestion in your application in your
blog activity you may want to provide a
link for more information about your
book let's quickly go to activity block
dot XML for example somewhere in this
layout you may provide a link for your
book for that you also need to change
the model of your book and provide a URL
for that and navigate the user to some
website if you want to give the user
some more functionalities but we are not
going to do that in here together
because that's really simple and I
believe you can do it by yourself ok I
think that's enough for this video it
was a relatively short video in the next
video we are going to see how we can
define some animations for example right
now when we navigate between different
activities we only have a basic pop-up
animation in the next video we are going
to see how we can create a better
animation for navigating between
different activities see you in the next
talk about animations in Android and
also beside that we will talk about how
to enable the UP button to be honest
with you we are not going to focus on
animations that much yet in the course
and after this video you will know my
reason okay let's see how we can
implement a simple animation in our
application for navigating between
different activities in order to create
a simple animation file in our project
inside app folder inside the resources
folder we can create any resources
directory by clicking on this new
Android resource directory from this
resource type we can select any the
first option in our list let's keep the
name anning and let's create our
resource directory now if you take a
look at your resources you can see this
einem folder by right-clicking on that
you can create a new animation resource
file I'm going to name this file a
slide-in let's create our file you can
see that the root element right now is
this set basically set means a set of
different animations for example you may
have a horizontal animation beside that
you may want to have a rotation
animation you can include them all
inside this set but in here we are only
going to implement one animation and
because of that I'm going to change this
root element I'm going to change it to
translate whenever you want to have a
simple horizontal or vertical animation
you can use this translate in this
application we are going to slide our
activity to the next activity
so in here we need a horizontal
animation let's see some of the
attributes on this translate element the
first one that I'm going to talk about
in here is called from X Delta this one
you can see that similar to that we have
this from void lttle you can guess the
meaning of each one of these when we use
the first one from X Delta it means that
we are going to define the horizontal
place of our animation where do we want
to start our animation in a horizontal
axis so let's say from X Delta in here
you need to define the first horizontal
place of your application for example
you can pass 0 pixel this is just a
normal pixel and it's going to be
started from the beginning of your
screen besides 0 you can pass 0% which
from the beginning of your element from
the beginning of the beats of your
so this percent in here means relative
if you pass percent P you are going to
start your animation from the zero pixel
relative to the parent of your element
not to the Viets of the element so for
example if we have a button that we are
going to apply this animation on that
this zero percent P in here means that
we are going to start our animation from
the beginning of its parent layout you
can enter any number that you want in
here from 0 to 100 beside that you can
also enter negative numbers for example
in here I am going to say negative 100
the next attribute that we are going to
take a look at in here is called 2x
Delta once again you can see that we
have two attributes 2x Delta and two Y
Delta you probably can guess their
meaning 2x Delta means the destination
in the x axis and two Y Delta is the
destination in the y axis mean here we
are just going to slide our activity so
we are going to use this 2x Delta you
can put whatever number that you want in
here but I'm going to say 0% P beside
these two you can also define a duration
for your animation as well for example
in here we can say duration you can see
that it's not working you can type it
manually you can say Android column
duration that's is equal to and we need
to pass our duration in milliseconds
inside these double quotations for
example I want to set the duration of my
animation to 1000 milliseconds so to
have a quick review in here our
animation is going to start from the
negative 100 pixel relative to the
parent layout and we are going to end in
the zero percent relative to the parent
inside the x-axis and the whole
animation is going to take one second
let's quickly create another animation
as well once again by right-clicking on
our animation folder that's a new
animation resource file let's name this
I think we can change the root element
from here and smell let's change this
one to translate and let's create our
XML file for this translate element I'm
going to define the same attributes but
I'm going to change their values for
example I'm going to say from X Delta
this time I'm going to pass 0% P let's
say 2x Delta this time let's pass
negative 100% DP or 100% peace sorry
about that and let's also define the
duration and rate column duration once
again let's say 1000 milliseconds okay
now we have two different XML files for
our animations how do we implement them
in our activities here is how you can do
that for example let's go to our all
books activity inside the oncreate
method of this activity before
everything we can say overwrite pending
transition you can see that this is an
inner method inside every activity this
method requires two animation files the
first one is the inter animation and the
second one is the exit animation let's
provide them for the first one I'm going
to say our thoughts annum dot let's say
slide in for the second one I'm going to
say our that einem dot slide out let's
run our application and let's see what
our animation looks like if we try to go
to all box activity you should see the
animation it's not that pretty but it is
what we created but in here if I try to
go back for example by pressing this
back button you can see that we are
still seeing the default animation if we
want to apply our own animation we can
do something like this in our activity
we can override a method by pressing
ctrl + o or alternatively by typing the
name of the method we can override that
method the name of this method is
finished so this method is going to be
called whenever we are going to finish
our activity let's call that after the
super statement once again we can use
this override pending transition let's
say overwrite pending transition this
time I am going to switch these two
animations for example for the first one
I'm going to pass a slide out and for
the second one I'm going to pass it
slightly so let's say our thoughts and
you dot slide out and for the second one
and if that slightly let's run the
application once again and let's go to
our old books activity this time if we
try this back button you can see that we
have some sort of animation it's not
perfect but it is what we have created
honestly I'm not a designer I am not
good at animating my applications but
probably you can do better than me just
play around with these numbers inside
your XML files and you will find
animations that you are looking for but
right now if we try to go to another
activity for example this already Redbox
activity you can see that we are still
seeing the default animation if you want
to apply your animation on this activity
as well once again inside this activity
you can overwrite the pending transition
or alternatively if you want to apply
the animation on your entire application
you can change the style let's quickly
see how we can change our style so
inside our values folder inside
styles.xml in here I am going to create
another animation sorry I'm going to
create another style let's say it style
I'm going to name my style custom
animation and for its parent I'm going
to say at Android column style forward
slash animation dot activity so this is
going to be our parent inside this style
file I'm going to define some items for
example for the first one I'm going to
name it activity open inter animation
this one and for its value I'm going to
pass my own animation I'm going to pass
this slightly I can say that any slide
in I need to create three more items the
next one is going to be activity open
exit animation this one this time for
its value I'm going to say add and slide
out let's also create the exit
animations item activity close inter
animation this one let's pass a slide
out once again at Panama slide out
another item activity close exit
animation this time let's pass our slide
in now that I have created this style I
can pass it as an item inside this app
theme style so in here inside this F
theme style I can say item for its name
I'm going to say Android column window
animation style this one and for its
value I'm going to address this file I'm
going to say add style forward slash
constant animation now that we have
changed our app theme style we can
delete that overwrite pending transition
method from inside our Albert's
activity so let's delete this one and
also beside that let's delete this whole
and finish method let's run our
application one more time and let's try
going to different activities for
example this currently reading books you
can see that we are seeing some sort of
animations let's go to this see your
favorites animation my emulator is low
on speed so for that you can see some
frames let's go to our all books once
again we can see the animation to our
book activity it seems like our
animation have been applied successfully
there are much more concepts and
elements when creating different
animations you can probably and design a
simple game with Android studio by just
using these animations but I'm not going
to talk about them in here the reason
that I'm not going to talk about them in
here is that because the Android team
are releasing a much better solution for
animations that solution is called
motion layout and right now it's in beta
we'll be released in a few monsters
probably in the spring of this year in
the Google i/o conference or later on
this year in Android developers
conference either way when it's released
on a stable Channel I will create one or
more videos about that I've seen and
I've heard a lot of good things about
that let me quickly show you some links
if you come to this web page at Android
developers official website you can see
some of the features of motion layout
together with the motion editor you have
all sorts of functionalities and all
sort of easiness when you are designing
your animations honestly I think that
they are drinking After Effects inside
Android studio so for that I'm not going
to talk about animations in here instead
I'm going to wait for the motion layout
motion layout is going to compete
constraint layout 2.0 and that
constraint layout is still in beta
phases we just need to wait a few more
months ok let's move on from animations
the next thing that I'm going to do
inside my application is that I'm going
to enable an UP button in my activities
for example inside this all works
activity but before I do that I'm going
to delete my animation because I don't
like it let's just comment this line and
let's close all of these extra points so
in order to activate your off button
inside on create method of your activity
you can say get action bar or to refer
circuit support action bar dot set
display home op enabled please option in
here if you pass through we will
activate your op button let's run the
application and let's see what does that
look like if we go to our all box
activity in here you can see this back
arrow which is our off button right now
it's not going to do anything because we
haven't defined the functionality but if
we want to navigate the user back to the
main activity we can do something like
this we can overwrite the on option item
selected method like this I'm talking
about this method you can see that we
are getting a menu item and we can use
that item in here we can create a switch
statement let's say switch on item dot
get item ID and let's create our cases
let's say in case it's Android that our
that ID at home which means that
you are going to click on our up button
this is the idea of that button we are
going to navigate the user back to the
main activity and here I have two
options I can create an intent and
navigate the user back to the main
activity or I can call the on backrest
method let's call that so this in here
means that do whatever you are doing
when the user clicks on the back button
basically the up button and the back
button in here are going to have the
same functionality but of course you can
do whatever you want you can navigate
the user to another activity let's also
add a break in here and let's create a
default case in the default case I am
NOT going to do anything I'm just going
to break out of the switch statement
let's run our application and that's if
it's going to work so if we go to our
all books activity if we click on this
up button you can see that we are
navigating back to the main activity I'm
not going to enable it in other
activities I just wanted to show you how
you can implement this out button okay
the next minor thing that I'm going to
do in here is that to change the name of
my application this title in here in
different activities for example I'm
going to change it inside these are
books activity to all books in order to
do that you can go to your manifest file
and in here for different activities you
can define different labels but I'm
going to create my labels inside the
string resources so inside my values
folder inside strings.xml I'm going to
define a few more strings the first one
is going to be named activity all books
and its value is going to be all books
after defining these strings you can go
back to your manifest file and inside
every activity tag you can set a label
attribute for example for this favorite
activity we can say label let's search
for favorite activity let's do the same
thing for the other activities let's run
our application and let's see if you
have changed the labels successfully in
the main activity we are still seeing
this my library which is the name of our
application let's go to our all books
activity you can see that the title has
been changed if we go to our book
activity you can see the pop details
let's go to already read activity once
again you can see the title has been
changed it seems like we have changed
the labels in all of our activities okay
I think that's enough for this video in
the next video we are going to work on
our utils class right now we are using a
static array lists for our data which is
not resisted over the life cycle of our
application in the next video we are
going to see my simple solution for
persisting data even if we restart our
device it is probably the last video of
this section of the course so make sure
to don't miss that ok see you in the
in this video we are going to solve our
problems with our data not being
persistent right now the problem is that
we are using static variables for our
data for example when they add one block
to one of these lists that book will be
added but when we close our application
these ArrayList will be cleared in this
video we are going to solve that we are
going to persist our data after an
application is closed and also for that
matter after the device is rebooted
usually in a real world application you
may want to use databases for situations
like this they have been designed
exactly for this problem and they have
been designed to persist our data but we
don't know anything about databases yet
later on in the course we will talk
about databases specifically we will
talk about SQLite database and also its
wrapper room database but we also have
another option that we are going to use
in here that option is called shared
preferences the purpose of shared
preferences is that to produce the data
that is not a structured they are being
void to use for the Preferences of the
user in your application for example if
the user decides to use the dark theme
you can save that preference in your
shared preferences but that's not all of
the purpose of the shared preferences as
I said you can save all of the on
structured data in shared preferences in
here in our application our data is a
structured we have a model called book
beside that we have all of these array
lists but we are going to manipulate our
data and we are going to save them
inside the shared preferences by
manipulate I mean we are going to
convert our Java classes and array lists
and after that we are going to save them
inside the shared references in another
word we are going to serialize our
objects and later on when we get our
data from the shared preferences we are
going to convert those strings to these
array lists and books that process is
called deserializing okay enough talking
shared preferences so if you remember
from the previous videos I have done all
of my interactions with Maya arraylists
inside this utils class and I've done
that because of this exact reason I'm
going to delete all of my array lists
and I am going to use shared references
so most of our changes are going to be
inside this utils class first of all I'm
going to define my shared preferences as
a member variable inside this utils
class for example down in here I'm going
to say private shared preferences you
can see that this is an interface that
comes from Android content package let's
name it shared preferences after that
I'm going to initialize it inside my
constructor inside these UTS before
everything let's say shared preferences
is equal to get shared preferences but
we cannot use this method because for
that we need a context for that reason
I'm going to receive a context via the
constructor of this class but notice
that and because I'm going to receive a
context in here I need to change a lot
of codes in my application wherever we
have used this constructor or for that
matter this instance and its method get
instance method we also need to pass a
context we will do that later on
so let's receive a context let's say
context and down in here let's say
shared preferences is equal to context
dot get shared preferences you can see
that in here we need a name this string
is going to be the key for our shared
preferences let's pass a name for
example let's say alternate divi divi S
stands for database after that after the
key you also need to pass a mod for
example the mode that I'm going to pass
in here is this mod private we have
other modes as well but if you want your
shared preferences to be specific to
your application only you can use this
mod private or to be precise context
that mod private now you have an
instance of your shared preferences you
can use it however you want you can put
some data into it or you can get
existing data from your shared
preferences but right now because I've
is an a constructor we should probably
see some error inside to get instance
method because if you remember in here
we are calling our constructor for that
inside this get instance method I'm
going to also receive a context and I'm
going to pass it to this constructor
that will fix our error for now next I'm
going to change this init data method in
here right now we are saving our books
inside these all books array list which
is a static array list but let's see how
we can use shared references so in here
first of all I'm going to define a new
array list let's say array list of books
let's call it books is equal to new
array list and after that I'm going to
add my books to this new ArrayList and
not to the static ArrayList so let's say
books that add and similarly for the
second book now let's see how we can add
these books I released into our shared
preferences so down in here I can say
editor this interface shared preferences
that editor let's name it editor is
equal to our shared preferences instance
dot edit you can see that this edit
method returns an editor instance after
that with the help of this editor we can
say editor dot for example put a stream
you can see that our options in here are
very limited we can only store primitive
datatypes plus the string but how do we
store our Java classes for example this
book class and similar to that our
ArrayList inside these shared
preferences I said that we are going to
convert our books I read list into a
string let's see how we can do that the
best option is to convert your Java
classes and array lists into a JSON file
if you don't know anything about JSON
don't worry we are going to talk about
that later on in the course specifically
we will talk about it in the networking
section basically JSON is a way of
passing data between different
godless of the language I mean here we
are not going to talk further about JSON
we are not going to work with JSON files
we are going to handle the job to a
third-party library so basically we are
going to serialize and deserialize our
objects with the help of another library
let's quickly implement that library
into our project the name of this
library is JSON if you search for JSON
you can see that the first thing from
github is the link of this library as
you can see above in here it has been
developed by Google down in here
somewhere we should see the dependencies
we are using Gradle not maven so we can
copy the dependencies from here let's
add it into our project inside build
that Gradle module app let's add it
somewhere and let's sink our project
after adding it in our routines class
first of all let's create an instance of
this JSON object we can say Giza you can
see that it's coming from countered who
built it Doosan package let's name it
JSON is equal to new JSON after that if
you want to convert an object to JSON
and you can say something like this you
can say geez on dot to JSON we have
multiple versions of this to JSON method
you can see that each one of these
versions receive different arguments for
example we can simply pass our books so
this way we can have a string let's
simply name it text is equal to json dot
to json so this way we can serialize our
books object with the help of this
reason we have converted our ArrayList
into a JSON file now let's see how we
can add this text into our shared
preferences I'm going to delete this one
I'm going to say editor that put the
string first of all we need the key I'm
going to use constants for different
keys and I'm going to define them above
in here as member variables let's say
private static find out the string
let's name it all box key let's
initialize it to all books after that
down in here inside this put a string
method we can pass that key let's say
all works key and after that we need our
value which I'm going to say jisan but
to JSON and let's pass our box object as
simple as that with the help of this
editor we can put this ArrayList into
our shared preferences but there is one
more step and we need to apply the
changes on this bed être for that we
have two options we can say editor dot
apply this method or the alternative
that we are going to use in here is dot
commit the difference between the apply
and commit is shown in here inside this
file and so whenever you use apply the
changes inside your shared preferences
will be applied in a background thread
and you will not block the main thread I
suggest that in most cases you use apply
because you don't want to block the user
from interacting with your application
but in here our data is really small and
we are not going to block our user
interface for long and beside that we
need the result of this applying
immediately for that I'm going to use
this comment later on we will see that
how we can use callbacks in order to
communicate between two different
threads and then we can use apply but in
here because we need the results
instantly and we haven't talked about
callbacks I'm just going to simply use
this comet method okay that's all that
we need to know in order to put some a
string into our shared preferences now
that we have successfully initialized
our data let's see how we can call this
in its data method so inside the
constructor if you remember in here we
are calling this in its data but before
that we are using these all books are
released in order to make sure that we
haven't defined outputs previously as I
said I'm going to delete all of these
array list so I shouldn't use these all
box ArrayList for that I'm going to
check my shared preferences and see if I
in the shared preferences or not if I
don't have any initial data I'm going to
call this in its data method but if I
don't I'm going to escape to the rest of
the code okay let's see how we can get
our data from our shared preferences the
right place to do that is inside the get
all box method let's quick answer for
that you can see that we have this
method in here and we are simply
returning all books ArrayList instead of
that I'm going to get the data from the
shared preferences let's say I really
stopped different book let's name it
books is equal to in here first of all
I'm going to get the text from the
shared preferences and after that with
the help of JSON library I'm going to
convert it to this books ArrayList so
before defining this ArrayList first of
all I need to create an instance of the
JSON so let's say juice on jihad is
equal to new JSON after that let's say I
really stuff different books is equal to
json dot from json this time we can see
that similar to to json we have these
from json as well in here first of all
we need the text itself which we are
going to get from shared preferences but
as you can see I'm not getting my shared
preferences and that's because when I've
defined my shared preferences I didn't
define it as a static variable and
because of that because we are trying to
have access to a non-static field if I'm
inside a static method we are getting
this morning let me type it completely
to see the error if you hover over these
shared references you can see that
non-static field cannot be referenced
from a static context for that I'm going
to delete this static keyboard from the
Declaration of this method we have used
it so far in order to have access to
this all books ArrayList but we no
longer need it so we can safely delete
it now that we have access to our shared
preferences I can say shared preferences
insightly's get a string method first of
all we need a key the key is going to be
our constant all books key this one and
after that we need a default value which
I am going to say not in this case so if
we did not get anything from the shared
preferences we are going to return null
as this string after that the next item
that we need an effort is from JSON
method is a type token the type token in
here defines that to what object we are
going to convert our JSON in this case
we are going to convert our JSON to an
array list of books so we need to create
a appropriate type so in here I can say
type you can see that it's an interface
let's name it type is equal to new type
token this one has the data type of this
type token inside the diamonds I'm going
to say array list of different books
after that we have a strange syntax in
here after the parentheses I'm going to
put a pair of curly braces after that a
dot and after that this gets type method
which you can see that will return a
type so let's use that this is fairly a
strange syntax and we haven't seen it so
you don't need to understand everything
in here we are just defining a new type
token you can copy and paste this one if
you need basically inside these type
token diamonds we are defining our
object kind and after that we are using
this dot get type method in order to
have a type after creating our type in
here inside this from json method we can
pass that now we are getting our books
from the shared preferences and we are
converting it to an ArrayList after that
we can safely return books instead of
all works so now that we have changed
this get out box method we can call it
from inside our constructor inside these
utils instead of using these all books
I'm going to say if null is equal to get
all books this method if you remember we
had a lot of non potentials inside to
get all box method let me quickly show
you one more time so for example if we
fail to retrieve this string from our
shared preferences we are going to get
known after that if our type doesn't
work we are going to get now which will
be produced by this JSON library so
there are a lot of potentially null
values so make sure to take care of them
whenever you call your methods so inside
this constructor I'm going to say if
null is equal to get all books I'm going
to call this in its data method let's
remove this line of code because it's no
longer necessary and if you want to have
a review inside this in its data method
we are initializing our data we are
creating these books ArrayList and we
are saving it inside the shared
preferences so the logic that we have
wrote so far means that if it's the
first time that the user is launching
our application if we don't have any
books saved inside our shared
preferences then call this in its title
method and save some ports but if it's
not the first time that the user is
launching our application just ignore
this if statement after initializing the
initialize these other array lists as
well because even if at the time of
installing our application these array
lists should be empty but we are using
them inside different activities and
based on them we are calling some
methods if these array lists are none we
are going to cause some exceptions for
that I am going to initialize all of
these with some empty array lists you
can see that basically inside these if
statements we are doing the same thing
for our aesthetic array lists this time
we are going to do it for our shared
preferences so I'm going to do the same
thing that I have done for my initial
data instead of these already read books
I'm going to use get already read books
if you remember we had such a method
down in here let's quickly find that we
have this get already read books
method and we are simply returning our
ArrayList this time let's see if we have
such a data in our shared references
once again I'm going to say jisan let's
say Tucson is equal to new JSON after
that let's define our type once again we
are using this interface which is coming
from the java packages so let's say type
once again let's name it type is equal
to I think I can copy some of the code
from inside this get out box method
let's copy everything we just need to
also we need to remove this a static
keyboard because we no longer need that
of course removing this static keyboard
is going to cause some problem in our
application for example inside already
read book activity in here without
initializing our utils we are trying to
call this method which was fine when we
have defined our method a static but
it's not going to work right now we will
fix this later on ok let's move on from
this part and let's go to our UTS class
inside this kit already read box method
when we are trying to get a string from
the shared preferences we need to put
another key in here let's quickly define
after defining these constants we can
pass them in different methods for
example for this gate already read books
method I can pass already read books and
constant now that we have changed this
gate already read books method inside
our constructor inside these utils when
we are going to initialize our already
read books we need to check that if it's
non or not let's say get already read
books to his method if it is not we are
going to initialize our shared
references because I am going to use my
editor in multiple places I'm going to
define that outside of these if
statements for example in here let's say
editor to be precise shared references
that editor let's name it editor is
equal to shared references that edit
then after that inside this if case we
are going to edit our shared references
I can say editor that put a string for
the key I am going to use my key from
above in here this one let's say I
already read books and for the values I
am going to pass an empty array list I'm
also going to use JSON libraries so
let's define our JSON above in here
let's say jisang is equal to a near json
after that inside this put a string
method let's say json dot to json and as
for the object I am going to say new
this way we are initializing our already
read books as an empty array list inside
our shared preferences we just need to
commit these changes we can say anything
commit let's do the same thing for the
other array lists as well for example
for this one to read books we need to
fix the get one to read books method if
you remember we had such a method down
in here let's copy everything from
inside this get already read box method
I'm going to copy everything and in here
let's paste them we just need to delete
this get a static keyboard and also
beside that use another key then after
that inside the constructor we need to
check that if this method is going to
return non or not let's say if not is
equal to get one to read books then we
are going to initialize our one to read
books ArrayList let's copy these two
line of code from here and let's paste
them in the second if case we just need
to change this constant similarly for
the currently reading book and favorite
books let's copy the logic from here and
paste them inside this gate currently
reading book method let's also delete
this a static keyboard and let's change
the constant let's do the same thing for
this gate favorite books method
delete the static keyboard and change
the constant let's minimize both of
these methods and let's go back to our
constructor let's say if null is equal
to get quietly reading books this one
let's copy these two line of code and
let's change the constant beside that in
here let's say if null is equal to get
favorite books once again let's paste
these two line of codes and also let's
okay now we have deal with our
constructor successful if we have
initialized all of our array lists let's
see what else do we have inside this
utils class we have fixed all of these
methods until down in here after this
get favorite books we also have this get
books by ID method let's quickly see
what will be having here so basically
once again we are using the all books
are released in order to get some pop by
its ID instead of that I'm going to get
the books from my shared preferences for
that I'm going to say array list of
books let's name it books is equal to
get all pubs I'm going to use my method
after that I'm going to check that if
these books are a list is null or not
let's say if null is not equal to books
then let's put our for loop inside that
if case and also let's iterate through
our books ArrayList that's all the
change we need to do inside this get put
by ID method now we are getting the book
from our shared preferences instead of
ArrayList after that we have these I had
two methods and is remove from method
let's quickly fix this as well inside
this method we are returning the result
of this dot add method which we have
called on our ArrayList this time I'm
going to connect my shared preferences
so I'm going to say negative stuff
different book let's name it books is
equal to let's say get already read
books if you remember above in here we
have fixed that method I'm talking about
this method in here and now instead of
using our ArrayList we are getting the
data from the shared references then
inside these two already read method we
are going to use that after that we need
to make sure that these books are a list
is not known let's say if not is not
equal to books after that let's say if
our books ArrayList that at
let's say book if the result of that is
then we are going to save these boats
are released into our shared preferences
as a reminder this add method will add
the book into our ArrayList and after
that we'll return through if something
wrong happens it will return false so
after this check in here inside this if
statement we have successfully added the
book into our list now we need to
convert this books ArrayList into a JSON
file and after that added to our shared
references for that I'm going to
instantiate address on in here and say
ji-sun is equal to new JSON after that
I'm going to instantiate my editor let's
name it editor is equal to shared
references that edit and after that I am
going to first of all remove the current
already read books and immediately after
that I'm going to put this new books
ArrayList so let's say editor dot remove
you can see that beside put a string and
get a string we have this remove as well
if you want to clear your shared
references you can use this clear method
which will remove all of the data inside
your shared references with the key that
you have provided above in here when you
have created your shared references I'm
talking about this key in here this
alternate TV it will clear all of the
data inside that shared preferences but
in here we are not going to clear all of
the data we are just going to clear our
already read books list and we are going
to update it with this new list so let's
use that remove method which requires a
key once again for the key I can pass my
constant already read books after that
let's say any third that put a string
for the key once again let's say already
read books and for the value let's say
Jason taught to JSON and let's pass our
books are released after everything we
just need to call that commit method if
we reach to this point we also need to
return true because as you can see the
return type of this method is abloom Ian
if we reach to this point it means that
we have successfully updated our share
preferences so we can safely return true
but in case if anything wrong happens in
this process we are going to return
false I'm going to do that after
everything let's say return false that's
all we need to do in order to add a new
book to our already read book list in
our shared preferences let's copy this
logic for the other methods for example
for this ad to want to read list let's
delete this return statement in here and
let's paste our logic we just need to
change this method let's say get one to
read books and after that we need to
change these constants as well once
again we want to read books ok let's do
the same thing for the other methods for
this currently reading let's change the
name of our method to get currently
reading books and after that let's
change our constant let's say currently
reading books let's minimize this method
and let's do the same thing for our last
method for this add to favorite method
let's say get favorite books and for the
constant let's say favorite books
okay I think that's enough for this
video this video is getting a little bit
long but we also have these remove
methods in which we are going to fix
beside that we have a lot of problems in
different part of our application
because first of all we have changed the
constructor of this you Tunes class and
also we have removed a static keyboard
from the Declaration of some of these
methods we will fix those problems in
the next video as well okay see you in
in the previous video we have rewrite at
this UTSA class up until these removed
methods in this video we are going to
change these remove methods in order to
get our data from the shared preferences
not static array lists beside that I
said that right now we have a lot of
problems in our application due to
changes in this utils class you can see
some red warnings above in here we are
going to fix them in this video as well
okay let's change this remove from
first of all let's delete this return
statement after that let's get already
read book let's say array list of
oops is the call to get already read
books after that I'm going to check that
if this array list is null or not let's
say if books or if not is not equal to
books if that's the case we are going to
remove the book from this list in here
you may think that you may write some
logic similar to these add methods for
example inside this add method after
checking that if our array is null or
not we are using this add method in
order to add our new book in here you
may want to do the same thing for
example you may want to say if books
taught remove the book that we are
receiving you may think that this is
I have tried that actually and the
reason for that is because this book
that you are passing to this method is
different from the book that is inside
your ArrayList the values are the same
for example the name IT description and
everything is the same for both of these
books but the reference is not for that
you cannot remove your book this way
instead I'm going to create a for each
loop let's say for pork let's name it be
inside our box ArrayList after that
inside the for loop let's say if B dot
ket PI D is equal to this incoming box
ID book target ID if that's the case
then we are going to remove this pot so
this way we are not checking the
reference of these two objects instead
we are checking their equality by their
ID so if that's the case we are going to
don't remove let's say be it's very
important to pass be in here and not
this incoming book or if you want to
have the boolean you can say if books
that remove let's pass P once again if
that's the case we are going to update
our shared preferences let's do that in
first of all we need an instance of our
JSON let's say is equal to new JSON
after that I can say editor let's name
it editor is equal to shared references
after that I can say editor dot removed
first of all we are going to remove this
already read book from our shared
preferences for that I need the key
that's math already read books after
that I'm going to put the new books
ArrayList let's say editor that put a
string or the key I'm going to say
already read books and for the value I'm
going to say json dot to json our books
are released after that lets say editor
dot commit in order to commit our
changes also the return type of this
method is a boolean so we need to pass
through in here after this editor that
commit if we reach to this point in our
code it means that we have successfully
removed a book from our shared
preferences so we can safely return true
but if anything wrong happens during
this process we need to return false
i'm going to do that after everything we
need to do the same thing for the other
three remove methods so let's copy all
of these and let's paste them inside
this remove from one to read we just
need to change the name of our method in
here to get one to read books and also
okay I think that's all the changes that
we need to do inside this utils class
that's where you can check everything we
have changed these remove methods we
have changed these add methods we also
have changed this get proper ID method
we change the gather methods for our
lists we also change this init data
method and after that we have changed
this constructor we no longer use these
array lists so I'm going to comment them
okay now let's fix our problems in our
application I'm going to check my
classes one by one so in the project
folder inside the package folder let's
check our classes one by one all books
activity first of all we have this error
in here we are using this get instance
if you remember this get instance now
requires a context which I'm going to
pass this you can see that we don't have
any much error inside this class let's
go to the next one I already read both
activity because we have removed a
static keyword from the Declaration of
this get already read books method I'm
going to fix that by calling to get
instance method let's say you tips that
get instance in which requires a context
in which I can pass this okay that's all
the fix that we need to do inside this
already read book activity let's go to
our book class because this is the model
and we haven't used anything from inside
the UTS class we don't need to change
this one inside the book activity we
have multiple errors you can click on
these errors from this left pane in here
in order to navigate to the errors for
example in here we need to pass a
context let's pass this let's check the
second one once again in here we need to
pass context the third error in here we
need to pass a context but because we
are inside this onclick listener we need
to pass group activity dot this instead
of this let's see others inside this
handle currently reading once again
that's passed this inside I click
listener of this between our two
currently reading I need to say book
in here we need to pass this also inside
this onclick listener of this BT an ad
to want to read let's say both activity
dot this that's probably all of the
changes that we need to do in our
application we just need to pass some
context to our get instance method let's
save this and let's check the last one
so let's say work activity that this I
think we have fixed all of the errors
inside this class let's check the others
inside the recyclerview adapter from the
onclicklistener of our positive button
of our alder toilet if you remember
inside this adapter we have received a
context where the constructor so I can
see damn context let's check others once
again let's pass in context in here as
well in context the last one that's here
let's say am context ok it seems like we
have fixed everything inside this
adapter let's switch to a currently
reading activity once again in here
because we have removed a static
keyboard first all we need to say get
instance and we need to pass our context
favorite activity let's say YouTube
start get instance and let's pass this
inside the main activity I believe we
have instantiated our routines let's
pass our context we have fixed the UTS
class we shouldn't see any error in here
let's go to one to read activity once
again we need to say gluten start get
instance and we need to pass this I
believe that was the last error in our
application let's also check the website
activity we don't need to change
anything inside this class ok now that
we have rewrite the logic for our
application let's test it and let's see
if everything works fine our application
is launching that's a good sign
let's go to our all books activity in
here we can see our books once again
that's a good sign let's see if we can
see the details of this book by clicking
on this button we are seeing them let's
also go to our book activity in here we
are seeing all of the details of our
book let's try adding this book to one
of our lists for example this one to
read it seems like we have added the
book successfully let's see if we can
see the details in here we can see them
we can see this delete water but here
comes the difference between using
aesthetic variables versus shared
preferences right now if we close our
application and run it once again we
should see this book still in our wish
let's close the application and let's
run it once again and let's see if we
can see this book in our wish list let's
run it once again let's go to our wish
list and as you can see we have our book
in our wish list now we have persisted
our data with the help of shared
preferences it seems to be great let's
also try to leting this book from our
I believe in here we should see a little
bug let's try deleting this book first
of all let's say no next time let's say
yes it seems like the book has been
removed successfully but we are seeing
the book inside our wish list I believe
the problem is that our recyclerview is
not refreshing if we go back to the main
activity and we open our wish list once
again you can see that the list is empty
but for some reason after deleting the
book the recyclerview does not refresh I
believe the reason for that is because
the process of deleting that book from
our recycler view takes too much time
and we have a trading issue in here if
you take a look at our recycler view
adapter this phonebooks recycler view
adapter you can see that after removing
the book from our lists immediately
after that we are calling these notify
data set changed we have seen the toast
message we have seen this book remove
text but it seems like this method in
here is not being called until this
point until calling this method we have
removed the book successfully from our
list but the process of updating the
process of making another connection to
the shared references inside for example
and I get favorites method takes too
much time and before that we have called
this method so we have called this
method before we get the new favorite
ports we can fix this we can create a
callback interface and create a
communication between this part of our
application between this on-click method
and for example favorite books activity
we can implement that callback interface
favorite books activities so that
whenever we remove some book inside this
on click method we update list of our
books but I'm not going to do that in
here because we haven't talked about
call back interfaces yet if you want as
a challenge for later videos you can
come back to this video and fix this
problem when we have talked about call
back interfaces I believe we will face
this problem a lot during the course and
we definitely will take a look at call
back interfaces and their usages in
situations like this so for that reason
I'm not going to talk about the solution
for that issue in this video let's
quickly check the other aspects of our
application for example let's add some
books to other lists let's try this book
let's say already read we have added the
book let's delete it let's say yes what
has been removed if we go back to main
activity and check our already read
books activity you can see that the list
is empty okay I think that's enough for
this video and for that matter for this
section of the course you have
successfully created your first
functional application as a reminder of
what we have done in this section I'm
going to say that we have practiced a
lot of our skill set beside that we have
learned how to show different dialogues
we have seen how to create a web view
and show our website beside that we have
seen how to create expandable record
views we have seen how to use aesthetic
beside that we have learned how to use
shared preferences we learned how to
enable this off button we learned how to
create animations for our activities we
also have learned how to change the
title of different activities I believe
overall this was a good section and I
hello everyone may Symes here this video
is about to reached its end but before
that I would like to say that it is
amazing that you have watched thus far
of the course make sure to have a little
party watching the entire course means
that you are serious about Android app
development because of that once again
I'm going to remind you that there is an
extended version of this course that you
can take there are more than 60 hours of
videos in that course and you will learn
everything that you need for a
successful career in Android app develop
by enrolling in the course you will have
lifetime access to all of the existing
videos and all of the new videos that
I'll add in the course you will have
access to the source code that I write
in the videos and you can ask your
questions directly from me and expect an
answer between hours above all you will
have 30 day money-back guarantee in case
if for any reason you changed your mind
click on the link in the description of
this video or go to Mako dot org for
more information about the course
remember that you can use free code camp
as the coupon code to get 20% discount I
hope you learned something in this
course take care and continue coding
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.