Data Binding and View Binding in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to my channel in this video we will learn data binding and view binding one by one with their examples so first let's understand what is data binding data binding is a technique that allows you to establish a connection between UI components and the data see in very simple language it's like giving a variable to text view directly in the XML layout I repeat variable not ID so usually we assign variable to a particular UI component in activity right but in data binding we can directly assign it in XML layout got it this is how we enable data binding in grer module build features and inside it data binding is equal to true now next is what is view binding see view binding is a feature that simplifies the process of interacting with UI components basically it replaces F view by ID method and also in find view by ID if you wrote incorrect ID or maybe not initialize it then it will obviously going to lead incorrect view type casting or null reference so to avoid such cases view binding was introduced this is how we enable VI binding in grd module build features and decided view binding is equal to true now what is the difference between data binding and view binding so view binding is a subset of data binding like whatever functionalities view binding can perform that can be performed by data binding as well but whatever functionalities data binding perform view binding cannot in simple words data binding can be used as a substitute to view binding but view binding cannot be used as a substitute to data binding like data binding can replace find view by ID and it can also connect data and UI while view binding can replace find view by ID but it cannot connect data in UI got it we have previously used view finding in most of the projects so I assume you are quite familiar with it and even if you're not then let's understand it better through examples this is an empty project where I will Implement view binding as well as data binding one by one so first let's start with e one that is view binding step number one is to enable view binding so go to build grader app and inside the Android brackets and below the cotl options here write build features and then inside It Right View binding is equal to true and that's it now click on sync now and done then go to activity main.xml here we will create a simple linear layout with a text View and a button so let me quickly create it and then see parent layout as linear layout and inside it a text view whose ID is also a text View and then a button whose ID is also a button and that's it there is no functionality or any logic in it I'm just demonstrating you like this will be your UI components now go to main activity. GD first let me show you the traditional method that is by using find view by ID [Music] see this is where we declare the variable and then using find view by ID we initialize the UI components but with find view by ID there are chances of wrong IDs or null reference so to avoid that let's go for view binding all also you might be wondering what's wrong with this method it's easy then why the need of view binding because if the project is small with less UI components then it is fine to use find view by ID but imagine an xtml layout file with six button F image view 10 text view then in that case you can't keep writing fine view by ID for each one of them right hence that's where we will need view pointing got it so we have already enabled view binding in grel and now we need to write three lines of code just to declare and initialize it first write private l in it where view binding as activity main binding tell me in which activity we are currently in main activity right and in which XML layout we are trying to connect activity main.xml correct so it will be activity main binding consider if the layout name is activity login then it will be activity login binding and if the layout name is activity snup then it will be activity snup binding do you see a pattern so prefix is XML layout name that is activity made and suffix is binding always remember that the Declaration is dependent on layout file name not on activity got it let me show you if I rename main activity as maybe login activity so see still no error but now I'll rename XML layout as activity login and see there is an error now it will become here as activity login pting got it hence declaration is is dependent on XML layout file name next we have already declared it now to initialize it write the variable as findinding is equal to activity login binding dot inflate as layout inflator means to fetch the layout then we usually attach XML layout in set content view but now instead of it we will write binding do root means to attach the entire layouts basically you can access any UI components from it now all the setups are done and it's time to actually use it see I want to set a text on text view so to do that first we need to access it so for that I'll write binding that is a variable then dot so when you write binding dot it will instantly show you a list of UI components see we have text view then button then container everything at one place so out of it I'll choose text view then dot text and some random text on it and that's it now I want to set on click listener on button so first to access it I'll write binding Dot and out of this list I'll choose button UI component then dot set on click listener on it and that's it then inside it you can perform any actions did you see we don't need to declare on initialize the button we just wrote binding Dot and then we had a list of UI components in it cool right and that is what VI binding is all about also make sure to remove old activity UT otherwise it will throw error now let's try data binding I'll clear everything and back to empty project now whatever we performed in view binding same things can be performed in data binding as well but view binding is not capable to establish a connection between data and your y components and that's when we use data binding so first we need to enable it and go to grle module below cot Ling options here write build features and then inside it write data binding is equal to true and that's it now click on sync now and done but if you'll notice here it throws a warning as you need to add a Codine kept plug-in so to do that in the same grel module file you will find a plug-in set where add a new plugin as ID cotlin K A and then click on sync now and that's it so data minding is enabled and now let's set up first go to activity Main XML here we have previous XML layout where linear layout as a parent layout then inside it a text view whose ID is also a text View and below it a button whose ID is also a button and that's it now here comes a Twist as I said data binding connects UI components with data so text View and button is an UI components but where is data so just above the parent linear layout we will create a layout out with all the necessary declaration so let me quickly write it and done see no linear layout no relative layout no constraint layout just a layout the closing layout will be at the end means it will cover all the UI components in it now inside the layout and above the linear layout we will add a data so I will write data and inside it I will write variable and then again inside it I will write variable name as message and its data type as string and that's it once the data variable is created then we will assign it to the UI component directly in the XML layout itself and that's what the magic of data binding is so we will assign this message variable to text view how write text as at theate curly brackets and mention the variable name that is message simple right see ID is text VI and variable is message both of them are different then come to main activity. KD here first we will declare it as private L it where binding as activity main binding I have already explained it in view binding then we will initialize it and has binding is equal to data binding util dot set content View and here mention this and the XML layout not the root and that's it so data binding is declared and initialized and now now we will see how are we supposed to use it okay so I'll write binding Dot and here you will see a list of UI components as well as variable name so I'll select message variable see if I would have go for text view ID then this is how the code would have look but as I chose variable itself that is assigned to text view I can skip text view ID as well as do text all I have to write is equal to as some string got my point also as I said data binding can do the job of view binding so in that case if I want to access button then I'll write here binding dot I'll choose button ID then dot set on click listener on it then inside it you can perform any operations and that's it I hope you have understood view binding as well as data binding Bing so make sure you use either one of them in your next project I'll recommend to go for view binding okay also for more updates you can follow us on Instagram or join our telegram group Link in the description box so yeah that is it for the video if you are new to this channel then please consider subscribing to my channel and I'll see you in the next [Music] video
Info
Channel: Android Knowledge
Views: 2,242
Rating: undefined out of 5
Keywords: data binding recyclerview android, data binding android in hindi, databinding android, data binding in java, data binding android kotlin, view binding, data binding, data binding android java, data binding tutorial, what is data binding, how to use data binding, binding adapter android, tutorial on data binding, binding adapter kotlin, binding adapter, using layouts with data binding, binding adapters, binding, binding adapter in data binding, binding objects., explain, examples
Id: 1V5g1qd8ToQ
Channel Id: undefined
Length: 13min 58sec (838 seconds)
Published: Mon Sep 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.