Data binding setOnClickListener not called
I making the Android Kotlin tutorial on Udacity, but my code does not work as expected. Please take a look on my code why does not work. The problem is the binding.doneButton.setOnClickListener not running.
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
setContentView(R.layout.activity_main)
binding.doneButton.setOnClickListener {
// This code does running never
Log.i("LOG_ME", binding.doneButton.text.toString())
addNickName(it)
}
}
private fun addNickName(view:View) {
}
}
I added the <layout> to my activity_main.xml and the 3 lines to build.gradle:
dataBinding {
enabled = true
}
Make sure you rebuild the project every time you change something when using data binding because you need to generate code for every change you make. If you are using Android Studio do a project rebuild. Also remove setContentView(R.layout.activity_main) You don't need this line since you're using databinding.
Also, your xml button should look something like this:
<Button
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/done_button"
style="@style/Widget.AppCompat.Button.Colored"/>
Your Kotlin class onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.doneButton.setOnClickListener {
addNickname(view:View)
}
No comments:
Post a Comment