I've been following this tutorial here about how to perform view Binding, and nothing seems to work properly, but first it doesn't seem to generate the new class file after modding the module gradle file, like it says it will.
First, I'm not sure how to do the gradle setup. With my browsing of the internet and developer blogs I've come across three different versions of how to put it:
android {
...
buildFeatures {
viewBinding = true
}
or like:
android {
...
buildFeatures {
viewBinding{
enabled = true
}
}
or maybe like:
android {
...
viewBinding {
enabled = true
}
And I've built (or "made", as it were) the project with all of those and nothing seems to happen. I know that the associated layout class file, which, in this case, is just a standard set of layout and class files that comes with making a default project with an empty Activity, is supposed to "generate" a class file that's the name of its associated layout file, written in Pascal format with "Binding" tacked on the end, thus, in this case, the layout file is called activity_main.xml
, so the class file should therefore be MainActivityBinding
(which, I know is backwards, but that's how they had it by default: MainActivity
)
So then, after that, there's supposed to be a getroot()
method to every file in the module that allows binding. But it don't get that either. In fact most of the code mentioned here doesn't work. I get unreferenced variable errors all over the place, which probably stems from the file not generating properly, but I don't really know.
I've got a lot more problems with this set of directions Google gives you, but if this is the main problem, I'll just start here.
So how do I fix this? What am I really supposed to do?
UPDATE: I figured out, through and error, that this is what I'm supposed to put in the gradle file (note: it would not accept viewbinding instead of databinding)
android {
...
buildFeatures.dataBinding = true
But the new file did not generate. What should I do?
activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Also, inb4, here's MainActivity.kt
:
package com.example.bindtestbinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}