Live Data in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will learn about live data with view model I have already covered view model in my previous video so I'll highly recommend you to watch that video first before watching live data okay all right coming back to today's topic you remember these are the three components of mbvm right model view and view model model represents the data view is basically the UI and view model acts as a bridge between model and view basically it is responsible for managing UI related data and also for communication that takes place between View and model communication isn't to send and receive requests to perform a particular action action is in to maybe update the data or delete the data got it now Focus between View and view model this is where live data resides between View and view model but what is live data live data is part of Android architecture components and also it is a data holder class that is observable which means it can be used to hold and observe changes to the data in very simple language live data is like a special container that holds a piece of information and it can let other parts of your app know when that information changes let's understand it through an example imagine you are building a weather app that displays the current temperature for our city so instead of manually updating or refreshing the temperature display every few minutes or whenever it changes you can use live data it's like live data holds the temperature data and the UI observes it the temperature data changes then the UI automatically displays the updated temperature value which makes your weather app more Dynamic and responsive without the need for manual updates got it now let's understand live data more through the project so open Android Studio choose empty views activity name the project as learn live data let me give you a quick overview what exactly we are going to create so we will be creating a counter app where if the user clicks on the button then the account will be incremented by one x0123 that way I know it's a very simple app but again our focus is to understand how can we use live data in the project also it's the same map which we created before in view model but this time we will be creating it with the help of live data the use of lime data ensures that any Observer of the counter live data will be notified automatically when count value changes this intent triggers the UI update in the activity simple right now let's create it first let's do the prerequisite so go to colors.xml add the red color hex code AS ff3131 and then next go to themes.xml add these three lines color primary color primary variant and Android status bar color and then now go to build.gradle app here add view binding and click on sync now and then as we want to use live data as well as view model hence we will require both the dependency you can find them in the description box then click on sync now and then close all the tabs now go to activity main.xml here I'll create a text view that represents the count and a button that will increment the count so let me quickly create it I'm done see parent layout as constraint layout then inside it a text view whose ID is text view counter and then below it a button whose ID is button increment and that's it this is how it looks now comes the important part as I previously said live data is present between View and view model so view is our activity main.xml and now let's create view model so right click on it create a new kotlin class name it as my viewmodel this is where the magic happens I already shown you how viewmodel works first is to extend clamps as view model perfect so previously inside it we created counter variable and its function right but now here inside it I'll create live data first create a variable named as count live data then initialize it as mutable live data and keep the data type as int now what is mutable live data mutable lime data is a subclass of lime data that allows you to modify the value it holds this means you can change the integer values stored in the count live data then we need to initialize the value so let me quickly write it first and then now see init blog is used to initialize properties when an instance of the class is created like in our case init block is setting up the initial value as 0 of the count live data property next we need to declare live data so let me quickly write it and then now see here I have declared a variable that is counter live data as live data which holds the integer values only then get Block is used to retrieve the value of count live data and provide it as the value of counter live data lastly I'll create an increment counter function so let me quickly write it [Music] and then now see this increment counter function will be used when the increment button will be clicked basically it will increment the number by 1. it goes like count live data Dot value will retrain the current count that is display on the text View and then using question mark and colon that is always operator it sets the default value as 0. and then increment it by 1. simple right so quick overview when you access the counter live data property which we will do it in main activity then it Returns the value of count live data this allows UI components to observe and access the counter value as live data without the need to directly access count line data great right now let's move to main activity and increment the counter so first let me quickly set a binding I'm done next we need to access my viewmodel function so for that we need to call my viewmodel class so let me quickly declare it and done now I need to initialize my V model so I'll write my viewmodel as viewmodel provider C view model provider is a utility class provided by viewmodel library that helps you to create and retrieve instances of view model classes for us it's a myvmodel class so first I'll write this as the owner this answer main activity itself then using get method I'll retrieve the my B model class this is how we initialize it next we need to observe the live data so I'll write my view model dot counter live data remember counter live data not count live data okay then dot observe as this then create account variable that will keep the count value and then inside it set the count on the text view counter using text method then add Observer here so here UI is observing counter live data and whenever the count change it will immediately update the text view counter see so simple right lastly set on click listener on button increment and then inside it right my viewmodel and access the increment counter function which we created in my viewmodel class and that's it we are done with the coding now let's run the app see it's initially zero now click on increment button and look the count goes like 1 2 3 and so on perfect the app is very easy that's not even the concern my concern is that you guys have understood how live data work right to make it easy for you remember these four steps first is mutable live data second is init blog then third is live data and then fourth is observing the live data simple right and that's it for more updates you can follow us on Instagram or join a 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 video [Music] foreign
Info
Channel: Android Knowledge
Views: 1,375
Rating: undefined out of 5
Keywords: livedata, android, androidviewmodel, model, androidstudio, view, orientation, guide, device, rotation, lifecycle, onsaveinstancestate, architecture, save, room, data, live, change, howto, rotate, kotlin, activity, tutorial, viewmodelprovider, component, components, app, mvvm, counter app, configuration changes, system configuration in android, model view view model, observe., java, xml, jetpack, advanced, knowledge, viewmodel, android advanced, simple app, create app, studio, coding, learn, livedata project, example
Id: YuWmPLH3vXk
Channel Id: undefined
Length: 12min 58sec (778 seconds)
Published: Wed Sep 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.