Android ViewBinding in 8 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to new video in this video i'm going to give you an introduction to view binding or actually i will cover all of you binding because that's not very complex to implement in your app so why do we actually want to use view binding well recently we could use kotlin synthetics that is that we could just um write the view name here so for example tv hello world if you have a view that has that id then we could simply um use these views with this with with directly calling them by their id that worked but now it is actually deprecated or will be deprecated and it's also not contained or included in your android studio projects by default anymore and the problem with that approach with kotlin synthetics was that even though we could call these views immediately by their id all of these could potentially be null so we could access views that actually don't belong to the layout so for example we could access views here in main activity that actually are not contained in this are the layout activity main xml file and with view binding this can't happen so view binding basically ensures us that the views that we access in an activity in a fragment in a recycler view that these are non-null so there is no way a null pointer exception can happen there and there is actually also an alternative to view binding which is data binding the difference is just that view binding is the much simpler version of it and viewbinding will only bind um your views so you will be able to access your views and these are non-nullable data binding also binds your views but with data mining you can also bind data that actually belongs to these views so you could directly um from xml actually bind the the content of an added text to a variable in your activity without needing to set that explicitly in your activity but the disadvantage of data binding is that it requires annotation processing that means it will usually take much longer to build and if you really only want to bind your views using view binding then you're totally fine to do that the build times will be very quick and we don't even need a library for that so if you're interested in data binding then that is usually a little bit more to learn but there are also plenty of tutorials and documentations for that in the internet i will show you viewbinding here because cotton synthetics are deprecated and viewbinding is just the quickest way to reference your views now so first of all i'm in an empty android studio project here and we need to go into our build.gradle file module app because here we actually need to explicitly enable that we want to use viewbinding here in this project so here in this android block we will also open another block called build features and in here we simply set view binding to true so we don't write ubinding is equal to true just viewbinding true is enough here and then we can synchronize our gradle file so when that is done we can actually go to our activity main xml file and you can see android studio inserted that text view here and that is actually enough for testing purposes here we can go to the code tab and actually give this text view an id because view binding will of course only create references for views that actually have an id so that is actually the same with kotlin synthetics you somehow need to reference that view of course so i will give this an idea of tv hello world and that is actually enough now and just that you should that you see that these kotlin synthetics don't work anymore we can't reference tv hello world here it just doesn't know that but that is what we want to fix here so how does that actually work now how can we reference our text view here in this activity i will by the way also show you how you can use view binding with fragments but first of all here in an activity what viewbinding actually did for us is it generated a so-called binding class and with this binding class we can simply access our views so what we want to do is we want to have a private latent var here called binding and you can already see that is of type activity main binding that is the class that we running generated for us so basically for for every xml file in your project view binding will do that and since it doesn't require annotation processing you don't even need to rebuild your project so we will simply write binding of type activity main binding and now since we have that binding we of course need to initialize that and we want to do that before set content view so in here we will write binding is equal to activity main binding you can see we can either inflate something or we can call the the bind function here the bind function is used when we already inflated the view that we actually want to bind here to this binding and inflate is well when we want to inflate a new layout we want to inflate a new layout here so we call activity main binding.inflate and we simply need to pass our layout flatter here that already is included in an activity and now we don't want to set the content view to our layout.activitymain because then this is not set to this binding actually instead we will remove this and set it to binding dot root so basically to the root layout of our activity main xml and if we now write binding dot you can see now we have access to this tv hello world so all of all of your views will be contained in this binding here and that is that is the whole magic to view binding that is really not difficult here and you can also see if we press ctrl q here um this is non-nullable so that is a text view that is not nullable so there is no way with this call that there can be another point exception so that is actually the whole point about view binding so let's actually also see how that works with fragments for that i will simply duplicate that activity main xml file so copy paste call that fragment test whatever um i'm not going to include that fragment in our activity you just have to trust me here that this works and in here we again get a hello world text and that actually also has that id because we copied the layout then i'm going to create a fragment class call that fragment test select class let that inherit from fragment and this time on the fragment we actually want to pass the layout so r dot layout dot fragment test here so we actually let our fragment um inflate the layout and then we override on view created and of course first also reference the binding again so private latent var binding you can see now we have fragment test binding this time we of course want to choose that binding and here we don't have that set contentview function because that is only possible in an activity but you can see we get a view here so this time we can actually set the binding equal to fragment test binding.bind because that takes a view and our fragment already inflated the view so here we can simply pass that view and then we can again reference the tv hello world here from this fragment and that's how you can use view binding for fragments so i hope you liked this video if so please give it a like and comment below and if you're interested in more advanced android courses then you should really check out my website pl minuscoding.com there you will find premium courses and with the discount code philip 15 you will get 15 percent off of all my courses so really check it out you will learn even more than with my youtube tutorials and also make sure to subscribe to the channel of course if you haven't already see in the next video bye bye [Music] [Music] you
Info
Channel: Philipp Lackner
Views: 20,320
Rating: 4.9857779 out of 5
Keywords: viewbinding, tutorial, philipp, filipp, phillipp, philip, philip lack, lackener, view binding, android studio, kotlin, kotlin tutorial, lack, data binding, data, binding, android tutorial, programming, native android
Id: z0F2QTAKsWU
Channel Id: undefined
Length: 8min 59sec (539 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.