FindViewByID - KotlinX Extensions or ViewBinding - ways to access your UI elements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i will show you the different ways to access your ui elements via code in kotlin and therefore i have prepared all three different ways that we will cover in this video because there were a couple of changes that were introduced in android studio 4.1.1 and those changes break a lot of code from my older examples also the ones that you saw on youtube but the ones are that you can see on my udemy courses as well so i wanted to give you a little update on what you can do with it and how you can fix this error so there are three different ways to access your ui elements so i have this activity main xml a very basic project here with a text view and i gave this text view an id of my tv standing for my text view okay and now if i want to access this text view and change whatever it says i can do so by using one of those three approaches so let's look at the first one so the first one will be to use find view by id then specify the type that we're looking at and in this case it's text view and i need to import the text view class here and then i have to define which id i'm talking about so here r dot id dot and then the name of the id in this case my tv which is this id here and this then allows me to use my tv so access my tv which is this text view and change its properties for example the text property to something like hello students so the default value that is defined inside of my xml file was hello world but now once i run the code it will say something like hello students okay so this worked and this is the old-fashioned way this works for java code this works for kotlin code so this has basically been around since the beginning of android app development even back then in 2008 and nine when it was first introduced okay so this has been around for a long time and it still works it's still fine but there was a new way which was even easier which basically made this whole line unnecessary so let's just comment it out and see if this is going to still work well unfortunately it doesn't work and why doesn't it work well it doesn't work because in android studio 4.1.1 and newer versions this line here does not exist anymore so in the latest kotlin version jetbrains deprecated this kotlin android extension compiler plugin which is this plugin here that was introduced this way okay so you need to add this line if you want this to work so let me add this line again and let's sync it and this is deprecated which doesn't mean that it doesn't work it just means that it is not best practice anymore so they have made this change okay so now in the main activity you can see that my tv is still red but i can make changes to it so i can now import the class in which it is used so you can see here android.kotlinx.android.synthetic.mainactivitymain my tv is that the one i want to use well in this case i can press alt enter and you and you can see it was imported for me so under synthetic i will find my id that i'm using so that's pretty cool because now i can just go ahead and use this i of course needed to add this line as well but i didn't do it manually the ide was smart enough to do it for me and i didn't even need to take care of any of the other stuff so i didn't need to create this line here where i said find view by id i just needed to go ahead and use my tv dot and then the property that i want to change and then basically change the property name so i can run this code and it will still work the same way and i can test it and it will be fine so let's look at it and we see hello students still appears as it did before now i have a little problem with this because i like this approach and i liked it because i used it in my courses so i used it in my youtube videos and i used it in my android master class so now the change has come and now i need to let you know so that's why i'm sharing this video with you where i'm showing you what is the best practice way 2021 and in the future let's hope this will stay the best practice way for a very long time so that i don't have to make another kind of these update videos so basically if you're using any of my older projects then you can just go ahead and add this line of code and it will work totally fine so all of your code will still work you can still follow along with the exact examples that i have given you now if you want to become a real pro and make the changes and view binding manually well you can do so but you can also still follow along with the approach that i've shown you in the past quick pause so you're learning something about android in this video and i hope you enjoy it if you want to learn everything that you need to know to become a real android developer then definitely check out my android master class because in this course you're going to build a bunch of great applications along your journey to becoming an android developer first you're going to learn the kotlin basics then you're going to learn to build one app after another and while you do that you get a bunch of demos which will really dig deep into the concepts as well as presentations which will help you to understand what you're learning so don't miss out and get the course right now you can find the link in the description below now let's look at view binding the new way of doing things okay so of accessing ui elements and changing their properties and therefore i'm going to comment out this line and i'm also going to comment out this line here so that we are really making sure that we're not using any old deprecated plugins so to speak and we can also get rid of this line as you can see kotlin x doesn't exist anymore so now we have an error so we can get rid of this line now for view binding to work we need to add another line to our build.gradle and that goes further down so here we need to add this here so build features view binding set to true okay as you can see there is no build features in my case so in case you have build features you can use it okay so this will activate the view binding so let me sync it again and once it's synced i can go to my main activity and i can prepare view binding for me so here i will need a private late init var which i'm going to call binding which will be activity main binding okay so now activity main you might wonder oh well that's the name of my xml file well that's exactly where it comes from so now it binds to the xml file so now we have a binding a connection that is going towards directly that specific xml file that is connected to this main activity and we need to add another line here which will be binding is going to be my activity main binding inflate and i need to pass in the layout inflator which will basically inflate the layout which means and will blow the layout up so that it is visible so to speak and now i need to make a little change to a set content view because what it currently does is it says the r layout activity main here but i wanted to use binding dot root okay so at this point i can use this binding my tv.text okay so let's run it and see how this is going to work for us so build succeeded and let's actually change this text so hello students again and now let me run the application again and we can see hello students again okay so that is the latest and greatest way of doing it and i still prefer this one but it really makes sense to go with the best practice approach which is going to be this binding but as i said either way is fine so your application will still work so no worries there but if you want to use view binding then this is the way to go now there is one more thing about view binding and that is to how view binding works in fragments and how it works in dialogues but that is something for another day where i'm going to cover these topics specifically as well so now you might wonder why even use this view binding and why was it introduced i mean a best practice is great and all but there must be a reason for it well kotlin acts synthetic which is the approach we saw earlier which allowed us to just use this line instead of adding all this view binding and this set content view and so forth it is great it's very simple to use but the problem is it exposes a global namespace of ids that's unrelated to the layout that's actually inflated with no checks against invalid lookups so that basically means that you could use a ui element from another activity and there wouldn't even be a check so i could for example have another activity and in that other activity i have another text view and now i could access this textview in my main activity even though the textview comes from another activity so it shouldn't even be possible to access it but unfortunately it is the case and that's one of the safety issues with the approach of using kotlin x synthetic and the next point is it's kotlin only so if you want to use java view binding is still going to work but in java the kotlin x synthetic plugin doesn't work so that's why it's another good reason then kotlin x synthetic so this approach here where we needed to add this line here this one at the top it also doesn't expose nullability when views are only present in some configurations so for example only possible to see in a specific situation it just doesn't expose the nullability there and altogether the issues caused the api to increase number of crashes for android apps and then google just decided to make sure that they don't have this problem anymore or many applications don't have this problem anymore people don't run into these issues anymore so they made sure that it is deprecated okay so these were just a couple of reasons and now you saw the alternatives and yeah that's basically it for this video i hope you enjoyed it thanks a lot for being with me and see you in the next one
Info
Channel: tutorialsEU
Views: 12,341
Rating: 4.9826088 out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, android 11, kotlin tutorial, KotlinX Extensions, FindViewByID, android kotlin project, android kotlin tutorial, denis panjuta kotlin, denis panjuta tutorial, tutorialsEU
Id: 4GXflIdrlus
Channel Id: undefined
Length: 10min 59sec (659 seconds)
Published: Thu Dec 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.