Fragment in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will be creating fragments but before proceeding with that first we need to understand what is fragment and what is the difference between fragment and activity so fragment is a part of an activity which represents a portion of UI on the screen in simple language there is one activity and inside that you can have multiple fragments it's kind of like a sub activity then the main difference between a fragment and an activity is that an activity represents a single screen while a fragment is a portion of an activity also fragment has its own life cycle as well so whenever an interviewer asks any question about fragment then always explain it with an example like you can say the same process happens at bottom navigation and navigation draw right only the fragment container is replaced with each other while bottom navigation and navigation drawer remains as it is so in this video we will be creating two buttons so when the user will click on the first button it will lead him to the first fragment and if the user will click on the second button it will lead him to Second fragment simple right this is a simple fragment project just to give you an idea of how fragment actually works also there are multiple ways to create fragment like with the help of navigation component or fragment transaction or maybe using a frame layout okay but for this video we will be going with a very common way that is using fragment transaction with fragment container view with that said let's create the project choose empty views activity then click on next name it as learn fragment and then click on finish let's do the prerequisites so go to colors.xml press Ctrl D to duplicate the line name the color as blue and hex code AS 1a90 F1 then next name the color as yellow and hex code AS ffd 230 now go to themes.xml uncomment the line and write the color as blue then duplicate the line and write the item name as color primary variant again duplicate the line and write Android status bar color and that's it now go to build.gradle app here we'll be adding view binding I have already explained it in my previous videos but still I'll explain it in short remember we used to write find view by ID in main activity to initialize the element right it's kind of increase the code length and also very boring to do it for each and every item right so instead we have a better solution for it that is using view binding so all you have to do is write view binding and inside that enable equals to true that's it then sync now so now you don't need to write find view by ID instead you just have to mention the element with binding I'll explain it in more detail in main activity okay now let's move on to activity main.xml as I previously said here we will be creating a fragment container view with two buttons okay so let's do it we will keep constraint layout as it is then give it a padding as 16 DP inside constraint layout we will create a fragment container View keep the width and height at 0 DP so we usually keep 0 DP when the element is dependent upon the constraint then give it an IDs fragment container in camel case then add all the necessary attributes if you haven't created button fragment one yet but we will create it now so keep the width as 160 DP and high Dash 60dp then the IDS button fragment 1 and add all the necessary attributes again I have not created button fragment 2 yet but we will create it now keep the width and height as same as previous button then give it an ideas button fragment too then add all the necessary attributes and it's done this is how it looks now of course we need to create fragments right so we will be creating two fragments for button one and button two okay so to create fragment right click on package name then new and then fragment choose blank fragment name it as fragment one and click finish similarly let me create fragment 2 as well and it's done now we don't have any operations to be performed on fragment right so we will just redesign it and remove all the unnecessary code okay so let me quickly do that foreign [Music] then as I said we will remove all the unnecessary code so go to fragment1.kt let me quickly do that see I have removed most of the code except on create view method okay because this is where your fragment is executed and also it's your view layout is present over here similarly let me do it for fragment2.kt as well and it's done now go to main activity this is where we will attach our fragments with their respective buttons so I have divided it in fourth step let me give you a quick overview so in first step we will declare all the variables then in the second step we will set The Binding then in the third step we will create a go to fragment method and then in the fourth step we will call the method inside the button on click listener simple right so let's do it step by step so step number one declare all the variables so let me quickly write it and then I'll explain you see we have used two variables first is fragment manager and second is binding so fragment manager is a class that is responsible for performing tasks on the fragment like replacing the fragment removing the fragment or maybe adding a new fragment all of this thing is handled by fragment manager got it then activity main binding so I am currently in which activity main activity right hence it will be activity main binding now consider that I am currently in login activity okay so it will be activity login binding you are getting my point right so it completely depends upon the activity or the activity in which you are students usually do mistake over here that they are in login activity and writing URLs activity main binding that is incorrect right now step number two set the root binding first we need to initialize the binding so write activity main binding dot inflate the layout inflator means it will fetch the layout then set content view inside that I'll write binding dot root means it will fetch the root layout got it root layout has every element present inside it okay now coming to step number three that is our main step here we will create a method which I'll name it as go to fragment so let me quickly write the code and then I'll explain you and it's done so we previously declare fragment manager right so here I have initialized it using support fragment manager so support fragment manager basically Returns the fragment manager then as I said fragment manager have different methods right so here we will be using replace method so entire syntax is divided in three parts so begin transaction starts a series of transactions so basically transaction is a set of changes like adding or removing or replacing that can be applied to fragments okay out of which we will be using replace method so it replaces an existing fragment with a new fragment in a specified fragment container which we created in activity main right the lastly commit method is called to apply the transaction so basically it commit the changes made during the transaction simple right now step number four and our final step that is calling the matter inside the on click listener so as I previously said we don't need to initialize element using find view by ID instead we will write binding dot button fragment 1 and see it automatically detects it then set on click listener on it and inside that I will call go to fragment method which we just created right so inside the bracket we need to mention the fragment that we want to navigate to so if the user clicks on the button fragment 1 then it will lead him to fragment 1 obviously right similarly we will write the code for button fragment 2 as well so over here if the user clicks on button fragment 2 then it will lead him to fragment 2 right and that's it now let's run the app so initially it will show you activity main itself you can set the initial fragment if you want to but now when I click on fragment 1 button and it navigates me to fragment 1. great then I click on fragment 2 button and it navigates me to fragment 2. perfect see both the buttons are at their original Place only the fragments are replacing each other cut it so yeah and that is it for the video if you are new to this Channel please consider subscribing to my channel and I'll see you in the next video [Music]
Info
Channel: Android Knowledge
Views: 4,859
Rating: undefined out of 5
Keywords: fragments in android studio, fragments in android studio java, fragment in android studio tutorial, fragments example in android studio, fragments in android studio tutorial, fragments in android tutorial, how to use fragment in android studio, fragment in android, android studio, fragments in android, fragments in android in hindi, how to implement fragments in android studio, what is fragment in android, fragments example in android, fragments in android app development, kotlin
Id: fT4MVQZWPRg
Channel Id: undefined
Length: 15min 36sec (936 seconds)
Published: Mon Jun 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.