♫INTRO MUSIC PLAYING♪
Hey what's going on guys welcome back to another brand new Android Studio tutorial,
so in the last tutorial I have discussed about using an introduction to the navigation
drawer activity so as I have said before we are going to learn about the fragment
in under studio let's get started so we have an Android studio here so before
getting started I just want to say what is a fragment so fragment is something
that can able to make your app a transparent item in the in the last
tutorial I have said that in just a single activity we can able to switch
into multiple windows with the help of fragment activity by keeping a activity
a static one like if we are taking the example as the activity main or the
content main we will be keeping that containment as a static activity and
about that we are going to add a different kind of fragment layout files
along with the corresponding layer activity for that layered file so
likewise we are going to introduce fragment so the menus of the fragment is
in cage software the navigation drawer as we have discussed in the last
tutorial and not only in the navigation rover we also use the fragment in the
tab activity bottom navigation activity and many other places even the google
map and many of the things use the fragment activity so fragment if you if
you have a knowledge in what is a fragment and how we can able to use it
in android apps then you go to almost the basic idea of how android app is
working so in our daily life as I have discussed before in our daily life we
use many applications and most of the application really using fragments and
there and yeah today we are going to learn about it
so I know that fragment is a little bit confusing one so I'm going to show you
and explain it in one or two different tutorials so this is just a basic into
this fragment and just the introduction and the coming tutorial you will be
about how to use that fragment into this navigation drawer activity and switch
from one fragment to another using the navigation drawer items so first of all
in order to get data we how the blank activity which is a navigation rover
activity same as that from the last tutorial and we have the main activity
and many other layers files so the first thing is that we have to create a new
layout and a new Java class for a single fragment so and first of all I'm going
to create a new layer right click on the layout going to new creating layered
resource and I just want to give the name for this first fragment as fragment
oops ok fragment 1 maybe underscore one ok so now we have a layout file which is
named as fragment underscore 1 dot XML file so taking the text we have
everything same I'm going to arrange this so pressing the ctrl alt L now it's
a little bit arranger closing that and I try to create two or three fragments so
I'm going to create the second and third ok so now we have three different
fragments one is the fragment 1 2 3 ok so we have three of them so now we have
to create the corresponding activity file for each of these layered files so
first of all I'm going to create for this fragment underscore one so
right-click on the Java file there were folder right-click on that
going to new then go to the yellow class now we have to give a name I just want
to give the first class as fragment fragment one okay so fragment one and
we'll be discovering the same thing into fragment two and three so we have to
first of all we have to add all these lines recall for this fragment and the
lines record for just getting this layered file so now we got a public
class fragment one and what we have to do is that this is not just as you
activities so we have to introduce the fragment into this activity file so what
we have to do is simply add this extent then fragment so now this activity can
able to introduce layout from the fragments and they once that then all we
have to do is inside these collaborators we have to add the Alliance record to
get the wave files and the customizing edge so in the normal cases we have seen
one create as the method that introduced the setcontentview and it catches the
layered file so in this case of a fragment we have to use the one one
create view and one view created methods so for that all you have to do is
right-click between these two curly braces so I'm just right clicking over
here now over to the generate you can also get it by dispersing the out end
there and inside this small window you housing a lot of options and not know
that what off just the some options and from that all you have to do is go to
this override methods which can be opened by just pressing the control all
I'm just double clicking and show you this and all you have to do is from this
you have to find one create view one create just you can also search one
create so yeah here it is one create view and one view creator all we have to
do is keep by key pressing the control on the keyboard I'm going to select both
of these and clicking the okay and here we go now it automatically generated the
methods and all other things I'm going to remove this in this text I don't want
it because maybe you don't want it because I'm going to teach you all of
the things so now we have two different kind of methods one it's a one create
view and the second one is one whose view creator and inside that we have the
layout in flatter with a variable name as in flatter so the layout in flatter
is going to inflate that layer file into is this particular activity file so we
have view group then bundle and many things will be discussing the view group
and in the upcoming tutorials regarding this
fragment in this tutorial we are going to inflate the layout into this activity
and we are going to set up this layout so we already have created the layout
now we how to inflate that layout into this fragment so that you can able to
control the items in this layout using this fragment corresponding to that
layout file so all we have to do is you have to just remove some of the lines so
here you can see it returned super one create view there is no layout file
added so we have to remove this line in order to add the new line that catches
the layered file so we have to return then yeah we have to inflate that so in
flagged or in flat and the layout file about layout door fragment 1 ok so after
that we have to just add comma and as it is required by this one create view
methods of Hamid and container again comma and bundles every instance I just
leaving it as false and I will be discussing about this false and
container in the upcoming tutorials and finally closing that now yeah we got
that we how inflated the layout file which is fragment 1 and now here it is
all you have to do is simply copy the fragment one and replace just one with
two and three for the corresponding layer file so I'm just pressing ctrl
after selecting this fragment one layer activity file I'm going to press f5 on
the keyboard to copy that class you can also press ctrl-c and ctrl-v but I guess
that f5 is better so that you can able to change the name I'm going to add
there too now in this case we have to replace this with two so now we have the
three different fragments one is a fragment 3 fragment 2 and fragment 1
and that's it so in the next tutorial we are going to use this fragment into our
navigation drawer item and we want once that user press each of the item then
user can able to switch from one fragment to another so yeah also I'll be
also adding the link all these source codes in my website which is check 0
comm which is actually under construction so as soon as possible I
will be adding all the source codes in my website so that even able to easily
navigate it and they just download it if you just want and if you're such a lazy
person to write all this code but anyways..,
Thanks for watching,
I hope to see you all in my next video!
|| Peace Out ||
|| Visit Our Website ||
Không có nhận xét nào:
Đăng nhận xét