‫Welcome back.
‫In this video, we'll create an application to understand the fragments in Android Open Android studio,
‫Select new project, Select Empty Views Activity, and I'll name it as Fragment app.
‫And to create a fragment, it's like creating an activity.
‫Select a new not activity select fragment.
‫And there are a bunch of different types of fragments.
‫I'll select the blank and also you can see in the gallery, if you select gallery, you can see these
‫types.
‫I'm interesting in fragment blank.
‫Next here, the fragment name, I name it as first fragment the fragment layout name fragment underscore
‫first.
‫This is for the layout file and the source language is Java for now.
‫Later on in this course, I'll use Kotlin finish.
‫And here we go.
‫Don't afraid from this code I'll delete them all in order to lecture it in detail in the next couple
‫of minutes.
‫A fragment like an activity has an XML layout file and a Java class that represents the fragment controller.
‫It's similar to the activity.
‫I'll design the layout of this fragment underscore.
‫First I go to the split mode and I'll make the background color for this layout.
‫I change the text view attributes, make it the size 48 and the text here I'll change it to first.
‫Also, you can define other views like the button.
‫Drag and drop it to here.
‫But we're not dealing with constraintlayout.
‫We're dealing with frame layout.
‫Frame layout is designed to block out an area on the screen to display a single item.
‫It's like a frame that can hold and display one thing on top of another, and this allows the child
‫views to overlap one another.
‫The child views are stacked in the order they are added in the frame layout with the last child added
‫appearing on the top.
‫You can see I added the button and it is overlapped and above the text view.
‫And the solution to prevent the overlapping is to add margins.
‫And here I'll add margin top 100 DPI.
‫You can see this is our first fragment and this is our bot.
‫Let's go to the first fragment here in the Java class and you can see it extends the fragment.
‫When you want to create a custom fragment, you typically inherit from the fragment class to define
‫your fragments, behavior, and appearance.
‫So by extending the fragment class, you are inheriting the basic functionality and life cycle management
‫provided by the Fragment class and the Fragment class includes a set of lifecycle methods that you can
‫override in your custom fragment to control what happens at different stages of the fragments existence.
‫And we talked about these callback methods in the previous lesson.
‫You can override on attach, you can override on start and other methods.
‫The most important method being used and override it in fragments is called Oncreate.
‫View fragments can define their own user interfaces using layout files, XML or by programmatically
‫creating UI elements.
‫But I prefer defining the UI elements using layout.
‫The Oncreate view method is often overridden to inflate the fragments layout or construct UI components
‫programmatically.
‫When a fragment is first attached to an activity or needs to display its UI, Android calls the Oncreate
‫view method.
‫If we look at the method signature, we can see there are three parameters one of type inflator.
‫The second is type view group and the third is of type bundle.
‫The first one the inflator.
‫It's an instance of layout inflator that can use to inflate a layout from an XML resource.
‫The container is the parent view that the fragments UI should be attached to.
‫This parameter is useful when you want to determine the layout parent, but you should not attach the
‫UI to this container explicitly.
‫Instead, the inflate method should be used to handle this job saved instance.
‫State a bundle that provides information about the previous state of the fragment which can be useful
‫for restoring the fragment state.
‫And since our fragment contains one text view and one button, I need to define them here on Create
‫View method.
‫You can get rid of these methods later on I'll add the others, but I want from you to focus with me
‫here the Oncreate view.
‫It's similar to Oncreate method.
‫You define the UI elements here.
‫I'll start by view instance equal to inflator dot inflate AR dot layout dot fragment.
‫First here I need to inflate.
‫I want to display the layout.
‫The second parameter is the container and the last parameter is false.
‫We talked about layout inflator and how to inflate layouts in the previous applications and the previous
‫section.
‫But as an overview, this line is a common pattern used in Android to inflate an XML layout resource
‫file into a view object.
‫Let's break down each part of this line to understand what it does inflator.
‫This is an instance of the layout inflator class.
‫It's a system service provided by Android that allows you to take an XML layout resource file and convert
‫it into a corresponding view object in memory Inflate method is a method provided by the layout inflator
‫class and it is used to create a view object from an XML layout resource or dot layout dot fragment.
‫First.
‫This is a reference to the XML layout resource file you want to inflate.
‫The container is the parameter representing the parent view group.
‫Usually a frame layout that will be a parent to the inflated layout.
‫It specifies where the inflated layout will be attached in the hierarchy.
‫The fragments UI will be added to this container.
‫False.
‫This is the final parameter.
‫Indicates whether the inflated layout should be attached to the container immediately.
‫When?
‫Set it to false, it means that you will manually add the inflated layout to the container, typically
‫by returning it from the Oncreate view method.
‫Now I'll get the reference for my widgets that I defined here.
‫A button and a text view button.
‫First button equals view dot find view by id.
‫I'm using this reference.
‫Here are.id dot button one frame.
‫Also I need to add an ID for this text view text drag one.
‫And as I said to you, I'll make it simple.
‫So I'll start by first button, dot set on click listener, new view on Click Listener.
‫And here I'll display a toast message that welcomes us to the first fragment.
‫And don't miss to return the view because we missed the return statement.
‫Return view.
‫So this is how we create our first fragment and how to use the most important method, which is the
‫Oncreate view.