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

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 back-end 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 have 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 what a 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 a 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 gonna implement fragment container View will set the width as match parent and the height as 600 DB Maybe and we'll give it ID so if you can see this particular area will implement the fragments so we're gonna change all the fragments inside this area right we have set the width to the match parent and height to 600 DP right now we are going to put two buttons at the bottom so let me just quickly do that so guys we are done with the main activity basically we have a container p 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 one and change this to relative layout click ok so inside this you can Implement anything like buttons recycler view something like that right okay 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 1 and fragment two and let us just uh 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 backend code so before that we'll go to build.gradle and here we will enable The View binding did I just sync the project so okay is this thing has been completed now to implement the backend code for fragment 1 and 2 we are going to create two classes so we are going to create a cartoon class name it as fragment one right and we are going to create another class fragment two 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 2 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 implemented the view binding concept now we are going to set the on click listener on both the buttons so segment one button dot set on click listener and similarly we'll set you on click listener on button 2. now inside this button we are going to call a method replace fragment right and 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 these fragments now inside this method we're going to create an instance of fragment manager and then fragment transaction so okay is what we have done here is we have just replace the fragment container with the fragment right so for example we can see this is the fragment container we want to replace it with this first fragment or second fragment depends on which button the user Taps on right so if user Taps on the first button will replace it with fragment 1 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 begin 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 for 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 2 we have got fragment 2 here so I guess we have successfully implemented this feature to an 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 the Instagram username 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 press the Bell notification icon for saving the notification of the upcoming videos so that is it for today's video see another video bye foreign [Music]
Info
Channel: Foxandroid
Views: 8,615
Rating: undefined 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: h-NcxT697Nk
Channel Id: undefined
Length: 10min 27sec (627 seconds)
Published: Sun Jul 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.