hey subscribers and Watchers what's up
this is whips from slide nerd here in
this video we are going to talk about
how to make dialogues and Android using
something called dialog fragment now
there are two ways of making dialogues
in Android one using the new data
fragment and two using the old dialect
class or alert dialogue builder classes
now the old method is something we'll be
discussing somewhere down the line after
maybe a few couple of videos but for now
let's focus on the new api's so first
let's talk about dialects in android you
always want to ask the user something
whether it's about some setting that he
wants to change or some information
needed so a dialog in Android is
composed of three parts part one which
is the area above which is also known as
the optional title here you identify the
name of the setting that the user is
about to change or you require some
action like maybe say erase USB storage
or something like that content area now
this is the second area where you can
have anything you want
sliders list checkboxes radio buttons
alerts and stuff and you can also have a
simple X view with some images over here
to display messages and ask the user of
something and step then the third part
is our buttons over here which are
either for dismissive actions or
affirmative actions which means you can
have negative buttons like cancel
positive ones like okay now make sure as
per the design patterns in Android that
the dismissive action is the first
button and the affirmative action is the
second button and one more thing to
remember is make sure that the user
clearly understands what you are trying
to say in the dialog like asking are you
sure don't do so this third question
over here is the most prominent one
where the user is clearly aware now yes
I have taken this from
developer.android.com will show an
example of how you should make proper
dialogues
so moving on how we construct the dialog
fragment define the layout or the
appearance create a class that extends
that I left fragment and three simply
use the on create view method inside
that class to link the appearance and
then we are done that's all we need to
do
how do we display the dialog fragment
now remember this is also two steps
creating is something and displaying the
dialog fragment is a separate step so
here what you got to do is create the
object of that class which you made that
extent the dialog fragment then simply
call the dot show and pass in the
fragment manager object along with a
name that you can use as part of your
transaction now you're probably
wondering here why haven't I started a
fragment transaction here for dialogue
fragments the fragments action is
managed inside the show method and that
is the reason why you're passing the
fragment manager object as the first
parameter to the show and one more thing
to remember is do not call commit when
you're calling show because inside this
show method the commit also takes place
without you having to specify it so do
not call commit and then when performing
several transactions like maybe you want
to add a you want to remove fragment B
and then somewhere you want to call show
dialog remember this sure dialog must be
the last call the reason because as we
discussed earlier show method also calls
commits internally so in other words
you're saying at a remove B show and
that is also committing these three
transactions on your back stack so with
that being said well we have discussed
whatever there is to know about dialog
fragments from a theoretical perspective
in the next video I'm gonna go into
Android studio and show you guys exactly
how to make a dialog fragment and get
this running in the meantime if you guys
liked what you saw please like this
video let your let me know your thoughts
in the comment section below and
subscribe to our channel and also
support us in any way you can thank you
very much have a nice day