<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ali Davoudi on Medium]]></title>
        <description><![CDATA[Stories by Ali Davoudi on Medium]]></description>
        <link>https://medium.com/@sowl.mi9?source=rss-5dd0758811d3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*C6NTEwuY1vE2yQL5l5dPEA.jpeg</url>
            <title>Stories by Ali Davoudi on Medium</title>
            <link>https://medium.com/@sowl.mi9?source=rss-5dd0758811d3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 08:34:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sowl.mi9/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android ViewBinding vs DataBinding]]></title>
            <link>https://medium.com/@sowl.mi9/android-viewbinding-vs-databinding-533ad48c36aa?source=rss-5dd0758811d3------2</link>
            <guid isPermaLink="false">https://medium.com/p/533ad48c36aa</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[view-binding]]></category>
            <category><![CDATA[data-binding]]></category>
            <dc:creator><![CDATA[Ali Davoudi]]></dc:creator>
            <pubDate>Sun, 05 Sep 2021 16:33:50 GMT</pubDate>
            <atom:updated>2021-09-05T16:33:50.265Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*h4F96eGEgwjd1Kqg" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Android ViewBinding, Yes or No?</h3><p>ViewBinding is a great tool that in most cases, replaces findViewById, so you can finialy say goodby to <em>null point function</em> from wrong ID casting. But what is the difference between V<strong>iewBinding</strong> and <strong>DataBinding</strong> and what is its pros and cons?</p><h4>ViewBinding vs DataBinding</h4><p>The one and only function of View Binding is <strong>to bind the views in the code,</strong> while Data Binding offers some more options like Binding Expressions, which allows us to write expressions the connect variables to the views in the layout.</p><p>Basically <strong>ViewBinding</strong> only binding views to code, while DataBinding bind data from code to views.</p><p>There are three important differences</p><ol><li>With ViewBinding, the layouts do not need a layout tag</li><li>You can’t use ViewBinding to bind layouts with data in xml (No binding expressions, no BindingAdapters nor two-way binding with ViewBinding)</li><li>The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding’s build time.</li></ol><p>There is nothing ViewBinding can do that DataBinding cannot but it costs longer build times. Remember you don’t need to use both, if you are using DataBinding, there is no need adding ViewBinding.</p><blockquote>View binding is a feature that allows you to more easily write code that interacts with views. 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 — Google</blockquote><h4>So why we shouldn’t use DataBinding frequently when it sounds much more fancy?!</h4><p>As much as it sounds fancy, it is a pretty heavy loaded library which might cause a longer compile time. So if you are not going to use DataBinding only functionalities then might be better to consider ViewBinding as it does have some advantages in terms of build time and apk size.</p><p>In short you can use ViewBinding to replace <strong>findviewbyid()</strong> effectively. If your project is however more complex and you need to add features like binding data to views, binding adapters e.t.c, use DataBinding.</p><p><strong><em>Biggest advantage of ViewBinding is Speed and Efficiency</em></strong></p><h3>So how we should use ViewBinding?</h3><p>There is a couple of things you should do and I try to make it organized and listed: (Based on Android Developers docs from <a href="https://developer.android.com/topic/libraries/view-binding">this link</a> and my personal experiences)</p><h4>Step 1: Adding ViewBinding to your project</h4><p>First you need to use Android Studio 3.6 canary11+ (I’m currently using Android Studio 4.1.2 and it is doing the job well for me)<br>You can find it from <a href="https://developer.android.com/studio/archive">here</a></p><p>Then You need to upgrade your Gradle wrapper to Gradle “5.6.4” and Gradle build tool to “3.6.0-rc01”, higher versions also work so don’t be afraid to be updated<br> distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip</p><pre>dependencies {</pre><pre>        ...<br>        classpath &#39;com.android.tools.build:gradle:3.6.0-rc01&#39;</pre><pre>    }</pre><p>To enable view binding in a module, add the viewBinding element to its build.gradle file, as shown in the following example:</p><pre>android {<br>    ...<br>      viewBinding {<br>        enabled = true<br>      }<br>    }</pre><h4>Step 2: Using ViewBinding in your project</h4><p>If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views that have an ID. The name of the binding class is generated by converting the name of the XML file to camel case and adding the word “Binding” to the end.<br>For example, given a layout file called result_profile.xml:</p><pre>&lt;LinearLayout ... &gt;<br>        &lt;TextView android:id=&quot;@+id/name&quot; /&gt;<br>        &lt;ImageView android:cropToPadding=&quot;true&quot; /&gt;<br>        &lt;Button android:id=&quot;@+id/button&quot;<br>            android:background=&quot;@drawable/rounded_button&quot; /&gt;<br>    &lt;/LinearLayout&gt;</pre><p>The generated binding class is called ResultProfileBinding. This class has two fields: a TextView called name and a Button called button. The ImageView in the layout has no ID, so there is no reference to it in the binding class.</p><p>Every binding class also includes a getRoot() method, providing a direct reference for the root view of the corresponding layout file. In this example, the getRoot() method in the ResultProfileBinding class returns the LinearLayout root view.</p><p>If you want a layout file to be ignored while generating binding classes, add the tools:viewBindingIgnore=&quot;true&quot; attribute to the root view of that layout file:</p><pre>&lt;LinearLayout<br>        ...<br>        tools:viewBindingIgnore=&quot;true&quot; &gt;<br>        ...<br>    &lt;/LinearLayout&gt;</pre><p>To set up an instance of the binding class for use with an activity, fragment or card view adapter perform the following steps:</p><ul><li>in the activity’s onCreate() method:</li></ul><pre>private ResultProfileBinding binding;</pre><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) {<br>    super.onCreate(savedInstanceState);<br>    binding = ResultProfileBinding.inflate(getLayoutInflater());<br>    setContentView(binding.getRoot());<br>}</pre><ul><li>in the fragments’s onCreateView() method:</li></ul><pre>private FragmentHousesBinding binding;</pre><pre>    @Override<br>    public View onCreateView(LayoutInflater inflater, ViewGroup container,<br>                             Bundle savedInstanceState) {<br>        // Inflate the layout for this fragment<br>        binding = FragmentHousesBinding.inflate(inflater, container, false);</pre><pre>        init();</pre><pre>        return binding.getRoot();<br>    }</pre><ul><li>in the card view adapter’s onCreateViewHolder() method:</li></ul><pre>HouseCardPropertyFragmnetBinding binding;</pre><pre>    @Override<br>    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {</pre><pre>        binding = HouseCardPropertyFragmnetBinding.inflate(LayoutInflater<br>            .from(parent.getContext()), parent, false);</pre><pre>        return new Holder(binding);<br>    }</pre><pre>    @Override<br>    public void onBindViewHolder(@NonNull HouseAdapter.Holder holder, int position) {<br>        holder.bindData(getItem(position));<br>    }</pre><pre>    class Holder extends RecyclerView.ViewHolder {</pre><pre>        HouseCardPropertyFragmnetBinding view;</pre><pre>        Holder(@NonNull HouseCardPropertyFragmnetBinding v) {<br>            super(v.getRoot());<br>            view = v;<br>        }</pre><pre>        void bindData(Tag item) {<br>            view.tagTxt.setText(item.Name);</pre><pre>        }<br>    }</pre><p>that’s it you are free from the findViewById from now on.</p><p>If you find this article useful <strong>please don’t hesitate to share it and give applause</strong>. If you have questions or would like to give me some feedback, please comment below or follow my social media ( <a href="https://twitter.com/Mikelanzh">Twitter</a>, <a href="https://www.linkedin.com/in/ali-davoudi-1a87b247/">Linkedin</a>).<br>For more information check my answer and its comments in the link below</p><p><a href="https://stackoverflow.com/a/60072000/12844156">How to use view binding in Android</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=533ad48c36aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>