Android Data Binding: Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna build the view you see here using the data binding library for Android every wedge that you see on the screen will be set using data binding there isn't a single fine view by ID method call the view has a rating bar the number of ratings a title an image being set with the glide library and a custom binding adapter a price a quantity and an Add to Cart button if you click the quantity box a dialog is inflated where you can select a number from 1 to 10 after a number is selected the quantity is updated with that number if you're new to data binding or you've never heard of it before let me give you some basic information before we look at the examples the Android data binding library was first introduced in 2015 the goal of the data binding library is to reduce the total amount of tedious code that developers have to write and to make code more concise and readable obviously as a consequence of writing less code and making code more concise there's typically a significant speed increase when it comes to development time some things you'll notice right away but the data binding library are you don't need to write fine view by ID layout files contain their own logic code and your code will be much more organized now that you have an idea of what we'll be building and why we'll be building it let's get started okay so so we can cover more in this video in less time I provided a starting point for the video so go to my github go to Mitch Tavian databinding getting started and this is the repository that we're going to start with so you can either just clone it or download it I'm going to download it and I'm going to open it with Android studio so we're here I have it downloaded I'm going to rename it to just data binding getting started and I'm going to copy that I'm going to put it into my android studio folder so where all of my Android studio projects are kept okay I've got the project in the right folder and I'm going to go open existing Android studio project going to find it okay if you're having problems I was having issues opening the project if you are just going to invalidate Cash's slash restart and you should be at the same point I am then and now we just got to configure the SDK so if we open the logcat we can see it's telling us that we need to configure the SDK so I'm just going to I guess I'm gonna choose the twenty seven point zero point three version for the build tools and click OK okay now let's press play and see if it's building correctly it opened this window so that that usually means that the SDK sorry I put bunch of popups here let's click Play okay apps running good so we ready to start so the first thing you want to do with data binding is is you need to tell Android studio that you're going to use data binding in your project so number one step one sort of is go to the SDK manager go to SDK tools and make sure that this Android support repository is installed you need that to use data binding so that's step one step two I've already done so if you go into your build.gradle file you'll notice there's this data binding in abled equals true that is required to use data binding that's basically just telling hydrants to do that you're going to use data binding in your project and that's it there's only two things you need to do so data but the data binding library is a code generator it generates code to make things easier and more convenient for you when you're when you're building your projects and to to tell Android studio which layouts you want to use data binding with or which activities or fragments you want to use data binding with it all just kind of starts in the layout so I'm gonna open up activity main because this is kind of the main layout that we're gonna be working with in this project and the first thing you need to do to tell it that you're using data binding is add these layout tags to the very top and you had those layout tags and then you take the entire contents of the layout someone's going to cut that and you paste it inside the layout tag now Android studio knows that you want to use data-binding with this layout so if I was to rebuild the project and give the code generator a chance to build the required files once once this is done loading we can go into activity main or sorry main activity and we can check to see if it generated the required class so it'll generate a new new file activity main binding so this is what's called Pascal case the the the cases here so we have activity and then it removes the underscore and then it capitalizes every first letter after an underscore or also the first letter so lower case activity becomes upper case activity it removes the underscore and then it capitalizes the next letter which is M for main and then it appends the word binding and this is what's going to hold all the variables for the view everything you need to use data binding basically so I'm gonna add a little heading here just a data binding and now we can actually get rid of this set content view and just do M binding equals data binding util dot set content view reference the context and then our layout activity main and that's gonna inflate the view just like we were doing before so if I run this again now you're gonna see the exact same thing as we did when we first ran the app just a few minutes ago so there we go we have our data bound view so that's kind of the very first that's kind of the step one for anything to do with data binding if you're using a fragment if using an activity it's always gonna be generally the same process we're not gonna actually look at fragments it's a little different but it's generally the same you add a layout tag to your layout and then you declare your binding object and then you bind the view so that doesn't seem like you saved much time you actually wrote one extra line of code as compared to just doing set content view but it definitely does pay off dividends and I'll show you what I mean so it's at a little bit more space here so what we're gonna be doing is we're gonna set a product to this view I've created a product object class it has one two three four five six seven eight parameters that describe what it means to be a product title description image price sale price ratings number of ratings in a serial number and then I have a products class here with a whole bunch of products in it they're just a bunch of stuff from my Pluralsight course I have a data binding Pluralsight course so I use the same products this is just kind of a resource so what does it mean to be a product well here we have it declaring a new product it gives it a title gives it a description gives it a rating gives it a serial number we have an image which is in the drawable folder this is the price this is the sale price and we have the number of ratings so each product in this class has these different properties and they're all just a little different we're just gonna use a single product we're going to choose one and we're gonna bind it to the view so that all probably sounded very confusing but generally just the take away from what I just said is I have a list full of products we're gonna choose one of them I'm probably just gonna choose like this red lamp and we're gonna set that product that particular product to this view and display all of its properties in this view so that's I just wanted to run and quickly go over the resource classes there and this string util I'm a big decimal in util class these are just they just have custom methods in here we'll talk about more we'll talk about those later so just don't worry about those two there's not really anything special going on here choose quantity dialogue is the dialogue for choosing the quantity so it's just gonna pop up a list of numbers when you click on this button right here but we'll talk more about that later too so don't don't even worry about that right now okay so how do we bind a product object to our layout that's that's our objective now so let's go into the layout and we need to pass a variable to the layout so with data binding you you declare variables in the layout also actually in the activity but you declare them in the layout and then you pass them to the layout file and the layout file will set them to the respective widgets so let's open up what's called a data tag here and inside the data tag is where you can declare all the variables for the view so I'll open up another new tag I'm going to open a variable tag and the name is the variable name so you can call this anything you know I could call this fubar it doesn't matter it does not matter what this name is it's just a variable just like any other variable in in an android project so but I'm going to call it product because it's going to be a product object so that makes sense and then here you want to specify the type since it's gonna be a product object we want to reference the product class so I'm referencing this class right here inside the models package and that's what you see here coated with Mitch comm data binding getting started and then models so I'm gonna click that and that's going to import the product object class so this is doing exactly the same thing as if suppose I was in main activity and I wanted to add a product object so I'm just going to create eight variables heading here and I'm gonna private product and product and as you can see if we take a look at our imports now it's referencing the product class by importing importing that class that's the same thing that I'm doing right here but we're doing in a layout and so so now that we have this product in here we can reference the variable inside of the widgets so this is the number of ratings that has a number of ratings right yes that has the number of readings we'll start with an easier one first we're gonna do the title because the title is just a string so suppose we wanted to set the title to this widget see when you're using data binding you go at and then two squiggly braces and that lets Android studio know that you're gonna do something to do with data binding you're going to be referencing one of your variables or something from inside of this data tag so we're gonna scroll down and I know it can write product to reference our product object and then I can reference all the properties for that object so as you can see these are all the parameters of the product class we want the title so I'm just gonna type in the title but if I was to run this right now we would get an error and when you get an error because in main activity we haven't said the product object to the binding so right now this knows the product class is but doesn't have an actual product set to the variable so what does that mean so if we go into main activity what we want to do is get a product object so we can go and actually we need to reference the products class fare so products products equals new products because we need to actually get up a resource from our resource class and then we're going to do M products or M product equals products dot products so this this is a list of the product objects right here that's that's this array right here it's just a big list of all the products so I'm referencing that and I'm just going to get the 0th position so that means I'm getting the first one in this list I'm getting I'm referencing this red lamp product right here so that's good now we need to set the product to the binding so I just do em binding dot set product notice there's a set product method here whoops and set the product so before I run it I just want to show you something so I at the beginning I said it doesn't matter what you call this I said you could call this foo bar it doesn't matter now if I go back to main activity notice the set product method is now red because we don't have a product variable in the layout anymore we have a foo bar variable so I would have to do set foo bar and then that would actually set the product but obviously we don't want to call it foo bar that doesn't really make any sense so we're gonna go back and call this product a set product so that's that's all we need to do now I can run it and we should see the title set to the title widget okay so there we go we none of the other widgets are set because we haven't passed any of the variables or any of the parameters but as you can see the title is definitely being set right up there so that's it that's that's how you set something to a view and then you set the properties of it and kind of the nice thing about data binding is now we're done in activity main I don't have to do anything else in here now all the work is gonna be done in our sorting main activity all the rest of the work is gonna be done in activity main so if I go in here now I need to work on the rest of these properties so let's do we'll do the number of ratings so the number of ratings is an integer so if we go to the product class the number of ratings is an integer and you can't actually set an integer to a text field using data binding I'll show you what I mean so if I do the same thing here I do num ratings and I run this we're gonna get an error okay so the app crashed let's take a look not sure why it's not showing I'm just gonna filter the the package here so let's copy the package go to edit filter paste that in let's see it's not showing an error that's weird so I'm going to rebuild and try running that again definitely crashed so I don't know why the error is not showing but let's it should come up here now okay there we go we got our air so let's see it says resource not found string ID yeah so basically it's telling me that nope that was not good you can do that and that's because you can't set an integer to a text parameter in with data binding so there's a couple ways you can work around this the first way is you can just do string dot value of just like you would do in in a java file like if I haven't if I had an integer in here and I wanted to change it to you know I had integer my int I equals 1 and I wanted to change it to a string so string my int I would do I would do string dot value of and I would pass my int and that would convert it to a string so this is the exact same thing we're doing in the layout file so if I delete this go back that's that's what we're doing here so if I run this again it should work and there'll be no problem so we have the app we have now we have the number of ratings being displayed up here and you can see obviously there's no errors because it's running so that works that's good that's kind of the first way you could solve the problem but we don't we don't actually want this to look like this we we want brackets to be around this so in order to do that you're going to do something extra kind of intuitively you might think you can do this like kind of what you do regularly like an activity file but that doesn't work you can't just add you can't just add text like that so instead what you do is you create a helper class to help you and that's kind of what this string util class is for so if we open this up you can see I've created this convert into string method and that's what this does oh no yeah that one it was supposed to be this one but anyway so what we'll do here is add some brackets so if I add a bracket here and then I add a bracket on the other side now that we can use this method to surround that value with brackets so if we go back to our file we're going to go up to the top and we're going to import that class so do import and we want to reference the type so we're gonna reverse our string util class that I was just in where we created our method convert it to string and now we're gonna use that method here so I can go string util because i've imported it and we go convert into string you can see it pops up there and there we go so we can reference a static method you can see this is a static method it takes an integer as an input and it returns a string especially formatted string that I just customized so now if I run this the number the ratings will be surrounded in brackets so there as you can see now our number of ratings is surrounded by brackets which is exactly we want so a couple ways to solve the problem you can just use kind of a default method that you use in a java file or you can create a custom class and then import it and then reference it in the widgets so it's pretty cool let's move on to the next one the next one is the rating bar so that's the one that's gonna display the number of stars here this so it's displaying this number right here the rating number and this actually takes a float so we need to if we look at our product class the rating is actually a big decimal number so we need to convert a big decimal number to a float in order to add it to the rating bar so we're gonna have a new method for that and that's gonna be this one right here so in order to use this method we need to import the bigdecimal util class to our file so let's go up to the top here and write import once you can do type and we want to reference our bigdecimal util class whoops so close that off now scroll down I'm gonna go big decimal util and then get float I believe is the method name I whoops we need to there we go so get float and then we want to reference our product and then we want to reference the rating and that should be good so let's now run that again and if we check let's open the products class here and just double check everything so the red lab product should have a rating of 4.5 so we should see 4.5 stars being displayed in the widget and there we have our 4.5 stars so that that's one way to do that but that's this is actually pointless you don't even actually have to do this because if you look in bigdecimal util all it's doing is it taking a big decimal number and it's calling float value on it so if you go to activity main you don't even actually need to do this so if I just get I can just get rid of this like a new product dot float value and that's gonna do the exact same thing so if I run this again I got it I got an error I'm probably typed it wrong let's see yeah it should be just value value dot float value well you know product oh sorry product dot rating float value and there we go we have our rating being displayed so there's two kind of different ways to do it obviously we don't need to use this method because all it's doing is calling flow value which is what we can do right inside the widget itself so next one let's see what we got here we got the image the image we're gonna come back to because it's gonna be a little tricky to do I'll show you why so if I go source on that set product image and if I run this I'll show you why it's gonna be a little tricky we're gonna have to actually build a custom binding adapter to set the image so you can see it's coming through as a purple color and the reason for that is if you go to the products class and we look at our red lamp it's referencing a drawable file that I've included so what's actually happening here is the when I do product image is it's the the drawable file is actually only an integer it's an integer that points to the resource in memory so it's an address but what happens when I do source equals product image basically it's just seeing an integer so it's Android studio thinks that I'm setting a color to it so if I was to like for example if I was just to type some random hex color here then it would set a color that's the same sort of thing that's happening here it's it thinks that I'm setting like an integer two to this to this widget which I'm not it's actually a pointer to a resource in memory so we are gonna create a custom binding adapter to help Android studio determine what's going on there but we'll come back to that after we're gonna do some of these some of these easier ones first okay so let's do let's do the price the price is the next one here so this first textview is for displaying the read price so if i actually open the demo app here so here's the demo this isn't the version we're working on you can see that there's a price in red and then there's a slashed out price here if there's a sale price so we're gonna need to do a little bit of logic a little bit custom logic in here and we're going to use what's called a ternary statement for this so before we I talked too much about it let's actually just let's just write it in so this this textview is the the red one right here so this is gonna either this needs to either display the price or display the sale price if there is one so you can see in the case of this picture there's a hundred dollar sale price and a regular play price of I think that's is one hundred and eighty five dollars and fifty cents it's a little small I can't really read it so in this case this does have a sale price but in the case of our red a red lamp product the red lab product has a regular pipes of 1099 and it has a sale price of 950 so we need to write some kind of logic here to handle whether or not to display a sale price here and as I said we're gonna use something called a Turner II it's called a Turner e-statement so first let's tell in our studio we're gonna use something to do with data binding and so I guess I'll show you the method first so if you go into the product class it has this method I wrote this method here called has sale price and what it does is it checks to see if there is a sale price so if the if the sale price is greater than zero it returns true if it doesn't returns false and that's what we defined to be a sale price in this video so if a product has no sale price let's go to the products class for example this yellow lamp product right here we take a look at it it has a sale price of zero so if we go back to our product class if it has a sale price of zero this method will return false if it if it has a sale price greater than zero then this will return true which is the case with our red lamp product ok so let's go back to activity main so what we can do is go products dot has sale price that will return true or false depending on if a sale price exists and then we just want to write the turn earning so if it does have a sale price then we do product dot sale price and if it doesn't have a sale price then we do product dot price so that that would be that it's it's saying if the products is the sale price display the sale price if it doesn't display the price but there would be a problem here because as you know we can't set numbers to text fields with with data binding so the sale price and the price fields are actually big decimal numbers so if we look here the price and the sale price are big decimal numbers so we're gonna use we're gonna need some kind of custom method to convert these to a string and it's got to be a specially formatted string because we need to display two or we needed to display the dollar value and then also the cents so only two decimals are to be on these strings and I created a method in the bigdecimal util class just for that so that's what this bit that's what this getvalue method is for it's going to convert the big decimal value to a dollar value and then convert it to a string and return it that's what this is right here so once again we're gonna use our big decimal util class I'm gonna go big decimal util dot was it called get value get value and then just surround both of these with that method so I'm just gonna copy this here and scroll over and there we go so now let's run it and take a look and see if that's coming through properly cool so there's our price you can see the prices coming through so but that's only displaying the sale price now so now we need to work on the actual price so let's go and display it in this kind of striped out version so we're gonna do the same thing actually in this case we don't need to write a turnery for the text because if there if there is a sale price then we want to show if there isn't a sale price then we don't want to show but no matter what this is going to be displaying the actual price so regardless of anything this is going to be displaying the actual price so product dot price no matter what the difference is going to be whether or not it's visible so in the case of let's go to the products class in the case of a red lamp product obviously it's going to be visible but in the case of a yellow lamp product we just don't want to display it it's just going to be invisible so we can use another turnery and set the visibility so we can do visibility and so if product dot has sale price once again then we want to set the visibility so we can actually reference the view class so if it's if it has a sale price then we want it to be visible otherwise we want to be invisible so if the product is the sale price we want to show this otherwise don't show it and we since we're using the view class here we need to import it at the top so we scroll up and we go to import type and we want to reference the Java Android view class just like that and that should be everything we need for our prices so let's run that and take a look cool so we have our regular price being displayed there so our sale price and there's a regular price slashed out now just to test it we should test with something that doesn't have a sale price and as we looked at in our products class the yellow lamp does not have a sale price so let's go to main activity and I believe the yellow lamp is the second in the route in the array yeah it's the second one so I can just set this to one and that will change the product to one that does not have a sale price and there we go so in the case of the yellow lamp we're only seeing the regular price because there is no sale price so everything is working as we expect so I'm going to change this back to the 0th position so we see this slashed out and now go back to activity mate and let's work on the next part so let's see here so the next part is going to be the quantity so that's going to be this little box right here and if we take a look at the test application we want to display a string that says QT y colon and then the number then then the actual quantity and also when we click on that and use to open up a dialog so we can select a quantity so I could select 5 and then 5 is passed down to the view down there so there's a lot of stuff going on here we need to number one I guess first display an integer representing the quantity and number two is we need some kind of onclicklistener to click this and then inflate choose quantity dialog which is this this class right here so we'll start with the easiest thing first first we're going to set the quantity and so the product the product object class actually doesn't have a quantity variable the quantity is going to be independent of the actual product itself so to display a quantity here we're going to need a new variable so let's go back to the top and let's a new variable so I'm gonna go variable I'm going to call it quantity and it's going to be of integer type that's all I need to do now I can scroll down to where I was and I'm just going to add data to a data binding thing and passed the quantity so now just that that number will be displayed here in this text video so now let's go into main activity and we need to set the quantity to the binding so M binding set quantity and we'll just set it to a quantity of 1 so let's run it and take a look and see what that looks like the app crashed and I bet you can guess why we just passed an integer to where you can't pass an integer so we need some kind of a specially formatted string here anyway so if we open up the test application we need to we need to say QT y colon and then display the integer so I created a custom method for doing just that I believe it's in nope it's in the string util class so here the method called get quantity string will take an integer as an input and then it will output a string this is QT y colon and then convert that integer to a string so this is the method we're gonna reference so I'm gonna go down here we're gonna do string util dot get quantity string and then we're gonna pass our quantity so let's run that and take a look there we go so we have our QT y and our number next is going to be the onclicklistener and inflating that dialogue so right now if I click this it does nothing so that's what we're gonna work on now and to do that we're going to use an interface there's a lot of different ways you can do this just like there's a lot of different ways you can add an onclicklistener in main activity like you could you know you could implement the view onclicklistener interface and then set the clicks that way or you could do something like binding reference reference something and then just do set onclicklistener there's that there's a bunch of ways to do it but we're gonna use an interface so I'm gonna right-click on the main package directory and I'm going to create a new interface called I mainactivity and we're gonna create a method called inflate quantity dialog so I'm going to take anything just it's just gonna be a method for for blowing up a dialog so let's go into main activity and we're going to implement that interface so I mean activity now click the red light bulb implement the methods implement the method and now in here we're going to inflate the dialog so choose quantity dialogue dialogue equals new choose quantity dialog and then dialog dot show and we do get support for manager we're gonna reference the name of the dialog so r dot string obviously I've added this ahead of time to the strings.xml file and that's going to inflate our dialog so now how do we use the interface in with data binding because as you know you have to pass variables to the view just like we've done we have to import libraries if you want to reference them and using an interface is no different so we actually need to pass the interface as a variable to the layout so we're gonna create a new variable I'm gonna call it I mean activity with a lower case I and now we're gonna reference the I main activity class that I just created now we're gonna go back to main activity and we want to set the set the interface to the binding so binding set I mean activity and we want to reference the context or sorry don't tell it that we're gonna reference I'm an activity and then reference the context so now just like these other variables we have a I'm a an activity interface set to our layout now we can reference the methods for that interface so in our case we just have a single method inflate quantity dialogue and we can reference that in our layout so if we scroll down and we're going to have it to this relative layout right here this this relative layout is the one that contains kind of this whole this whole thing here so we need to do on click and then we're going to get started with the data binding and do two brackets oops two brackets a right carrot and then we're gonna reference I mean activity and do inflate quantity dialogue and that's all we need to do so we don't need to set it on click listener this on click attribute is taking care of everything we need so let's run that and see if we're getting our dialog popping out okay let's click it and there we go so there is our dialog so the onclick attribute is working correctly our interface is working correctly everything's looking good but there's it's currently it's not sending it back to the layout file so if I select something the number isn't being set so we're gonna need to use another interface method for that we need to send that value back to main activity so they get it gets set so we can set the quantity to the binding so let's go to choose quantity dialog and I've already added an onclicklistener and everything to the ListView here so really all we need to do is call some kind of a method in here that's going to send that value back so if I go to i main activity we can create a new method so void just do set quantity integer quantity now go back to choose quantity dialog we can just create an instance of the I mean activity interface and then just do I mean activity set quantity and we can get the quantity from the adapter so oops so I'm I do integer parse int because it's going to be a string value because the the values in the ListView are actually string so if you look at the this is what's set to the ListView and these are strings so we'll need to convert it to an integer since the set quantity method accepts an integer so there's string and then adapter view I get item at position I and that should do it so that'll pass the quantity back to main activity so if we open main activity we need to implement that new SEC or guess that quantity method so get set quantity and now we can set that quantity to the binding so we just do in binding set quantity to quantity now let's write and see if that's working all right click the dialog select a number there we go so the number is being passed back to the view so that's good it's it's the dialog opens I select something from the list that quantity is then sent through the I'm an activity interface and main activity picks it up and sets that quantity to the binding just by writing m binding dot set quantity so that's pretty cool let's take a look at what's next just the Add to Cart button so I'm actually not even gonna do that because we're not actually adding anything to the cart I can print out a toast or something but we'll leave that for now let's let's work on the image so if we go up let's work on that image that's displaying the color so for this we're gonna need to do build a custom binding adapter a custom binding adapter is like kind of like another way to build a custom method but more more detailed I guess so really what you can do is create custom attributes so active before I even talk about it we're just gonna do it because it's just gonna sound confusing so let's go and I'm gonna create a new package so right click on the main package directory go to new package I'm gonna write data binding and then inside the data binding package I'm going create a new class I'm gonna call it glide binding adapters because in here this is where we're gonna put a binding adapters just for setting images with glide and give some more space here so how do you make a binding adapter you start with the binding adapter annotation and then inside of the brackets you want to give a name to the binding adapter so this is what's going to show up in the attribute in the layout so I'm just gonna call this image URL because we're going to be setting I could call it image resource but I'll just leave it because we're gonna be using that to set an image so I'm creating a static method public static void I want to call it set image this name doesn't matter I could do set image resource just so it's different so you can see that they don't have to match and then I'm gonna pass the widget type that we're going to be setting which is an image view and then of course the resource so in our case we're using integer resources because we're referencing drawables so I'm going to write an integer resource but typically you probably if you're going to use data binding in an app it's probably going to be setting a string resource probably nine times out of ten it's going to be a string resource because databases will typically hold a URL pointing to an image in the form of a string resource we're gonna create another binding adapter for that but just for now we're going to work on this draw ball so next we need to get the context so context context equals view dog get context and then we're going to set some request options for glide equals new request options just do placeholder our drawable whoops our draw I'll do just do I see launcher will do error same thing arta drawable dot I see the launcher and now we're gonna use glide so Glide dot with context and then we want to set the request options and then we want to load the URL so image URL and then load it into our image view so this is exactly how you would use Glide normally just as if if we were in main activity and wanted to set the image you would have done the exact same thing in here if you wanted to set the image but instead with data binding you got to create a custom binding adapter so now we can use this custom binding adapter in our layout so if we go to activity main and I write app and you can see now that image URL method is popping up here so I can do this and then I can reference reference the image so product image and now it will work properly because we have a custom method for setting that image this is referencing the binding adapter method right here so if I was to change this just to show you I change this to image resource this would have to be image resource so now if I run this damage should be displaying as it's supposed to so there we have our red lamp and that pretty much completes our layout so we have our title we have a rating we have our number of ratings we have the price we have the sale price we have the quantity we have a dialog for selecting the quantity and we haven't add to cart button that doesn't do anything so that's it and so what I was talking about before about the string resources is let me just open a let's go to reddit.com most most of the time when you're setting images you you set a string resource nine times out of ten that's usually how it is so we're gonna go to earth horn and we're gonna grab grab an image so I'm just gonna pick anyone doesn't matter I'm choosing to want it complete random selecting the image so this is what would be this is a URL pointing to an image on the internet and let me just I'll try and set this and I'll show you that it doesn't work so I'm going to create a new variable I'm gonna call it test URL and it's going to be a string and I'm gonna go down to where we just set that image where we just set the image here so I'm gonna do on a pass test URL here and I'm gonna go into me an activity and do em binding set test URL I'm gonna paste paste in that URL that I got from the internet so with glide Glide can definitely handle this write it Glide can set anything pretty much but this isn't going to work because this only accepts an integer so you can see there's there's the error coming up so what you need to do is you need to overload this to accept strings also so if I'm copying that I'm just going to change this input to a string now if I run it again it will work because now it there's another method for accepting a string and it's it's not working because I don't have internet permission so it's trying to get an image from the internet so use this permission Internet try running that again now it looks like there's an issue with the image let me try a different image glide doesn't like this image I don't feel like troubleshooting it let's just choose a different one try this one go to main activity change this try that again I just realized I didn't want my Wi-Fi on on the device I was testing on so this wasn't working so I've turned Wi-Fi on now so the image should load and there we have the image so you can see that now it can accept a string resource and if you wanted to accept any other type of resource you'd have to do the same thing you just overload it change the object type and you would be good to go so that's that's it for the video hi this is just kind of a brief introduction of data binding and might I just produced a new Pluralsight course it was launched the other day on data binding it goes much more in-depth this is honestly just barely scratching the surface of what you can do with data binding you can see we saved a little bit of time and definitely our code is a little more concise a little better but it can definitely get a lot better still and in the course we look at custom observable classes which will actually update parameters in the layout without you having to do anything which is really awesome so if you're if you have if you have a view that's constantly being updated or values are changing too the view may be you're bringing in different data from the internet somehow data binding is definitely gonna save you a lot of code in terms of that if you're using custom observables and I definitely encourage you to check out the course it's really good really great getting started kind of data binding course this was just kind of a sneak peek now you hopefully see you have a bunch more tools in your tool belt you can import variables to views you can use an interface you can import custom classes and reference static methods create a custom binding adapter in the in the actual Pluralsight course we look at something called two-way data binding which is updates to view is causing updates to other views some really cool stuff this is the course here on Pluralsight it's called android data binding getting started of course Pluralsight does cost money to join but you can start a free trial there's going to be a free trial link in the description of this video so if you want to try Pluralsight out and watch my data binding course click that link and you can get ten days free on Pluralsight actually while you're watching I might as well show you the full the full version of the app we actually build in the course in my data binding course on Pluralsight so this is what we're building we're building kind of an e-commerce app I tried to make it look like Amazon I think it looks quite a bit like Amazon so there's a bunch of products you can choose from I can you know select select one of these looks just like the fidgets spinner of course you can choose the quantity this is the screen we were just working on in the video I can click Add to Cart you can see that it was added the cart I can adjust the quantities in this cart and this is what's called two-way data-binding right here so you can see the view is updating in the list and then it's also updating in the cart up there so the items are changing the cart total is changing everything is changing so and then I can if I increment this down to zero it will actually get removed from the cart and everything's updated so this is this is what we'll be building in the course if you choose to take a look at my data-binding course on Pluralsight but that's it for this video hopefully it was helpful and as always thanks for watching you
Info
Channel: CodingWithMitch
Views: 96,721
Rating: undefined out of 5
Keywords: android tutorial, android data binding, data binding android, android data binding example, how to use data binding android, android binding adapter, binding adapter android, android clean code, android model view view model, android mvvm
Id: v4XO_y3RErI
Channel Id: undefined
Length: 46min 4sec (2764 seconds)
Published: Mon Mar 19 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.