<?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 Philip Andrews on Medium]]></title>
        <description><![CDATA[Stories by Philip Andrews on Medium]]></description>
        <link>https://medium.com/@philandrews1?source=rss-7c7d7d4beef3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6jD2IKn8BbHXt4_Pt3Z4cw.jpeg</url>
            <title>Stories by Philip Andrews on Medium</title>
            <link>https://medium.com/@philandrews1?source=rss-7c7d7d4beef3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 19:51:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@philandrews1/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 Data Binding - "Hello World"]]></title>
            <link>https://medium.com/@philandrews1/android-data-binding-hello-world-fb7086a12e6?source=rss-7c7d7d4beef3------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb7086a12e6</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Philip Andrews]]></dc:creator>
            <pubDate>Mon, 14 May 2018 14:50:05 GMT</pubDate>
            <atom:updated>2018-05-14T15:46:38.322Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android Data Binding — “Hello World” (part-1)</h3><p>Data binding is one of the wonders of the Android world. Take your first look into it, and you’re lost! You may struggle to understand where and how it works. After the tenth time looking at it, something switches on…ITS ALIVE!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl41lI4bYmcsPJX9Go%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl41lI4bYmcsPJX9Go%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl41lI4bYmcsPJX9Go%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/95f130925d09f0e7d4b5e92b433fec7e/href">https://medium.com/media/95f130925d09f0e7d4b5e92b433fec7e/href</a></iframe><h3>Start With Gradle</h3><p>Through the magical world of Gradle, enabling data binding is really simple. All that is needed is dataBinding.enabled = true in the Gradle Module file under android.</p><pre>android {<br>   dataBinding.enabled = true<br>}</pre><p>To enable data binding with kotlin, you need three entries in the Gradle Module file.</p><ol><li>apply plugin: &#39;kotlin-kapt&#39;</li><li>android { ...... dataBinding.enabled = true }</li><li>dependencies { ...... kapt &quot;com.android.databinding:compiler:3.2.0-alpha10&quot; }</li></ol><h3>What is data-binding?</h3><p>Data binding is the ability to bind UI components in your layouts to data sources in your app. Layouts are often applied to the activity class and UI elements are obtained by using ‘findViewById’. Data binding sets the data directly in the layout file and removes the need to use ‘findViewById’.</p><pre>&lt;TextView android:text=&quot;@{helloWorldViewModel.helloWorld}&quot;/&gt;</pre><p>It’s as simple as that…Or is it?</p><p>That is the simplest way of binding the data from the viewModel to the view.</p><p>But where is viewModel actually defined?</p><p>View model is defined at the top of the layout file, under a data variable tag.</p><pre>&lt;data&gt;<br>  &lt;variable <br>    name=&quot;helloWorldViewModel&quot;<br>    type=&quot;com.package.name.HelloWorldViewModel&quot; /&gt;<br>&lt;/data&gt;</pre><p>The data tags tell the layout/bindings what variables are part of the binding class. Variables can be anything from simple strings to view models.</p><p>So, what is the structure behind the layout file?</p><h3>Layouts &amp; Binding Expressions</h3><p>Data binding expressions sit differently in the xml layout file. Normally, the layout file would begin with a root tag of either ConstraintLayout, LinearLayout, etc... When data binding is brought into the equation, the root tag begins &amp; ends with &lt;layout&gt; &lt;/layout&gt;. This structure usually follows the &lt;data&gt;tag and then the normal view tags.</p><pre><em>&lt;?</em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<em>?&gt;<br></em>&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br><br>    &lt;data&gt;<br>      &lt;variable<br>        name=&quot;helloWorldViewModel&quot;<br>        type=&quot;com.package.name.HelloWorldViewModel&quot;/&gt;<br>    &lt;/data&gt;<br><br>    &lt;LinearLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:orientation=&quot;vertical&quot;&gt;<br><br>        &lt;TextView<br>            android:layout_width=&quot;wrap_content&quot;<br>            android:layout_height=&quot;wrap_content&quot;         android:text=&quot;@{helloWorldViewModel.helloWorld}&quot;/&gt;<br><br>    &lt;/LinearLayout&gt;<br>&lt;/layout&gt;</pre><p>The above layout now shows that the core tags are layout with the data tags and LinearLayout tags being held within.</p><h3>Generating Binding Classes</h3><p>By default, after a clean and build, the data binding library recognises the layout file as a binding class, and creates binding variables that are used within the class structure. The name of the class is based on the name of the layout file (i.e. activity_main would be ActivityMainBinding). This binding class holds all the variables defined in the data container tags within the layout.</p><p>When creating the MainActivity class, we call our onCreate function as normal, but we don’t have to call setContentView(R.layout.activity_main). Inside the class file, we can now write the following</p><pre>class MainActivity: AppCompatActivity() {<br>    <br>  override fun onCreate(savedInstanceState: Bundle) {<br>    super.onCreate(savedInstanceState)<br>    var binding = ActivityMainBinding.inflate(<br>                              LayoutInflater.from(this))<br>    setContentView(binding!!.root)<br>  }<br>}</pre><p>This is setting the activity_main.xml to the class allowing us to obtain variables from the layout. This makes working with UI elements and data a lot easier. No more findViewById or setLayout as everything is controlled by the binding class.</p><p>Binding a view model to the view. Well first, we need to create a simple view model class. Nothing massive, lets just start with the simple HelloWorld (since that has been shown in all the examples so far). A view model looks like the following:</p><pre>class HelloWorldViewModel: ViewModel() {<br>    var helloWorld = &quot;HelloWorld&quot; <br>}</pre><p>Right now we don’t need any functions in the view model, as all we want to do is display “Hello World” in the TextView.</p><h3>Binding the HelloWorldViewModel</h3><p>So we have our view model, layout file and MainActivity class file. To bind the view model to the layout, we need to go back to the MainActivity class file and add one line to the onCreate function.</p><pre>class MainActivity: AppCompatActivity() {<br>    <br>  override fun onCreate(savedInstanceState: Bundle) {<br>    super.onCreate(savedInstanceState)<br>      var binding = ActivityMainBinding.inflate(<br>                              LayoutInflater.from(this))<br>      setContentView(binding!!.root)<br>      binding.helloWorldViewModel = HelloWorldViewModel()<br>  }<br>}</pre><p>That is it! we have passed the instance of HelloWorldViewModel to the binding, simply by pulling the helloWorldViewModel variable out of the binding and setting it to a new instance of the HelloWorldViewModel class.</p><p>Now when we run the project, the app will generate all the binding variables it needs and take helloWorld from the HelloWorldViewModel class and display it in the layout.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F2xPPojqe3mraUXS6dk%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fworld-hello-text-2xPPojqe3mraUXS6dk&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F2xPPojqe3mraUXS6dk%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1c735afb0e4c853943593adb09c13d5/href">https://medium.com/media/b1c735afb0e4c853943593adb09c13d5/href</a></iframe><p>I hope this helps you get a better understanding on where to start with data binding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xZNDf9D0utlRpxRqTBmOxA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb7086a12e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>