Fragments Implementation using Kotlin || Fragments using Kotlin || Android Studio Tutorial || 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video fox android so in this video we're going to learn how to implement fragments using kotlin as the backhand language right so if you do not know about the basics of the fragments i already have a video on that you can just check out that video by clicking on this i button then you can continue with this video so that video gonna help you a lot so do check out that video so let me just show you what we're gonna get by the end of this video so if you can see on my mobile screen i have this activity opened and at the bottom of the activity i have two buttons fragment one and fragment two so if i just tap on this button you can see the fragment has been inflated to this activity right we haven't changed any activity but our fragment has dynamically been added to this activity now if i just tap on second button this fragment is replaced with the second fragment right so if i just again tap on fragment one we can switch between these fragments so this fragment concept is used in most of the modern apps like youtube instagram facebook right where we have one activity and we keep on changing the particular area with the fragments right so this is a really good thing to learn in android development so if you want to implement the same to your android application make sure you watch this video till the end so without much arrow let's get started [Music] so guys if you're new to this channel make sure to hit that subscribe button press the bell notification icon for saving the notification of the upcoming videos and by the end of the video if you like the video make sure to hit that thumbs up button as well so let's start with the development so first we're gonna make the front end part and we are going to alter the code of the main activity so i'll just change this layout to relative layout and here we're going to implement fragment container view we'll set the width as match parent and the height as 600db maybe and we'll give it id so if you can see this particular area will implement the fragments so we're going to change all the fragments inside this area right we have set the weight to the match parent and height to 600dp right now we are going to put two buttons at the bottom so let me just do that um okay as we are done with the main activity basically we have a container b and two buttons to change the fragments now we are going to make two fragments right so there are two methods of doing that the first one is you just right click on this app go to new then you can just go to this fragments tab and choose the fragment according to your requirement this is pretty much simple as we are creating a new activity this is just same as creating a new activity right so there is another method the second one is we are going to create a new layout resource file let me just name it as fragment 1 and change this to relative layout click ok so inside this you can implement anything like buttons recycler view something like that right okay uh as i've told you that you can implement anything inside this layout this this particular layout is for fragment one but as if now i'm just implementing a text view that that would differentiate between fragment one and fragment two and let us just set the background color so for that we are going to declare two colors here fragment one so let us take something greenish okay we have declared two colors these colors will be using as the background okay now let us just create second fragment quickly let me just copy this text view and set the background as fragment 2. so okay we have created two layout resource file for fragment one and fragment two so we are pretty much done with the front-end path now we'll just go to the back-end code so before that we'll go to build.gradle and here we will enable the view binding [Music] resisting the project so okay this thing has been completed now to implement the backend code for fragment one and two we are going to create two classes so we are going to create a curtain class name it as fragment1 right and we are going to create another class fragment 2 right so to implement the fragment this will implement the fragment class and inside the constructor will pass the layout resource file so for this fragment one and similarly for fragment two okay that is pretty much everything we need to do inside this fragment classes right now we'll just go to main activity so first thing we are going to do is the view binding thing okay we have implement the view binding concept now we are going to set the on click listener on both the buttons so fragment one button dot set on click listener and similarly we'll set the on click listener on button two now inside this button we are going to call a method replace fragment right let us just create this method also okay this will take a argument of type fragment right so here uh when the user taps on this first button we want to inflate this fragment right fragment one so we'll pass the object of the same fragment one and similarly when the user taps on second button we want in plate second fragment right so we have called this method and pass this fragments now inside this method we're going to create an instance of fragment manager and then fragment transaction so guys what we have done here is we have just replaced the fragment container with the fragment right so for example you can see this is the fragment container we want to replace it with this first fragment or second fragment depends on which button user taps on right so if user taps on the first button will replace it with fragment one and similarly if user taps on second button will replace it with fragment two then we are going to call this fragment transaction dot commit you can just consider this transaction as a normal transaction for example in a banking transaction right we have just began the transaction we have replaced it then we have called the commit method right so that is pretty much everything to implement these fragments now let me just quickly install this app to my cell phone and check if it is working fine or not right so okay guys the app has been installed to my cell phone as you can see on my mobile screen if i just tap on this fragment one we have replaced the container view with fragment one if i just tap on fragment two we have got fragment two here so i guess we have successfully implemented this feature to our android application and i also hope that you guys have understood the code in case of any doubt you can always ask me in the comment section or you can dm me on instagram instagram use the name will be there on screen right now if you like the video make sure to hit that thumbs up button and in case you're new to this channel make sure to hit the subscribe button and press the bell notification icon for receiving the notification of the upcoming videos so that is it for today's video see another video bye [Music] you
Info
Channel: Foxandroid
Views: 4,555
Rating: 4.8076925 out of 5
Keywords: kotlin, fragments, android studio, android, development, fragment, tutorial, 2021, how to implement fragments in android studio, how to implement fragment in activity, fragment manager, fragment transaction, fragments in one video, android fragments tutorial, android fragment, fragments example in android studio, what is fragment in android, fragments in android studio kotlin, fragments kotlin, fragments using kotlin, foxandroid, fragments for beginners, fragments basics kotlin
Id: Q2HY58s9cHs
Channel Id: undefined
Length: 10min 27sec (627 seconds)
Published: Sat Jun 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.