Android View Binding Delegation way

Md Ali Hossain
4 min readApr 28, 2021

--

Probably some of you still using kotlin synthetic in your project like us and want to get rid of it. Since Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported. If your app uses Kotlin synthetics for view binding, you can use Jetpack view binding.

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

In most cases, view binding replaces findViewById. In this blog, I will share how you can use android view binding in an efficient way through kotlin delegation features.

Setup instructions

View binding is available since Android Studio 3.6

View binding is enabled on a module by module basis. To enable view binding in a module, set the viewBinding build option to true in the module-level build.gradle file like:

android {
...
buildFeatures {
viewBinding true
}
}

If you want a layout file to be ignored while generating binding classes, add the tools:viewBindingIgnore="true" attribute to the root view of that layout file:

<LinearLayout
...
tools:viewBindingIgnore="true" >
...
</LinearLayout>

On each binding class, view binding exposes three public static functions to create a binding an object:

  • inflate(inflater) – Use this in an Activity onCreate where there is no parent view to pass to the binding object.
  • inflate(inflater, parent, attachToParent) – Use this in a Fragment or a RecyclerView Adapter (or ViewHolder) where you need to pass the parent ViewGroup to the binding object.
  • bind(rootView) – Use this when you’ve already inflated the view and you just want to use view binding to avoid findViewById. This is useful for fitting view binding into your existing infrastructure and when refactoring code to use ViewBinding.

Then follow this for your activity/view/fragment class

  1. Remove all imports from kotlinx.android.synthetic.
  2. Change all view references to use the binding class instance instead of synthetic properties:

View Binding in Activity

This is the way how you can use View Binding in your activity.

override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityExampleBinding.inflate(layoutInflater)binding.title.text = "Hello"binding.subtext.text = "Concise, safe code"binding.button.setOnClickListener { /* ... */ }setContentView(binding.root)}

View Binding in Fragment

This is the way how you can use View Binding in your Fragment.

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = ExampleBinding.inflate(inflater, container, false)
return binding.root
}

View Binding in Custom View

This is the way how you can use View Binding in your Custom view.

private lateinit var binding: ViewExampleBinding
override fun onFinishInflate() {
super.onFinishInflate()
binding = ViewExampleBinding.bind(this)
}

What code does it generate actually?

  • View binding generates a Java class that replaces the need for findViewById in your code. It will generate one binding object for every XML layout in your module
  • When editing an XML layout in Android Studio, code generation will be optimized to only update the binding object related to that XML file, and it will do so in memory to make things fast. You don’t have to wait for a full rebuild.
  • View binding doesn’t do any logic– it just exposes your views in a binding object so you can wire them up without error-prone calls to findViewById.
  • Since all properties are annotated with @Nullable or @NonNull Kotlin knows how to expose them as null-safe types.

What about included layouts

  • One binding object will be generated for each layout.xml in a module. This is true even when another layout <include>s this layout.
  • Note that the <include> tag has an id: android:id="@+id/includes". This is required for view binding to generate a property

TL;DR

  • Enable view binding in build.gradle (no libraries dependencies)
  • View binding works with existing XML, and will generate a binding object for each layout in a module.
  • View binding generates a binding object for every layout in your module (activity_awesome.xml → ActivityAwesomeBinding.java)
  • Binding object contains one property for every view with an id in the layout — with the correct type and null-safety
  • Android Studio is optimized to update the binding objects immediately when editing XML layouts.
  • Full support for both the Java programming language and Kotlin
  • Faster compilation: View binding requires no annotation processing, so compile times are faster.
  • Ease of use: View binding does not require specially-tagged XML layout files, so it is faster to adopt in your apps. Once you enable view binding in a module, it applies to all of that module’s layouts automatically.

https://stackoverflow.com/questions/58554751/java-lang-nullpointerexception-missing-required-view-with-id

https://issuetracker.google.com/issues/143729912

https://issuetracker.google.com/issues/143683987

Build time

I couldn’t find that much difference in build time it’s almost the same.

APK size

Apk size is a bit higher(Alpha build) for view binding enabled apk and it’s because of the java code generated for the related xml during compilation.

Let me know if you face any issues or any feedback. Appreciate your claps and comments.

--

--

Md Ali Hossain

Senior Android Engineer @Delivery Hero | Android developer | Kotlin lover | Flutter explorer | Problem Solver