Popular

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

Published on Sep 21, 2018 14,271 views

📗 Get the code written in this tutorial 👇👇
https://resocoder.com/data-binding-li...

Start saving time now by planning weekly with Week Sweep: http://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:
● http://resocoder.com

Follow me on social media:
● https://www.facebook.com/resocoder
● https://twitter.com/resocoder

Comments
  • Shivam Dawar
    Shivam Dawar 6 месяцев назад Use diagrams to explain more clearly
  • NCML NCML
    NCML NCML 13 часов назад how to set spinner using two way data binding ?
  • Naalein Grohiik
    Naalein Grohiik 6 месяцев назад 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
  • Subind Suresh
    Subind Suresh 5 месяцев назад What is two-way data binding? 10:37 - 11:02 .Thank you for the awesome tutorial.
  • Diego Vidal
    Diego Vidal 7 месяцев назад Amazing channel, please keep doing this!!
  • Reso Coder
    Reso Coder 7 месяцев назад Thank you! I'm not planning to stop anytime soon 😉
  • Muhammad Fahad Khalid
    Muhammad Fahad Khalid 7 месяцев назад (изменено) Good Tutorial, i also use Studio 3.2, but your IDE looks Cool, Why ?
  • Reso Coder
    Reso Coder 7 месяцев назад Thanks! I'm using this plugin: https://plugins.jetbrains.com/plugin/8006-material-theme-ui
  • Muhammad Fahad Khalid
    Muhammad Fahad Khalid 7 месяцев назад Thank you !!! Also done by following http://www.material-theme.com/docs/getting-started/
  • Faraz Ahmed
    Faraz Ahmed 2 месяца назад Hy i just waanna know that displaying image with glide is a two way binding or not
  • jithinbabu mb
    jithinbabu mb 4 месяца назад Thank you, it was great tutorial , but.... how to start or navigate to another activity
  • Stefan C
    Stefan C 7 месяцев назад Amazing! You are a great teacher
  • Adrian De León
    Adrian De León 1 месяц назад With DataBinding you can also delete the line setContentView(R.layout.activity_main) in MainActivity
  • Corneille Eddy
    Corneille Eddy 8 месяцев назад Great tutorial. Thank you
  • Reso Coder
    Reso Coder 8 месяцев назад You're welcome!
  • Faraz Ahmed
    Faraz Ahmed 2 месяца назад becuase live data wont notifiy the ui if the image is changed
  • Themusicaligera
    Themusicaligera 8 месяцев назад I have a question related with the UI of your Android Studio, how can I implement that UI?
  • Reso Coder
    Reso Coder 8 месяцев назад 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 8 месяцев назад thanks!!! By the way amazing channel, I just subscribed and clicked the bell icon!!!!
  • Reso Coder
    Reso Coder 8 месяцев назад Thank you!
  • Avaz Alimov
    Avaz Alimov 8 месяцев назад I have an issue with @Bindable val editTextContent = MutableLiveData<String>() It gives me an error: Bindable must be on a member in an Observable class.
  • Christian Mengler
    Christian Mengler 8 месяцев назад 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 8 месяцев назад 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!
  • Christian Mengler
    Christian Mengler 8 месяцев назад @Avaz Alimov Perfect! I'm interested to find out to see if this is what the new AndroidX package does :)
  • frigorifico91
    frigorifico91 5 месяцев назад (изменено) Its not necesari to annotate MutableLiveData with @Bindable, actually MutableLiveData it's not gonna change, just its value and Databinding cares about it
  • Orhan Avan
    Orhan Avan 4 месяца назад While textView presenting, you used LiveData instead of MutableLiveData. Why?
  • Deepak Bisht
    Deepak Bisht 2 месяца назад because you can only observe changes using Livedata
  • Orhan Avan
    Orhan Avan 2 месяца назад @Deepak Bisht thx
  • Mitch Besser
    Mitch Besser 2 месяца назад Warning... The two way data binding is broken. It creates extraneous callbacks.
  • Bendix Mahnke
    Bendix Mahnke 5 месяцев назад I'm getting error "Bindable must be on a member in an Observable class." in viewmodel. Any ideas why?
  • Christian Mengler
    Christian Mengler 8 месяцев назад (изменено) 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...
  • Reso Coder
    Reso Coder 8 месяцев назад Oh yes! AndroidX is the new android.support. It may be the case that the new ViewModel implements Observable.
  • Christian Mengler
    Christian Mengler 8 месяцев назад @Reso Coder Cheers! :) Thanks again for your great content! :)
  • Mijael Viricochea Parra
    Mijael Viricochea Parra 1 месяц назад Hi, can you tell me how did you configure that theme please?
  • Hero Truth
    Hero Truth 6 месяцев назад 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...
  • Reso Coder
    Reso Coder 6 месяцев назад 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.