{"id":737,"date":"2021-07-23T04:24:31","date_gmt":"2021-07-23T04:24:31","guid":{"rendered":"http:\/\/meatpie\/?p=737"},"modified":"2021-08-09T04:56:41","modified_gmt":"2021-08-09T04:56:41","slug":"weather-app-data-binding","status":"publish","type":"post","link":"https:\/\/snersbots.co.uk\/index.php\/weather-app-data-binding\/","title":{"rendered":"Age Calculator App &#8211; View Binding"},"content":{"rendered":"\n<p>Doing the Google Kotlin training courses on Udacity &#8211; <a href=\"https:\/\/www.udacity.com\/course\/developing-android-apps-with-kotlin--ud9012\">here <\/a>&#8211; they&#8217;re free, why wouldn&#8217;t you. If you use View Binding, you can link the view to the code at compile time &#8211; potentially improving the performance.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>OK so the course is a few years old, and I&#8217;m getting some warnings &#8211; still a good course. Just cross reference for the latest details in the Kotlin language reference here: <a href=\"https:\/\/kotlinlang.org\/docs\/kapt.html\">Using Kapt<\/a><\/p>\n\n\n\n<p>Oh that is out of date as well &#8211; going down a rabbit hole &#8211; try here: <a href=\"https:\/\/stackoverflow.com\/questions\/59390492\/dsl-element-android-databinding-enabled-is-obsolete-and-has-been-replaced-with\">Stack Overflow<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gradle:app<\/h2>\n\n\n\n<p>Which then takes me to here: <a href=\"https:\/\/developer.android.com\/studio\/releases\/gradle-plugin#buildFeatures\">Android Gradle Plugin<\/a><\/p>\n\n\n\n<p>The first thing you need to do is update your app Gradle &#8211; the one that goes build.gradle(:app). You need to add the Kotlin Kapt plugin as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plugins {\n    id 'com.android.application'\n    id 'kotlin-android'\n    id \"org.jetbrains.kotlin.kapt\" \n}<\/code><\/pre>\n\n\n\n<p>Or depending how your project is set up, you can alternatively do the following (same file)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apply plugin: 'kotlin-android'\napply plugin: 'kotlin-kapt'<\/code><\/pre>\n\n\n\n<p>Enable data binding in the app gradle, at the top level.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">android.buildFeatures.dataBinding true<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Layout<\/h2>\n\n\n\n<p>Add a new top level closure to each layout resource. Move the namespace info into this tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>&lt;?<\/em>xml version=\"1.0\" encoding=\"utf-8\"<em>?&gt;\n<\/em>&lt;layout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n        xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n        xmlns:tools=\"http:\/\/schemas.android.com\/tools\"&gt;\n\n    &lt;androidx.constraintlayout.widget.ConstraintLayout ...<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">In the code<\/h2>\n\n\n\n<p>Make a binding variable. The type AgeCalcBinding is derived from the name of the layout file. In this case the layout file is called age_calc.xml.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><em><em>\/**\n * @property binding The binding class is autogenerated from the name of the layout it refers to\n *\/\n<\/em>private lateinit var binding: AgeCalcBinding<\/em><\/code>\n<\/pre>\n\n\n\n<p>Instantiate it at the earliest opportunity:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">super.onCreate(savedInstanceState)\nbinding = DataBindingUtil.setContentView(this, R.layout.<em>age_calc<\/em>)<\/pre>\n\n\n\n<p>Then you can remove all the findViewById variables, and instead access the fields directly with the binding. prefex.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">binding.calculateButton.setOnClickListener <strong>{\n    <\/strong>calculateAge()\n<strong>}<\/strong>\n\nbinding.ageCalculated.setText(getString(R.string.<em>fabulous<\/em>))\n\n<\/pre>\n\n\n\n<p>And here&#8217;s how to do an adapter for an AutoCompleteTextView the binding way.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">val monthNames = <em>resources<\/em>.getStringArray(R.array.<em>months<\/em>)<br>val adapter: ArrayAdapter&lt;String&gt; =<br>    ArrayAdapter&lt;String&gt;(this, android.R.layout.<em>simple_list_item_1<\/em>, monthNames)<br><br>binding.editMonth.<em>threshold <\/em>= 1<br>binding.editMonth.setAdapter(adapter)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Notes<\/h2>\n\n\n\n<p>It turns out to be very easy. Just be aware that you cannot just say binding.field.text = value for EditText. The reason is that EditText is an editable string type which you cannot assign. You have to use setText(value).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Doing the Google Kotlin training courses on Udacity &#8211; here &#8211; they&#8217;re free, why wouldn&#8217;t you. If you use View Binding, you can link the view to the code at compile time &#8211; potentially improving the performance.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8],"tags":[48,49,35,52],"class_list":["post-737","post","type-post","status-publish","format-standard","hentry","category-android","category-software","tag-age-calculator","tag-android","tag-kotlin","tag-kotlin-view-binding"],"_links":{"self":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=737"}],"version-history":[{"count":20,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/737\/revisions"}],"predecessor-version":[{"id":782,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/737\/revisions\/782"}],"wp:attachment":[{"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snersbots.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}