How to use Android View Binding like a pro | David Cruz Anaya | Skillshare

How to use Android View Binding like a pro

David Cruz Anaya, Android Developer & Tech Lead

How to use Android View Binding like a pro

David Cruz Anaya, Android Developer & Tech Lead

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
7 Lessons (52m)
    • 1. Intro

    • 2. Start view binding and use it in an activity (inflate)

    • 3. Use ViewBinding in a Fragment by using Inflate

    • 4. Use ViewBinding in a Fragment by using Bind

    • 5. Use View Binding in a Recycler View View Holder

    • 6. Pro tip: View Binding using it in abstract class

    • 7. Pro tip: View Binding Null Safety handling

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

In this comprehensive Android View Binding course  we will learn step by step on how to use Android View Binding like a pro.

  1. Use View Binding Inflate in an Activity.
  2. Use View Binding inflate in a Fragment.
  3. Use View Binding bind in a Fragment. (When view is already inflated)
  4. Use View Binding in a RecyclerView ViewHolder.
  5. Create a BaseFragment to reuse our code
  6. View Biding null safety handling

Minimum requirements:

  1. Basic knowledge in Kotlin,
  2. Basic understanding of an Activity lifecycle.
  3. Basic understanding of a Fragment lifecycle.

Please do not hesitate to ask any questions, doubts or issues. I am here to help you and I will reply a soon as possible. or re-record or extend specific videos if was not clear enough.

Thank you!
David Cruz

Meet Your Teacher

Teacher Profile Image

David Cruz Anaya

Android Developer & Tech Lead


Hello, I'm David Cruz. 

