<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
	<channel>
		<title>Use ViewBinding for View Interaction Handling in Android | Infinum</title>
		<atom:link href="https://infinum.com/blog/viewbinding-the-new-standard-for-view-interaction-handling-in-android/feed/" rel="self" type="application/rss+xml" />
		<link>https://infinum.com/blog/viewbinding-the-new-standard-for-view-interaction-handling-in-android/</link>
		<description>Building digital products</description>
		<lastBuildDate>Fri, 03 Apr 2026 12:58:20 +0000</lastBuildDate>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>

					<item>
				<image>
					<url>8094https://infinum.com/uploads/2020/04/viewbinding-the-new-standard-for-view-interaction-handling-in-android-0.webp</url>
				</image>
				<title>ViewBinding – the New Standard for View Interaction Handling in Android</title>
				<link>https://infinum.com/blog/viewbinding-the-new-standard-for-view-interaction-handling-in-android/</link>
				<pubDate>Mon, 06 Apr 2020 12:45:00 +0000</pubDate>
				<dc:creator>Stjepan Banek</dc:creator>
				<guid isPermaLink="false">https://infinum.com/the-capsized-eight/viewbinding-the-new-standard-for-view-interaction-handling-in-android/</guid>
				<description>
					<![CDATA[<p>View Binding shines where many of its popular predecessors fail. Here's why it’s the new best practice in handling view interactions.</p>
<p>The post <a href="https://infinum.com/blog/viewbinding-the-new-standard-for-view-interaction-handling-in-android/">ViewBinding – the New Standard for View Interaction Handling in Android</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</description>
				<content:encoded>
					<![CDATA[<div
	class="wrapper"
	data-id="es-217"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-92">
	</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-95"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-93">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-94'
	>
	<a href="https://developer.android.com/topic/libraries/view-binding"><em>View Binding</em></a> is Google’s new method of referencing views. It generates a binding class for each of your <em>XML</em> layout files, and is used to interact with all views that have an assigned id value in a null-safe and type-safe way.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-98"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-96">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-97'
	>
	In comparison to the well-known methods such as <a href="https://kotlinlang.org/docs/tutorials/android-plugin.html"><em>Kotlin synthetics</em></a>, <a href="https://github.com/JakeWharton/butterknife"><em>Butterknife</em></a> and <a href="https://developer.android.com/reference/android/view/View#findViewById(int)"><em>findViewById</em></a>, it provides a safer and more concise way of handling view interactions inside your views. Before we compare them all side by side, let’s dive in <strong>the features of <em>View Binding</em></strong>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-101"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-99">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-100'
	>
	Advantages and disadvantages of <em>View Binding</em></h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-104"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-102">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-103'
	>
	Some of the most relevant benefits of using <em>View Binding</em> are:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-107"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="lists" data-id="es-105">
	<ul	class='typography typography--size-16-text-roman js-typography lists__typography'
	data-id='es-106'
	>
	<li>Null safety – Properties in the generated class are non-null. In the case of multiple layout versions, e.g. <em>layout file</em> for different screen orientation or size, if some configuration contains an id that is not present in others, the generated property will be nullable.</li><li>Type safety – Binding properties will be correctly typed, even with custom views.</li><li>Interoperability – Generated classes are in Java and are optimized for Kotlin-Java interoperability.</li><li>Injection capability – Generated class can be injected in activity or fragment.</li><li>Speed – There are no impacts on build speed, as it doesn’t use an annotation processor. After the first build with <em>View Binding</em> is enabled, it will dynamically generate new properties. And if you add new view elements to your <em>XML</em>, there is no need to rebuild every time.</li></ul></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-110"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-108">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-109'
	>
	On the other hand, there are also some limitations:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-113"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="lists" data-id="es-111">
	<ul	class='typography typography--size-16-text-roman js-typography lists__typography'
	data-id='es-112'
	>
	<li>Large overhead possibility – It generates classes for each <em>XML</em> by default. This could be a large overhead if <em>View Binding</em> is not used in all classes.</li><li>Multiple binding class instances required – There is no clean way to handle single activity/fragments with multiple layout files, since every generated view binding class is bound to exactly one layout file and its configurations. This means that you have to use more than one binding class instance in your controller to handle more layouts.</li></ul></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-116"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-114">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-115'
	>
	Setup and usage example</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-119"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-117">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-118'
	>
	<em>View Binding</em> is available since Android Studio 3.6. It has to be enabled in your application module’s build.gradle.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-121"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-groovy github-light" data-language="groovy" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">android </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token">     viewBinding</span><span class="token" style="color: #d73a49;">.</span><span class="token">enabled </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">true</span><span class="token">
</span></span><span class="line"><span class="token">     …
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-124"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-122">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-123'
	>
	This setting will enable generation of a binding class for every layout <em>XML</em> file in the module on the next build. However, if you would like to skip generating a class for specific layout, the following tag must be added in root element of that layout.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-126"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-xml github-light" data-language="xml" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">&lt;</span><span class="token" style="color: #22863a;">LinearLayout</span><span class="token">
</span></span><span class="line"><span class="token">        ...
</span></span><span class="line"><span class="token">        </span><span class="token" style="color: #6f42c1;">tools</span><span class="token" style="color: #6f42c1;">:</span><span class="token" style="color: #6f42c1;">viewBindingIgnore</span><span class="token">=</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">true</span><span class="token" style="color: #032f62;">&quot;</span><span class="token"> </span><span class="token">&gt;</span><span class="token">
</span></span><span class="line"><span class="token">        ...
</span></span><span class="line"><span class="token">&lt;/</span><span class="token" style="color: #22863a;">LinearLayout</span><span class="token">&gt;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-129"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-127">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-128'
	>
	The generated binding class is called ResultProfileBinding. This class has two fields: a TextView called name and a Button conveniently called, well, <em>button</em>. The ImageView in the layout has no ID, so there is no reference to it in the binding class.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-132"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-130">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-131'
	>
	If layout includes another layout file with <em>\</em> tag, a property will be generated, holding the included layout generated view binding. The included layout, same as all other generated views, must have an id tag.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-135"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-133">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-134'
	>
	The following code block shows the initialization and usage of the generated <em>View Binding</em> class in activity.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-137"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">ResultProfileActivity.kt</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">private</span><span class="token"> </span><span class="token" style="color: #d73a49;">lateinit</span><span class="token"> </span><span class="token">var </span><span class="token">binding</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ResultProfileBinding</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">override</span><span class="token"> </span><span class="token">fun onCreate</span><span class="token">(savedInstanceState</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Bundle</span><span class="token">) {
</span></span><span class="line"><span class="token">    </span><span class="token" style="color: #005cc5;">super</span><span class="token">.onCreate</span><span class="token">(savedInstanceState)
</span></span><span class="line"><span class="token">    binding </span><span class="token" style="color: #d73a49;">=</span><span class="token"> ResultProfileBinding</span><span class="token">.inflate</span><span class="token">(layoutInflater)
</span></span><span class="line"><span class="token">    </span><span class="token">val </span><span class="token">view </span><span class="token" style="color: #d73a49;">=</span><span class="token"> binding.root
</span></span><span class="line"><span class="token">    </span><span class="token">setContentView</span><span class="token">(view)
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">…
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">And now you can use the binding in the following way</span><span class="token">
</span></span><span class="line"><span class="token">binding.name.text </span><span class="token" style="color: #d73a49;">=</span><span class="token"> viewModel.name
</span></span><span class="line"><span class="token">binding.button</span><span class="token">.setOnClickListener </span><span class="token">{ viewModel</span><span class="token">.userClicked</span><span class="token">() }
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-140"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-138">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-139'
	>
	In addition to the described way of initializing, <em>View Binding</em> can also be injected in activities and fragments, using one of the dependency injection libraries such as <em>Dagger</em>. This makes the “view controller” classes even more cohesive and independent from view initializing.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-143"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-141">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-142'
	>
	Before <em>View Binding</em>, there was no convenient way to achieve this. In order to inject the <em>View Binding</em>, you need to <em>@Provide</em> it in the module of your activity or fragment.<br>The following example shows <em>View Binding</em> injection in activity.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-145"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">@Module</span><span class="token">
</span></span><span class="line"><span class="token">class ViewBindingModule </span><span class="token">{
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Method providing the View Binding class</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #6f42c1;">@Provides</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token">fun viewBinding</span><span class="token">(activity</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ViewBindingActivity</span><span class="token">)</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ActivityViewBinding</span><span class="token">                    
</span></span><span class="line"><span class="token">   { 
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">return</span><span class="token"> ActivityViewBinding
</span></span><span class="line"><span class="token">                 </span><span class="token">.inflate</span><span class="token">(activity.layoutInflater)
</span></span><span class="line"><span class="token">   }
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">This module makes sure the activity contributes itself to injection</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">@Module</span><span class="token">
</span></span><span class="line"><span class="token">interface ActivityModule </span><span class="token">{
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@ContributesAndroidInjector</span><span class="token">(modules </span><span class="token" style="color: #d73a49;">=</span><span class="token"> [ViewBindingModule</span><span class="token">::</span><span class="token" style="color: #6f42c1;">class</span><span class="token">])
</span></span><span class="line"><span class="token">   </span><span class="token">fun viewBindingActivity</span><span class="token">()</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ViewBindingActivity</span><span class="token">
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Activity</span><span class="token">
</span></span><span class="line"><span class="token">class ViewBindingActivity </span><span class="token">: </span><span class="token" style="color: #6f42c1;">DaggerAppCompatActivity</span><span class="token">() {
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">The injected View Binding class</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #6f42c1;">@Inject</span><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">lateinit</span><span class="token"> </span><span class="token">var </span><span class="token">binding</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ActivityViewBinding</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">   </span><span class="token" style="color: #d73a49;">override</span><span class="token"> </span><span class="token">fun onCreate</span><span class="token">(savedInstanceState</span><span class="token">: </span><span class="token" style="color: #6f42c1;">Bundle</span><span class="token">?</span><span class="token">) {
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #005cc5;">super</span><span class="token">.onCreate</span><span class="token">(savedInstanceState)
</span></span><span class="line"><span class="token">       </span><span class="token">setContentView</span><span class="token">(binding.root)
</span></span><span class="line"><span class="token">       </span><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">}
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-148"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-146">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-147'
	>
	Comparison with popular methods of handling view interaction</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-151"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-149">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-150'
	>
	In the following sections, I will try to make the case of why developers should start using <em>View Binding</em> by comparing it to other popular methods of handling view interaction: <strong><em>Kotlin synthetics</em>, <em>Butterknife</em></strong>, and the good old <strong><em>findViewById</em></strong>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-154"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-152">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-153'
	>
	The following is an example of referencing and setting a text in TextView using all four methods.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-156"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">findViewById</span><span class="token">
</span></span><span class="line"><span class="token">val </span><span class="token">textView</span><span class="token">: </span><span class="token" style="color: #6f42c1;">TextView</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">textView </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token">findViewById</span><span class="token">(R.id.textView) </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">This is not null or type safe</span><span class="token">
</span></span><span class="line"><span class="token">textView.text </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">Hello</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Butterknife</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">@BindView</span><span class="token">(R.id.cardTextView)
</span></span><span class="line"><span class="token" style="color: #d73a49;">lateinit</span><span class="token"> </span><span class="token">var </span><span class="token">textView</span><span class="token">: </span><span class="token" style="color: #6f42c1;">TextView</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Initialized by passing the reference of activity/fragment</span><span class="token">
</span></span><span class="line"><span class="token">Butterknife</span><span class="token">.bind</span><span class="token">(</span><span class="token" style="color: #005cc5;">this</span><span class="token">)
</span></span><span class="line"><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">textView.text </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">Hello</span><span class="token" style="color: #032f62;">&quot;</span><span class="token"> </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Not null safe</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Kotlin synthetics - Only supported in Kotlin classes</span><span class="token">
</span></span><span class="line"><span class="token">textView.text </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">Hello</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">ViewBinding</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">lateinit</span><span class="token"> </span><span class="token">var </span><span class="token">binding</span><span class="token">: </span><span class="token" style="color: #6f42c1;">ActivityViewBinding</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">ActivityViewBinding</span><span class="token">.inflate</span><span class="token">(layoutInflater)
</span></span><span class="line"><span class="token">setContentView</span><span class="token">(binding.root)
</span></span><span class="line"><span class="token" style="color: #d73a49;">..</span><span class="token">.
</span></span><span class="line"><span class="token">binding.textView.text </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">Hello</span><span class="token" style="color: #032f62;">&quot;</span><span class="token"> </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Completely null and type safe</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-159"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-157">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-158'
	>
	Syntax</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-162"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-160">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-161'
	>
	Using <em>View binding</em> requires using an instance of generated binding class, which means there is an additional variable in each view reference. On the other hand, you don’t have to create properties for views that you will be using in your activity, as is the case with <em>Butterknife</em> and <em>findViewById</em>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-165"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-163">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-164'
	>
	<em>Kotlin synthetics</em> definitely wins this one, because an extension is created for each view with id tag and it’s referenced just by the id value.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-168"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-166">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-167'
	>
	Code generation</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-171"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-169">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-170'
	>
	In terms of the generated code, <em>Butterknife</em> and <em>ViewBinding</em> behave very similarly. Each creates classes which bind views to properties based on the view’s id. The difference is that the Butterknife-generated class is based on the view controller class passed in the initialization, while <em>View Binding</em> is based on the <em>XML</em> layout file.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-174"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-172">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-173'
	>
	In the case of <em>Butterknife</em>, this can lead to nullability and safety issues as activities and fragments can inflate more than one different layout. <em>Kotlin synthetics</em> generate an extension. In order to reference the view, it must be inflated.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-177"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-175">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-176'
	>
	However, <em>Kotlin synthetics</em> have no way of checking if the right layout is inflated, so referencing a view that is not currently on the screen is allowed and leads to a null pointer exception.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-180"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-178">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-179'
	>
	<em>FindViewById</em> approach is easily the winner of this category, as it doesn’t generate any additional code, even though it does require some additional properties and is far from being safe.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-183"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-181">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-182'
	>
	Type and null safety</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-186"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-184">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-185'
	>
	<em>View Binding</em> is the only member of this group that ensures existence and the right type of every view for a given layout. This is one of the main advantages of <em>View Binding</em>, as it also works for different configurations of the layout. <em>View Binding</em> must be inflated with the <em>layoutInflater</em> from the screen controller, which means it ensures referencing the right layout.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-189"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-187">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-188'
	>
	<em>Butterknife</em> and <em>Kotlin synthetics</em> are not safe in terms of referencing the current layout and this can lead to null pointer exceptions. The same goes for <em>findViewById</em> as it is completely layout-ignorant and uses only the id to reference a view.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-191"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-kotlin github-light" data-language="kotlin" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Example of Kotlin synthetics causing a null pointer exception</span><span class="token">
</span></span><span class="line"><span class="token">…
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">activity_two layout imported</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">import</span><span class="token"> </span><span class="token" style="color: #6f42c1;">kotlinx.android.synthetic.activity_two_synthetics.</span><span class="token" style="color: #005cc5;">*</span><span class="token" style="color: #6f42c1;"> </span><span class="token">
</span></span><span class="line"><span class="token">…
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Inflating the activity_one layout</span><span class="token">
</span></span><span class="line"><span class="token">setContentView</span><span class="token">(R.layout.activity_one)
</span></span><span class="line"><span class="token">…
</span></span><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;">Referencing textView from another layout which is not inflated causes an exception</span><span class="token">
</span></span><span class="line"><span class="token">textView.text </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">Some text</span><span class="token" style="color: #032f62;">&quot;</span><span class="token"> 
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-194"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-192">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-193'
	>
	Java and Kotlin interoperability</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-197"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-195">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-196'
	>
	<em>Kotlin synthetics</em> work only with Kotlin classes, which is a main disadvantage of this approach as there are still many Java Android developers and projects. Other mentioned approaches work with both Java and Kotlin.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-200"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-198">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-199'
	>
	It’s worth noting that <em>View Binding</em> generates Java performance optimized code and outperforms the rest of the group.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-203"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-201"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-202">
	<picture class="image__picture block-media__image-picture">
								
			<source
				srcset=https://infinum.com/uploads/2020/04/viewbinding-the-new-standard-for-view-interaction-handling-in-android-1-1400x779.webp				media='(max-width: 699px)'
				type=image/webp								height="779"
												width="1400"
				 />
												<img
					src="https://infinum.com/uploads/2020/04/viewbinding-the-new-standard-for-view-interaction-handling-in-android-1.webp"
					class="image__img block-media__image-img"
					alt=""
										height="910"
															width="1636"
										loading="lazy"
					 />
					</picture>

	</figure></div></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-206"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-204">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-205'
	>
	Finally, what should you use?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-209"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-207">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-208'
	>
	<em>View Binding</em> and <em>Butterknife</em> have a lot in common in terms of the idea behind the approach. However, <em>View Binding</em> addresses and successfully solves some of the issues of using <em>Butterknife</em>. That is probably why the official <a href="https://github.com/JakeWharton/butterknife">Butterknife Github</a> page recommends switching to <em>View Binding</em>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-212"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-210">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-211'
	>
	Even though it might seem that <em>Kotlin synthetics</em> offers a cleaner way of referencing views, the issues of null and type safety, as well as performance and interoperability issues should be reason enough to go with <em>View Binding</em> instead. As for the <em>findViewById</em> approach, it is legacy and it should stay that way considering all the disadvantages.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-215"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-213">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-214'
	>
	When in need of a safe way of interacting with your views, without having to worry about null and type issues, <em>View Binding</em> is definitely the way to go.</p></div>	</div>
</div>
</div>		</div>
	</div><p>The post <a href="https://infinum.com/blog/viewbinding-the-new-standard-for-view-interaction-handling-in-android/">ViewBinding – the New Standard for View Interaction Handling in Android</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</content:encoded>
			</item>
		
	</channel>
</rss>