View binding for android.R layouts

from the CommonsWare Community archives

At November 10, 2020, 11:55am, root-ansh asked:

I am trying to move one of my project onto viewbinding. at one of the places i use an android layout resource i.e android.R.layout.simple_list_item_1 from what i know, if i have a layout R.layout.layout_x, i get a generated binding class as LayoutXBinding.class whose instance i can generate via .bind() or .inflate(…). But how to get binding for system layouts?


On a similar note, I am currently working on a project that uses a kind of generic adapter for dealing with various layouts. its not exactly generic, but a adapter creating views of multiple types and a view holder receiving and working on such views accordingly.
The synthetic binding is great in this area, helping me reduce the boilerplate by a lot. sealed classes also helps as i get all the different data modal types at one place.
to add any new item type , i just need to create its modal, add an associated entry getAllSubclassDetailsMap and add the attachment logic in viewholder. the adapter automatically extracts the layout , inflates view and returns it to holder.

So i wish to use viewbinding in this. is there some static function say ViewBindingMaster.getBinding(layoutRes:Int):T such that if i pass it ViewBindingMaster.getBinding(R.layout.activity_main) , I get an instance of ActivityMainBinidng with which i could easily use in viewholder?


At November 10, 2020, 12:28pm, mmurphy replied:

AFAIK, you don’t. If you wanted to stick with view binding even for that layout, you could copy it out of the Android SDK and into your project.

AFAIK, no. With data binding, DataBindingUtil offers some options. But AFAIK that does not work for view binding and there is no view binding equivalent.


At November 10, 2020, 1:05pm, root-ansh replied:

Ugh, i thought so . Data binding is a super sharp blade with no handlebar, so it is bound to cut us backwards if we use it in our project. And seems like view binding is worse than a butter knife( the literal one) , having no powers more than what findviewbyid would offer. we end up writing way more code than any other option . I am so angry at google and jetbrains for promoting this :confused:

I also can’t seem to find any reference docs for this the best i have found is this interface, thanks to android studio :

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package androidx.viewbinding;

import android.view.View;
import androidx.annotation.NonNull;

public interface ViewBinding {
    @NonNull
    View getRoot();
}

Also where does this plugin generates the code in our project? can’t seem to find no java classes under generated or build folders


At November 10, 2020, 1:24pm, mmurphy replied:

It prevents us from requesting widgets by ID for which the ID would be invalid.

For some things, they stopped generating Java source and are now generating Java bytecode. However, at least for view binding, I see generated results in AS 4.1:

I am converting my remaining Kotlin synthetic accessors uses over to view binding in the next update to Elements of Android Jetpack, owing to the deprecation of Kotlin synthetic accessors. This screenshot is from one of those updated samples.