Welcome to my profile, let me introduce myself. I am a 37 years old mobile developer, passionate about new technologies, learning and teaching. 
I am currently an Android Tech Lead and also content creator for a small academy named LastReact.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Intro: Hello and welcome to this course about how to use Android Bu binding like a probe. In this course, we want to learn how to confuse or project to use Bu binding. Also, we will use Bu binding in an activity, in a fragment. And in Osaka view beholder, we will create a base fragment to reduce or overplayed code and also demonstrate how Bu binding new safety is working. For this course, there is no basically minimum requirements, but I will recommend you to know how to create an Android project. And also also to know and understand what is a lifecycle for an activity. And affirmed me. With that, you will be ready to go. In a class project. We will create what? We will create one small, small, angry project with one activity and two fragments. Among fragment, we wanna have only Ableton. And in another fragment we wanna have recycler view with aesthetic data or public API depends on what you like to use. Be binding in every single view. Please feel free to experiment as you like. These designs are very basic and are just for demonstration purposes and suggestions. I'm your instructor. My name is David 2s. I'm a software developer with more than ten years of experience. And for as an Android developer, I'm a tech lead, mentor and priming content creator in different learning platforms. You can find more info about me in www dot David Kruse dot code the UK. And now let's dive in into the course. 2. Start view binding and use it in an activity (inflate): Hi and welcome. In this first video, we want to see how we do include Bu binding in your project and how to use it in an activity. Okay, so let's go to the code and to start to use view binding in our project. The first thing we need to do is included in our build Gradle file. So we'll go to the build Gradle file and into the Android brackets. We can put it on the bottom here, just we need to include view binding. And you see, oh, non-capital case view binding. Put open brackets and we just enable it. Okay? So this should be enough to start to use beer bonding in your project. Now would just sink. Now, just ignore if you see this one the player just suggesting, etc, for this course, don't need it's just ignore it at the moment. Ok. So now, but we need to do is just go to their main activity. Okay? So we're going to start to use the binding for starting view binding. The first thing we need to do is to include it inside of our Set Content, setContentView. Okay. So here on the setContentView, roughly, just looking inside of the activity, we can see how it accepts our layout resource and also accepted view. We can actually see etc. video and audio parameters, et cetera. But we're going to use this one, this set view. Let's go back. So the first thing we need to do is the following one. here in kotlin, we can start. That is different implementation. You can use the whatever you know, fit for you for. I want to show you one which is it fits for me, which is the following one. So we created a prevalent letting it bar here, the word binding. Okay? And now after we included the worldwide binding, we need to see one thing, okay? If we open the resources and layout, we're going to see here in this specific layout, which is the same we're going to use here in the main activity. The main activity, which is activity made view binding when we actually include it. When we actually start to increase the, you know, from the build Gradle file key creates for us, for each liable source, he creates a binding class, which is the following one. Say it start to activity main. So the thing, what we need to do is just call activity main and with the word binding. Okay, just a little bit more clear. Let's create another Layout resource. So okay, so to be clear that specific point, okay, so we can include them now, another layout resource, let's create a fragment ... named fragment main. Ok. So now we have a fragment main. And so if we go here, instead of activity main binding, we can actually call fragment main binding, which is the exactly the, exactly the same name with the word binding into the Indian. But because we are actually activity. Let's use the activity, sorry, activity mind by now, after I create the activity_main binding, we need to inflate the activity_main binding. So we create binding equals. And then we put activity, activity mine binding. And we can see we have three different, three different methods. One is bind, another is inflate, which we just can include a LayoutInflater. And then we have this one which is you can include inflator, parent, view group and attach to the parent. Okay. This this is we're going to use for the pregnant, but the one we're going to use for the activities is going to be these inflate. So now we'll have to inflate and we can include the LayoutInflater. This line with the inflater is communist travel from activity. So as long as you extend from our compact activity or activity, this LayoutInflater is going to be huge room. You're right, LayoutInflater and is going to be there. So now after we create this binding, then here inside we just include this, but binding itself. It is not the layout, it is the class minded. So we can see it here. So you see the activity mind binding, the Wiener wanted activity_main binding. We want to the root of this one of the layout. So in this case, we use the root. Now, this one is the B1 is a constraint layout wise concerned layout is simple. It's a concern. This root is a concern Liao because they've activity main, the root. The Main View is a constraint layout. What happened if we change this one to, for example, a linear layout? Let's do it. Yeah, yeah, so now it's a linear layout, should be back to the activity and now press Command or Control in Windows, you can see now this one is a, is a linear layout, okay? So if you, as you can see, you don't need to build it again, it's automatically, automatically update. Let's come, let's go back to the activity. Let's roll back. Use the layout with the MSDS case, you know, use LinearLayout, etcetera. Ok. So now we have this and we have the beautiful inflated. We can run it, press play, and just need to wait until the emulator is going on. Why I create these privates, let any variable, the erosion I use it is because in a normal project you normally when you are going to bind something there. So that's why I put it put it outside. If you needed if you just need to create an inflate, then you can actually just include the insight of uncreate them. Sign. Okay? So now if you can say, OK, the LayoutInflater is works perfectly as before. If we go here to the main activity, you can see it actually there with the text Hello World is going inside. Okay? How are we going to call? How are we going to call to the, to the text activity inside politics activity, sorry, texts, TextView. Here if we include an ID number, text, view them on. Okay, so now we have an idea in the next video demo, what's happened is this binding actually includes binding. If we click, we can see TextView demo. Textview demo is exactly the same name if we press command or controlling windows and click here, you can see TextView demo. All right, so now this one we can actually put the text and hello demo. Hello text, we'll demo. I said nothing else. So now we stop it, just read it again. And it will actually run. Let's see it. How things going. And we can see actually is, is, you know, we get the binding with the TextView demo and we add the text Hello TextView demo, okay? And this is what you need to know to start to use bookbinding and include Bu binding into your activities. Okay, and the next, in the next video we're gonna see how to use it in a fragment. 3. Use ViewBinding in a Fragment by using Inflate: Now let's continue what we thought class. Now we know how to use be bundy in an activity, but how to use it in an africanus in Afrikaans. Let's have a look. Okay. So as you remember, European Oprah class will create these layout, which we create, we call it threatened me. And in this class we're going to use it. We have this with this main activity. But we can do right now is just create a new class, new cooking class, and let's call it main fragment. Alright, so see we have this get careless exchange that no preventable. So now we have a decent main fragment. You can extend the front fragment. Alright, so now we have this ferment and less inflate or fragment may own. As you know, to inflate, We need to we need to do it on onCreateView. Oncreate only eight bw. And then create view. We have these three parameters, inflator containers, instance bundle. The first thing we need to do is let's just remove this row at the moment. And here on the top, we can start with a private bar. Nobel is par. And we put underscoring the front binding. Where do we put the underscore? We put an underscore because we're going to use, we're going to use only this variable locally. You will see why binding and we extend. We make this specific variable from fragment, fragment for frack fragment main binding. Alright, so we're gonna make this variable notebook. And when we initialize with node, you will see why we did this way, why we do this one now. So now here in onCreate, what we can do is to bind it and we inflate or fragment, fragment by and we have this bind. This one is for the activity will see before. And then we have this insulator. So we put this inflator in plate. We pass the inflater. Inflater. As a second parameter, we pass the container. And as, as you know, we, the third parameter we're going to do, we don't want to attach it to the parent. So in this, in this one and this third parameter, we put false. Okay? We then one of them and include it in this case to the parent. So now we return bind dot. Ok. This one is no levels, so we put it in this way. Okay? So now we have this one inflate. But to make this one as a good practice. We want to, we want to instead instantiated here in these local, but we're going to use another private. In this case, we are going to put ball to not change it. And here on the ball, we use binding, binding. And here we're going to get whatever is on the binding. And we put the bang to make it nano level. Why we did this? Because we need we need to set this fragment mine binding. We need to set it to know at some point when actually the beer is destroyed. So because we can all make it, we cannot make it not abolish. We put as above. Then we're gonna use in this way. So on. So now in onCreate, sorry, sorry, this should not only destroy view, okay, in on, distributed on, on, before the Suffer, the Super, We can use binding. And we set a slow, oh, oh, say does not. So now what's going to happen here? When we, when we instantiate the fragment, my indicting, we're gonna instantiate it here. And when, when we want to use it, we're going to use it here to know if we set the bar. But it's going to happen is we can actually instantiate it two times. And that's gonna cost money issues. So in this case we're gonna use it to power. And this variable is constant, variable, it's going to return whatever she's on the binding. Okay, so and also because it's just not going to be known as long as, as long as we use it to, it's never going to be no song and putting the subband, so we don't need to include that exclamation mark all the time. So now here, as we say, we're going to use this one. We're going to instantiate it with the, with the bar. And we're going to use it with the bulb, with the constant. Here in this specific scenario, we just remove this and as you know, then there's no safe is redundant. Use it anymore. Alright, so now we have to, now we have this main fragment instantiated. Let's go now to the activity. We can go today. And here on the bottom, what we can do is just create a new fragment with mass spawning, can use this one. And here we can use dick. Alright. So we put similarly p because we're going to use the concern layout actually, let's do this. So we're gonna put a split. Let's put design. It's going to be even easier for us. So now we have this fragment here on top is put into the autumn, is fully to the top and then have disrupted hidden side. Okay, so now what we can do to us to make some tests, we can actually get this sorry one. Let's copy this one too, just to make it quicker. Here the Lisbon asks set id, okay, we can actually set us fragment inflict. Okay? So we're gonna use another fragment to you to see how as to how you can do it with bind. So now we'd inflate. Let's go to the fragment mind main, Sorry. And here inside we can put it in this way. Yes, include the declaration and hello, fragment. It. Alright, so now we can actually run it again. And where we're going to see is a below or where we can see before around activity. We're going to see the fragment with that text in the middle. So let's have a look. See. Oh, yes. Apology. With we need to do here is I forgot for this one, I'm really apologies. Name. And we can include a fragment solvent, inflict some insight. And now we can actually run it again, and this one should works. So now we're going to have this one and see, you can see this one is hello TextView demo, which is activity. And below here, these are air. We have these Hello fragment inflate. Okay, so now in the same way, how are we going to use it that, well, how are we going to use the text views? So here on this one, text demo. Okay, so now I have this one, okay, just leave it as it is. It's going to something he played it. So here on the activity or when we are going to use on nucleated. Ok. And when he is on bu creator, we are going to use finding this in this binding. We're gonna use text demo. Okay, so this one is the next demo, TextView demo, but from the pregnant now from the activity. And here, pass the text. And here the text, we can actually say hello, programmatic, program MAC, programmatic. All right, so now what is going to happen? Exactly the same thing, but we're going to use this text view and we're going to include, we're going to pass these tests instead of the TextView programmatically. Let's run it again. See it. It's running. And as you can see, this hello programmatic TextView is here. And that is all what you need to do to start to use you, buy into your fragments. We're going to see you in the next video how to use view binding, but when the mu is already inflated. So until next video. 4. Use ViewBinding in a Fragment by using Bind: Now we're gonna see how to use be binding in a Fragment when the view is already inflated. Okay? So in this fragment, we actually inflate the layout, the view we inflated here on a onCreateView. Okay, just for the demo purposes, let's create another fragment. In this case, we're going to create the list new layout. Let's call it fragment main. Dinah, for example, from the main container. All right, so now we have this family main container. Last. Let's copy the spirit inside to for demo purposes. And this Cole, Dave spiel. Cool. And not just include. It can create a new class, Katlyn class P, The New Class, which is going to call main container fragment. Alright. So now we have this main container fragment. You're gonna extend from fragment instantiated and we're going to inflate the BU before. So to infect the BU will include the layout here on top. And we put here this fragment main container. So now the view is going to be inflated. So we don't need to inflate it with the pyramid, but actually we're going to use me by name because it's already inflated. And now we're gonna see how to use it. So because we have the C, the previous statement, as you can see, we're going to use this one on Bureau created. The abuse are already being created. We don't need to use this onCreateView. So let's go here and we're gonna use on bu created. Alright. So now let's create, as we've seen before, is private bar binding. Instantiate fragment, fragment main container binding Street is from for us, for us. So whenever Enola ball we instantiated a snow. Cool, we're gonna say private PBL binding. Binding, get, sorry, get. Here. We're going to see binding and put the band to reality. Now here, if we're going to start to use them, we need to get the BU, unbind this BU binding inside of the beam. Okay? So in this case, we're going to use binding dot, sorry. Now binding, sorry. We're gonna fragment main container. And in this bind, knowing the inflate We use, the bind you see is accept of BW by default. So because except the beer, what we're gonna do is insert, dispute that insight. So that so now we have in this book binding exactly spine this dispute. Just for consistency, or we're going to use the onDestroy bu and he'll destroy bu. We're going to set this fine tune them. Cool. Okay, so now we can just use this in this binding, just dot TextView container as we declared before in an ID. So we have this TSV or container. And what are they? So we can now start the new and high fragment. Kleiner. Alright? So we need to fast text. We can include that any string it straight away to the view. So yeah, I have this beer container. All right. All right, so now we need to start over main container fragment. So this term containing fragment, we can go to the activity itself. We have this fragment. Let's create another fragment, fragment, much pair-end 0 dB. And now we can't go with the ID. And we're gonna kind of fragment bind document by. All right, so here as name, we will use the main container. Main container fragment. Below is main fragment, and on the top is main container fragment. Alright, so this one is red because we have no include jet, all, all the constraints. So this are the constraint right now. Go to the design. We can see this one on top here, here. And thus it, because we don't need to just above one. So now we have this wrapper on top, which is the bind, and on the bottom is the main fragment. Let's start back here. Yep. So now it's accessory. Yeah. So now we can see on top or texts how include high fragment container here. So what is the main difference? The main difference is if you're going to start a new fragment from scratch, okay? Then you can use, then you can use straight away. This one. You can inflate it inside as we did we do in the past when we actually do the inflated inflate. And you can use it this way. But if we're going to extend one fragment from another fragment and that first fragment, I already inflated view, then you cannot, you can actually inflate again, but you need to add it. And it's going to look very bad because one fragment is gonna look, looks on top and other family going to Luxembourg on practically is going to be the same. So in this specific scenario, if we want to just extend, we're going to use in another, in another freshman class. You're going to use the beans in this fragment. You just can't binding and said, you need to do nothing else. Okay? So this is x, this is practically everything. What bookbinding is going to offer us right now, we're going to start with the prototypes are going to teach you. So we want to see it for the next video. Actually, I'm going to say for this video, okay, so at the moment, this is the basics. Now you can use Bu binding on activities, on fragments you combine what are ready, the bill is inflated and practically politically, that's all right now in the next video we're going to start to see the proteins. Okay? So in the next video. 5. Use View Binding in a Recycler View View Holder: Hi guys. In this video I will show you how to implement be binding inside of a recycler bu, bu holders to know what's much time. I already created the beholder and displays very, very basic list of TextViews. Alright? So we can actually run it just to see it. As you can see we have discussed on the custom view holder just inject, which is inflate, sorry, the layout here. And we chose an opposite, the beholder, we pass it the view everything as very basic as it is in the documentation basically. Ok. So I create this new activity, which is doesn't contain anything. You just pass it. You know, we use B1 in there just to present. And we have the, and the layout manager is a linear Lyudmila. Something very basic. Okay? So as we can see here, we pass it then items, okay, from 0 to nine, and everything looks fine. No problem, et cetera. The only, the only probably we can say is we don't have new binding and this is the objective of this course. So let's do it. We can actually coming back to the adapter. Inside of the data, we have this inner class, which is the beholder itself. Okay? So let's start with a refactor. Here. On this, we can actually see if we can inflate. We use the LayoutInflater would just get this copy and then we can remove all this part. Ok, cool. In this bu, we actually call it, let's call it bind. Interestingly consistent finding. And now we can actually see how it's called the name of the layout, which is item, custom item, cuz com binding, then you just play. And when you put inflate and we pass it inside, that's all we need to do. The binding, etcetera. So now we go to the view holder and the beholder actually expects a bill. There is no expects a new binding, okay? So we can actually come in here and pass the binding inside. Nights complains because here we can see on top expects to BU, but don't worry, we can actually pass the b often. So can actually put here this, this is E ten custom binding. And now here on the, on the recycled bu, bu holders expect appeal, no problem at all. Just call it binding. And we put bindings, sorry, windings, sorry. And now inside of band bending, we can call it, we'll call it binding answer. Yeah. Cool. So now we have this binding and now we pass the beer, which is, as we know, the root bump, Lassa. Now we have B binding inflated and we pass it the view. We pass the view to the rosette, could be beholder, etcetera. So now let's start to use it. To use it. We just can't remove this part because we know we don't need anymore. And here on this TextView it's not exit anymore. We can actually use binding. Binding, we cannot access, we just need to provide it as private ball. That's all. Now we can access the binding and we can access to the text view and we put text. And that's all we need to do to use the binding and setup Bu holders. But how I look at this, we have this property list and we can actually stop it. And here on this item has position. We can put this item has, let's call it mu binding position, bu, binding position. Alright. So right now, let's wait a little bit until combines and executes. Now it's executes and we can see it here, but have a look at this before the Rose who was more wider. So what's, what's the problem now, you know, this is, this is not how we expected, which was looking before. So the, these are the, this is a problem, okay? And the program resides here. If you try to use only the inflater insight, insight of the recycler view inside of their second bu. Then it's going to look like this is not gonna get anything from the parent. Okay? So to fix this issue, what we need to do is coming back coming back here to where is the onCreate view holder and hearing country before that, when we pass the inflate, We know we can pass it the inflate and the parent parent. And after the panel and you're going to accept and another value which is false, which is attached to the attached Lebanon. Okay? So now this issue should be fixed, will just stop. It, will run it again. Can see or emulator. And now we can see it displays exactly the same as it was at displaying before. No problem. No issues. Alright. So please be aware if you have any problems with any parent with your fragments or do you have any problems with your layouts like this? The most probability you are passing the inflate only with the bass, the inflating only with an inflation of the layout. Ok. Do you need you need to attach the parent. You need to pass the parent. And you need to pass also, you know, if you want to attach the parent or not actually putting force through etc.. And that's all for this video. I hope you like it. In the next video we're going to see something, another protein, okay? But 6. Pro tip: View Binding using it in abstract class: Hi, in this video, I will show you how to reduce the boilerplate code when we use the winding. Okay, so as an example, we can see from the previous, from the previous demos this main fragment. Okay, so we have this main fragment and also we have this main container fragment. What is the issue here? So the issue is we need to, for every single fragment we want to have, we will have one primary tourist. Much issue. But because we want to use mu binding in big projects probably, okay, or daily basis, job, et cetera. So we need to write this one all the time. So we create them private bar, we instantiated when we create a new bar to use it, etc. So it's gonna be too much. And also we need to add this one here on the destroy view. So if we have one or two farming has, as I say, there's no money, no mutation. But if we want to use the one in a 100 fragments or something like this is going to be madness. So let's, let's see how we can reduce this one. Ok, so to reduce this, all this code and make it more accessible and more easy to read, et cetera. So I should just do one thing which is for me is works very well. So I will give it to you and you feel free to use it or not. So here on this main, we can actually create a new package. This package, I personally call it days. We call it as you like. In this base, we're going to create a new constraint graph. Club that cutting class. Yes, cooking class, and we'll call it base. Okay. So in this week's fragment, Sorry. So in this base fragment we have, we're going to pass a beholder, outsourced beholder BU binding. And okay, so we, we pass this one because every single, every single binding class understood it will create for us, extends from the winding. Okay, let's, let's have a look. So here in this one, so this activity, mine binding, we just press command or controlling windows we see is extended from new binding. And it will go to the main container exactly the same. You can see extended the binding. So what we do is we create a base class and we're going up except one parameter which is BU binding. And it can be any Bu binding. Okay, so let's go back to our base fragment. We put there'll be binding and this one extends from Fragment. Alright, so now we have all based fragment. And here we can actually start to do the same as we did in the past and the pattern and the previous demos. We can use the private bar binding. And you will have an extend for B because you're going to be any binding with past debt when we actually use it. Okay? So one thing is this class, as we discussed, always will be extended, never going to be used on his own. We're going to pass. We're gonna make it abstract. Ok? So we have this one here. This one is going to be nullable and said no. So now here we can actually, now this time the binding variable we're gonna use when I'm gonna make it private. Let's make it protected at the moment. So we can actually use in their class weeks and therefore so far as binding. And this one is going to get something. And this gets us money. We're going to pass binding. We put the bank to remove the no liability. So now we have this binding. Cool. So now on this one. So we can actually, as we lead within the main front men, for example, you have this onCreate BU, and we pass. And we created here. So oh, sorry. I think I lost yes, here on the ABO. So we have this onCreateView when bus inflator, container, et cetera. So at the moment, we can remove this. And now we want to have one very small issue, which is going to be the following one. We pushed the binding, but now we can use this one, use bad and then inflate because it's known to be accepted. Ok, so to fix this issue, we can create another abstract method function. I want to create new binding. And you will see why we need this, and here we return b. Ok, so now here this is abstract, abstract. Yet of course, pics, we'll actually remove this party. Cool. So now we created, we passed here, Create New binding, and then we return or binding. Okay, so now we have all based fragment and what it's going to solve this for what is there. So we go to the main fragment now, and then we see we have all this boilerplate code. So first of all, let's remove it. We don't need this one anymore, and we don't need this one anymore. And we don't need this one anymore. Ok. Oh, I forgot one thing. The thing I forgot is to use the owned as trivial as we did before. So we can pass binding and then pass it normal. Ok, so when we illustrate the BU, we said this was an old and couldn't use it anymore. So cool. Here on the main fragment we back here and instead of extend from Fragment, we're gonna extend from base fragment is the abstract class we actually created, created there, and then it accept one parameter, and this parameter is fragment main, main binding. Alright, so now it's complaints here and it's because we have one abstract classes before and then implement the members. And is this creates a binding. You see we pass B, but actually, because we pass it, we pass it here on top. The fragment might binding. This one becomes red, is accepted as a return value. The fragment from a main binded, sorry. Okay, so we can actually put code, okay? Oh, one thing I forgot. I forgot. So we can back here. Okay, so here on this, greet me by name. We want to need to prevent the error we saw before on the recycler view, okay, where everything is looks bad and it doesn't get nothing from the parent. So we want to pass here the inflator and container. Okay, so now we have an option out, enter in Windows, option enter here, and then we add parameters to the function, okay? Yeah, okay, so we can pass it both Missy here we pass both formatting. And now we can go back to our main fragment and we will see here, etc. So here. Now we're going to actually pass fragment buying inflate. And we're gonna pass the insulated container. Container. And then false. This one is so long, so there's out or option command and an elk performance all the class itself. Alright, so now we pass it here and we can see that the state is still detecting these binding, but this binding actually is the b. Okay? But what is this b? The b is the fragment mind binding we actually create here. So it's nothing else. And now to make it more reusable, we have this main container. This one was the example we saw before about, you know, when we actually have the latter, the bw already inflated and then we use binding. But for this example, we want to remove it. And we're going to do exactly the same base fragment. And here we're going to pass fragment main container binding. Okay? So here main-content binding, which is open, and here I actually want to implement the members. Let's implement the member. And we can see now you say extend its expect as a return value, a fragment main contaminant binding. No program. This equal. And then fragment container inflate. And then we pass here inflated container. False cooling. We have this value now. So we don't need this anymore. We don't need this anymore. And we don't need this anymore. So they say, how much code we need to read off. There is no need anymore all this code. So because everything is here, we don't need to, if we use this base class, we don't need to write this code again and again and again in every single fragment we're going to use. Okay. So now we can stop it. Because we were using before this or secretory activity less commented this part. Okay. And then commented part because we don't want to see now anymore the activity. I'm going to leave it there for you guys. But we don't need we don't need it anymore. So we're going to run it, make it put them later inside. And then everything is should look exactly the same as before. With older base fragment, you see will have the famine container. Let's just check it up. Say hi, Fleming container is here, okay? So we see it from a container. And then if we go to the activity, you know, we have this hello Text view. As we said before, Hello TextView Demo, exactly the same. And then container now is a fragment Hello programmatic TextView. And as you can see here, hello programmatic text field. So this is, So I hope you liked this one because for me is a safe a lot of time. I don't need to create this one all the time, et cetera. And also, you know, you can actually experiment with it. If you want to create another abstract class and pass it, you can actually create on here. So to create a secular, something, you can actually create nucleated. And b, sorry, on bu created. Here, you can create another abstract class, abstract function setup. So W's, alright. And this one is, and this one is going to be unit. We don't need anymore. This one you will, you can pass it inside here down. And then for all fragment is going to basically going to need this. But inside, if you are not going to usually do pass unit. Okay, and the exact same thing for all main fragment implement setup. And if you don't, I'm going to use it. You put unit, et cetera. And then if you, if you have there one include or another class, you can go in here. You know, you can come here and just pass the binding like a fat man. For example, this one is pass the, imagine we have this, this binding is there inside. We can actually put bind and all distinct recomputing inside. Okay? So yeah, experiment, you know, make it as visible as you can and it's gonna save you a lot of time and I hope you like it. Any questions, please let me leave it there and, you know, I would repair to you. Thank you so much. 7. Pro tip: View Binding Null Safety handling: Hi, one thing you need to know about the binding is be binding is actually new safety. Ok? So that means it can handle when one view is not the and when is it actually. So one specific cases scenario I found when I was working with new binding is the following one. So let's give an example. If we come back, you know, to or let's say main container, for example. The main container. We will see insight. Let's go to the main container, okay? In this, in this main Khan, in this main container, we can actually come here and add a new text text view. Let's, let's, let's copy, let's copy this TextView. There's Alice here on the top and say it's a TextView contain optional. No. Okay. So this is just gonna be, you know, this is a fragment. This op, the optional. Let's go to the spit. You can see it is on top, but the bottom to the top of Unless put the container. Okay. So let's see, we have this one and this remove the part. Alright. So we have these texts, optional, et cetera. And now to see, to see the no safety, we have this main fragment, main container. We're gonna create a new one here on the, on the, on the layout, which was going to create a new layout. And here in the new layout we're going to say it, you know, this layer detection noise less put layered orientation, which is going to be easier to test layout. Yet orientation is put here is going to be landscape always, okay? And this one is going to be fragment mane, mane on Dana. Cool. So now, so now we have to, now we have to list. Let's get this just to for consistency. This one as, as we can see, is a new one just for landscape. Let's go to the code. We can actually add it hidden side to the code. So now we can remove this option. Okay? So now we have, now we remove this optional only in the landscape. So here on this optional, let's get it. Let's go to the container itself. And now we have this text view container and now say binding dot container, not sorry, TextView Optional. Okay. Now have a look at this. One of them is TextView and is always optional. And other one is optional. Why this one is optional? It is optional because mu binding actually detects when one view it can be or it cannot be at some point. Okay, as, as we know, this TextView, text view container is on all the layout text view container, the container. So always this mu is going to be there. But this TextView optional is only when Eastern portrait not in landscape. Let's go back. Let's have a look. This text container is going, sorry. Next be optional. And now we need to add nullable text. And now this one, this is only on or tricked. Alright? So less stopping and run. Different. It's very nice to know because if for example, you are transferred euro, all your code from previous approach or butter knife, et cetera, from BU buying itself from butter knife to be buying them from synthetic to BU binding. In the, at the moment you catch it. At the moment you can see it is optional is because at some point in any of your layouts is, is not that, that view is not a soil can be when is maybe a tablet or this when it's in portrait and landscape, et cetera. So keep in mind if that is nullable is because at some point that you, that is specific mu is not going to be that. Now we can see we have this, this is only on portrait. And now let's rotate it. If we rotate it, it's gonna, it's gonna create a UI. Okay, so basically he's now go, no-go is very well. Ok, so this is only important. Let's put landscape. Okay. All right, but we can actually see it here on the, we can actually see here on the bottom, ok, it is not bad. Ok. So, so just to make it clear, less, let's make it here. Let's put a breakpoint so we can actually see it properly. Okay? Listen, the breakpoint, alright. So now we have the breakpoint is not. So now we have the breakpoint less rotated. But okay, so now we go to the variables. Let's get it here. Yep, so let's go to the variable if we can actually say the optional and the optional is going to be there is some material text view in this case, et cetera. And now we can actually play it, say, And now we can rotate it. And now we can see this TextView optional is absolutely o. So that's everything for this video. Okay. Please play around with the beam binding is very easy. Once you start to be binding is really easy to use. So please play around, etc. And if you have any questions, please leave it there for me and I will reply to you as quick as possible. Okay. Thank you so much, guys. Until the next one.