No More findViewById

I was recently told that Google likes Medium, so I’ve started posting there. Here’s a quick intro to using data binding in its most basic form:

No More findViewById

Advertisements

7 thoughts on “No More findViewById

  1. Data binding is great! I’ve managed to implement a nice MVVM approach that I’m quite pleased with. But I find something very useful is lacking – binding for option and toolbar menus. Any plans to add support for these? Would be great if it was possible to bind a menu item to its own ViewModel that controls a separate item in the menu. I mean its entire state – visibility, title, command, isChecked. Currently I don’t see a simple way to make a custom binding adapter to achieve such behavior. The problem is that in MVVM the ViewModel is forbidden to communicate to the View except through the binding mechanism.

  2. Hi George,

    My android code is full of generics GenericBindingAdapter , GenericArrayAdapter

    ==>

    ==>GenericModel i am using wild card to catch any type which is a valid syntax,
    however i dont understand why it gives the error “?” type not found , generics do work this way , it is not necessary for me to see wild card type in intellisence which is for developer comfort , can you share any work around for this issue

  3. Hi George,

    I am following your articles on DataBinding on Medium and I have got a head start on this.
    My Project Fragment Navigation is controlled by Parent Fragment or Activity through interfaces. How I can achieve this in MVVM ? Do I have to open new Fragment from ViewModel or View ?

    • You want to achieve fragment navigation through a View Model? Normally I recommend having the View Model contain the data and have a separate class control the callbacks, just for separation of concerns. In any case, fragment navigation can be achieved using the callbacks. Assuming you have a button associated with the UI that causes the navigation, you might have something like this:

      
          <Button android:onClick="@{handler.navigate()}" .../>
      

      And your handler would be something like this:

      
          public class Handler {
              private FragmentManager mFM;
              public Handler(FragmentManager fm) { mFM = fm; }
              public void navigate() {
                  mFM.beginTransaction().replace(...).commit();
              }
          }
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s