Android View Binding Delegation way

Setup instructions

View binding is available since Android Studio 3.6

android {
buildFeatures {
viewBinding true
tools:viewBindingIgnore="true" >
  • 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.
  1. Remove all imports from
  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() {
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


  • 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 →
  • 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.

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Md Ali Hossain

Md Ali Hossain

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