Data Binding with LiveData (Two-way & One-way) - Android Kotlin Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 2024
  • 📗 Get the code written in this tutorial 👇👇
    resocoder.com/...
    Start saving time now by planning weekly with Week Sweep: bit.ly/2Jf3QA0
    Managing the layout programmatically can be quite time consuming and verbose on Android. Even though Kotlin spares you of calling findViewById, accessing views in the activity code can still get ugly. When you use data binding, you can do most of the UI related stuff right in the XML layouts.
    With LiveData, data binding becomes even more powerful and simple to use. While you could create observable fields even in the past when LiveData didn't exist, this new life-cycle aware observable makes data binding even better and simpler.
    In this tutorial you are going to learn how to use one-way and two-way data binding with LiveData and MVVM architecture.
    Go to my website for more information, code examples and articles:
    ● resocoder.com
    Follow me on social media:
    ● / resocoder
    ● / resocoder

ความคิดเห็น • 97

  • @subindsuresh4897
    @subindsuresh4897 5 ปีที่แล้ว +13

    What is two-way data binding? 10:37 - 11:02 .Thank you for the awesome tutorial.

  • @Emsique
    @Emsique 5 ปีที่แล้ว +2

    Greeting from Russia! One of the best tutorials I have ever seen!!! The best wishes!

  • @BruceCichowlas
    @BruceCichowlas 5 ปีที่แล้ว +2

    I like your explanation style. I couldn't quite get the repository to build, but I was able to transplant the code into another project and run it. I watched quite a lot of videos on two-way binding and read a lot of tutorials, but yours was the clearest and had code I could use. I'll be looking for other topics, like recycleview with databinding, preferably in Kotlin.

    • @Krush11
      @Krush11 ปีที่แล้ว

      it is clear an d a calming voice.

  • @KyawAungWin-Win
    @KyawAungWin-Win 6 ปีที่แล้ว +3

    Greeting from Myanmar! Great tutorials... Wish you get more and more subscribers and keep doing that kind of great tutorials!

  • @andreeagorcsa1812
    @andreeagorcsa1812 3 ปีที่แล้ว +1

    Thank you and God bless you for making this helpful tutorial!

  • @fuzuyx
    @fuzuyx 4 ปีที่แล้ว +1

    been struggling for this thing for days. missing .value and now it work. thanks

  • @voicheci007
    @voicheci007 6 ปีที่แล้ว +4

    Amazing! You are a great teacher

  • @haykmkrtchyan7093
    @haykmkrtchyan7093 3 ปีที่แล้ว

    I'm a big fan of your tutorials)) Also, I've added one more imageView in the XML and created a Binding Adapter. Using that, I'm loading an image using Glide. And for the image URL, I've created an Object, where I store my URL. Then I imported that Constants Object inside XML as and used the URL inside that object to load the image.

  • @LeonardoCostaCoesta
    @LeonardoCostaCoesta 4 ปีที่แล้ว

    Clear and concise tutorial, thanks from Brazil!

  • @alexwijoyong
    @alexwijoyong 4 ปีที่แล้ว

    Thanks for the information about MutableLiveData for two way data binding.

  • @codervlogs4921
    @codervlogs4921 4 ปีที่แล้ว

    Great tutorial

  • @tonyalbanese6446
    @tonyalbanese6446 4 ปีที่แล้ว

    Thanks for making this video. You explain this concept excellently! Just perfect.

  • @rahulchandrabhan
    @rahulchandrabhan 2 ปีที่แล้ว

    Awesome video bro, also can you please create playlists on android tutorials.

  • @ricindigus
    @ricindigus 4 ปีที่แล้ว

    THANK YOU VERY MUCH!!!!!!!!

  • @jaken82
    @jaken82 2 ปีที่แล้ว

    Perfect! Just what I needed.

  • @farshaadheydari5807
    @farshaadheydari5807 9 หลายเดือนก่อน

    Thanks man , nice video 👍

  • @PetBuddies
    @PetBuddies 2 ปีที่แล้ว

    I really wanna know which theme of android studion you are using! It looks awesome

  • @nicovillegas4774
    @nicovillegas4774 5 ปีที่แล้ว

    Thank you so much your tutorials are so great.

  • @Shad0wB0X3r
    @Shad0wB0X3r 3 ปีที่แล้ว

    Is it even needed to use databinding while using kotlin? Woopsies, ur vid description answers my questions

  • @luisbaltodano1941
    @luisbaltodano1941 4 ปีที่แล้ว

    Great tutorial , 2w binding is awesome

  • @deepakrattan2328
    @deepakrattan2328 4 ปีที่แล้ว

    Great tutorial.Thanks for the help .

  • @vladfedos5554
    @vladfedos5554 3 ปีที่แล้ว

    Amazing, thats all i need) thanks

  • @AkashJhaDDN
    @AkashJhaDDN 5 ปีที่แล้ว

    Thank you for your tutorial, it was so clear

  • @farazahmed6749
    @farazahmed6749 5 ปีที่แล้ว +3

    Hy i just waanna know that displaying image with glide is a two way binding or not

  • @2004michaelpeer
    @2004michaelpeer 4 ปีที่แล้ว

    Great tutorial, thank you

  • @yugdamor5556
    @yugdamor5556 5 ปีที่แล้ว +4

    e: error: Bindable must be on a member in an Observable class. MainViewModel is not Observable
    please help

    • @begalisydykov6404
      @begalisydykov6404 5 ปีที่แล้ว +5

      Remove `@Bindable` annotation. `@Bindable` is used with `Observable`. It'll work.

  • @williamrai9911
    @williamrai9911 5 ปีที่แล้ว +11

    Bindable must be on a member in an Observable class. I get this error.

  • @polinacoulter4037
    @polinacoulter4037 2 ปีที่แล้ว

    new sub ;) ur dope bro keep it up

  • @bensaidali1670
    @bensaidali1670 4 ปีที่แล้ว

    Good Tutorial but i got an error illegal character at index 0 android

  • @amilalisahil4661
    @amilalisahil4661 4 ปีที่แล้ว

    which theme you are using

  • @terjemah_alquran
    @terjemah_alquran 4 ปีที่แล้ว

    thank you and autosubscribe after watch this excellent tutorial

  • @diegovidal7963
    @diegovidal7963 6 ปีที่แล้ว

    Amazing channel, please keep doing this!!

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +1

      Thank you! I'm not planning to stop anytime soon 😉

  • @begalisydykov6404
    @begalisydykov6404 5 ปีที่แล้ว +1

    Remove @Bindable from ViewModel's MutableLiveData field. It's redundant and causes compile time exceptions.

  • @christianmengler2169
    @christianmengler2169 6 ปีที่แล้ว +1

    Thank you for your great tutorials! I noticed you are using AndroidX package which I had no idea what it was until your tutorial :)
    Now that this AndroidX package is 1.0.0 production ready as of 21st September, is it recommended to use these over the android.support* packages? Reason I'm also asking is because when using @Bindable in ViewModel the compiler complains about the class not being of Observable which I suspect there may be changes in the new AndroidX package that I'm yet to try to see if it resolves this error...

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +2

      Oh yes! AndroidX is the new android.support. It may be the case that the new ViewModel implements Observable.

    • @christianmengler2169
      @christianmengler2169 6 ปีที่แล้ว

      @@ResoCoder Cheers! :) Thanks again for your great content! :)

  • @rohitmanohar2733
    @rohitmanohar2733 4 ปีที่แล้ว

    Hi , I have just one doubt , if we have a dataclass having 2 field name and address and that is intialised with Live data then how can we achieve 2 way binding for that,

  • @ftrrhmn09
    @ftrrhmn09 4 ปีที่แล้ว

    i can't next step in type="com.resocoder.databinding.MainViewModel" why??? can help me :(

  • @shivamkapoor5549
    @shivamkapoor5549 4 ปีที่แล้ว

    Hi, could you please help me. When I try to bind mutablelivedata-string variable it works fine and I can observe any changes in the activity, however, when I try to bind mutablelivedata of custom model type, I am unable to observe any change to the value of the custom model attribute in my activity.

  • @sreerajvijayan3072
    @sreerajvijayan3072 3 ปีที่แล้ว

    Your voice seems similar to Jiraiya from Naruto series. Only a bit lighter.

  • @sergio3528
    @sergio3528 5 ปีที่แล้ว

    Great tutorial, thanks.
    I have a question though, imagine we wanted to do something else besides modifying 'editTextContent'. For example store in the database every change (just theoretical example).
    I don´t want an observer observe the field from the Activity, because this is a business action, nothing to do with the view.
    How could i do this? Is it possible to customize the way the value is set, maybe with a setter method or similar?

  • @abhisheksoni4822
    @abhisheksoni4822 5 ปีที่แล้ว

    @Reso Coder which version of android studio is you are using ?

  • @lekkalanarayana182
    @lekkalanarayana182 3 ปีที่แล้ว

    How @bindable is working in viewmodel??....when I use same like your code ...it reflecting error.....same issue is raised in stackover flow ....no need to use @bindable just @={viewmodel.edittext} is enough...it will update edittext field in viewmodel.

  • @munierparker
    @munierparker 5 ปีที่แล้ว

    Hello, thank you for a great video on data binding. Could you please tell me what Theme you are using, I really like it.

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว +1

      Material Theme UI

    • @munierparker
      @munierparker 5 ปีที่แล้ว

      @@ResoCoder Thank you :)

  • @avaz.alimov
    @avaz.alimov 6 ปีที่แล้ว +1

    I have an issue with @Bindable val editTextContent = MutableLiveData()
    It gives me an error: Bindable must be on a member in an Observable class.

    • @christianmengler2169
      @christianmengler2169 6 ปีที่แล้ว

      I have the same issue, I just noticed the use of the AndroidX package which is something new to me that I only found out about today. I'm wondering if ViewModel has changed in these new packages.

    • @avaz.alimov
      @avaz.alimov 6 ปีที่แล้ว +1

      Actually it's not so much difficult to fix it. I've just created my own ObservableViewModel class which extends the Observable class, and used it in my viewmodel. Then it worked!

    • @christianmengler2169
      @christianmengler2169 6 ปีที่แล้ว

      @@avaz.alimov Perfect! I'm interested to find out to see if this is what the new AndroidX package does :)

    • @frigorifico91
      @frigorifico91 5 ปีที่แล้ว +3

      Its not necesari to annotate MutableLiveData with @Bindable, actually MutableLiveData it's not gonna change, just its value and Databinding cares about it

  • @tusharpingale2384
    @tusharpingale2384 6 ปีที่แล้ว

    How to use focus listener on edit text and validate the data after loosing focus on edit text. Any Ideas?

  • @jithinbabumb2355
    @jithinbabumb2355 5 ปีที่แล้ว

    Thank you, it was great tutorial , but.... how to start or navigate to another activity

  • @yavgani
    @yavgani 5 ปีที่แล้ว +2

    How can I customize my android studio to look like yours?

  • @mijaelviricocheaparra7474
    @mijaelviricocheaparra7474 5 ปีที่แล้ว

    Hi, can you tell me how did you configure that theme please?

  • @fadialkhachroum9548
    @fadialkhachroum9548 5 ปีที่แล้ว

    thank you

  • @shyiamnot212
    @shyiamnot212 4 ปีที่แล้ว +1

    The way of writing databing related code in the video is over complicated. 5:47 / 32:04

  • @Monarch_943
    @Monarch_943 5 ปีที่แล้ว +5

    I'v been using ontextchangelistner all the time :/ I didn't know about the @={}. This saves alot of time and a lot cleaner. Dude you are a life saver

  • @razanabughazaleh8342
    @razanabughazaleh8342 4 ปีที่แล้ว

    how to read ImageView from ViewModel?

  • @farazahmed6749
    @farazahmed6749 5 ปีที่แล้ว

    becuase live data wont notifiy the ui if the image is changed

  • @papanino4415
    @papanino4415 5 ปีที่แล้ว

    You're holding on to the same state - currentRandomFruitName - in two different places while trying to synchronise between them. There should be a law against that.

  • @svoicum
    @svoicum 5 ปีที่แล้ว

    What color scheme are you using? ^.^

  • @ncmlncml9506
    @ncmlncml9506 5 ปีที่แล้ว

    how to set spinner using two way data binding ?

  • @muhammadfahadkhalid4779
    @muhammadfahadkhalid4779 6 ปีที่แล้ว

    Good Tutorial, i also use Studio 3.2, but your IDE looks Cool, Why ?

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +1

      Thanks! I'm using this plugin: plugins.jetbrains.com/plugin/8006-material-theme-ui

    • @muhammadfahadkhalid4779
      @muhammadfahadkhalid4779 6 ปีที่แล้ว

      Thank you !!! Also done by following www.material-theme.com/docs/getting-started/

  • @shivamdawar97
    @shivamdawar97 5 ปีที่แล้ว +6

    Use diagrams to explain more clearly

  • @Themusicaligera
    @Themusicaligera 6 ปีที่แล้ว

    I have a question related with the UI of your Android Studio, how can I implement that UI?

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว +1

      I have downloaded a plugin called "Material Theme UI" and I have chosen the "Material Darker" variant. I'm also using Consolas font because I don't like Fira Sans that comes packaged with the plugin.

    • @Themusicaligera
      @Themusicaligera 6 ปีที่แล้ว

      thanks!!! By the way amazing channel, I just subscribed and clicked the bell icon!!!!

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว

      Thank you!

  • @orhanavan7222
    @orhanavan7222 5 ปีที่แล้ว

    While textView presenting, you used LiveData instead of MutableLiveData. Why?

    • @deepakbisht7764
      @deepakbisht7764 5 ปีที่แล้ว +1

      because you can only observe changes using Livedata

    • @orhanavan7222
      @orhanavan7222 5 ปีที่แล้ว

      @@deepakbisht7764 thx

  • @lakshminarayanacharan837
    @lakshminarayanacharan837 3 ปีที่แล้ว

    Every one will take only text view and edit text for data binding… no one used check box spinner and other important views along with text views

  • @abdusalam3ar
    @abdusalam3ar 4 ปีที่แล้ว

    Pro tip: play the video in 1.25x speed!

  • @DanishKhan-ps3ik
    @DanishKhan-ps3ik 5 ปีที่แล้ว

    downloaded the source code and it has too many errors in MainActivty.

    • @AkashJhaDDN
      @AkashJhaDDN 5 ปีที่แล้ว

      I imported in latest android studio and goes well

  • @advancedprogramming62
    @advancedprogramming62 5 ปีที่แล้ว

    its work but its not perfect

  • @bessermt
    @bessermt 5 ปีที่แล้ว

    Warning... The two way data binding is broken. It creates extraneous callbacks.

  • @appranger6639
    @appranger6639 3 ปีที่แล้ว

    This doesn't work anymore. I wasted my time

  • @herotruth4613
    @herotruth4613 5 ปีที่แล้ว +1

    Good, but why would you use MutableLiveData if you think its unsafe. Why not teach people how to do the proper way instead of an unsafe way. Seems irresponsible...

    • @ResoCoder
      @ResoCoder  5 ปีที่แล้ว

      It's only as unsafe as the programmer writing the code is irresponsible. There's no way around MutableLiveData when it comes to two-way data binding. That's because the data binding needs to "mutate" the data itself.

  • @14bbb88
    @14bbb88 5 ปีที่แล้ว

    expose

  • @ZulqurnainCodingDiaries
    @ZulqurnainCodingDiaries 5 ปีที่แล้ว +1

    you talk too much

  • @EddyCaffrey
    @EddyCaffrey 6 ปีที่แล้ว

    Great tutorial. Thank you

    • @ResoCoder
      @ResoCoder  6 ปีที่แล้ว

      You're welcome!