Rock
Rock

Reputation: 9

Should we use butterknife or findViewById() with Kotlin Android project as we can directly access views by id

I'm able to access my layout views(like button, TextView, EditText etc) directly inside the activity by their ids defined in layout xml file in Kotlin android project.

So, do we need to use findviewbyId(), or butterknife lib in kotlin android project?

StudentActivity.kt


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val studentViewModel = getStudentViewModel()

        updateButton.setOnClickListener {
            val name = nameEditText.text.toString()
            val age = ageEditText.text.toString()
            val subject = subjectEditText.text.toString()

                studentViewModel.updateStudentRecord(
                    Student(
                        name,
                        Integer.parseInt(age),
                        subject
                    )
                )

    }
}
}```

Upvotes: 0

Views: 1196

Answers (3)

A S M Sayem
A S M Sayem

Reputation: 2080

Nope, here is the the magic of kotlin. Just use your id from the layout file (xml) and directly use it. Like: button.setOnClickListener {} and so on. hope it will help.

Upvotes: 0

Squti
Squti

Reputation: 4487

ButterKnife is an old solution for view binding. It has less boilerplate code than old findviewbyId way but because of annotation processors it impacts build time speed and doesn't provide Null safety and Type safety. A better solution is kotlinx.android.synthetic that you used in your example but it has some problems too. For example, if you set your content view to a layout, then type an id that only exists in a different layout, the IDE lets you autocomplete and add the new import statement. Unless the developer specifically checks to make sure their import statements only import the correct views, there is no safe way to verify that this won’t cause a runtime issue. As everything is global, one has to be careful to make sure that they only use views they are expecting and ignore the autocomplete. DataBinding and ViewBinding are the best solutions for now. They are similar at first look. Both generate binding classes that you can use to reference views directly with support Null safety and Type safety, but there are differences:

  • DataBinding approach needs you to add <layout> tag to your XML layout in order to enable the data binding process
  • ViewBinding doesn’t support layout variables or layout expressions, so it can’t be used to bind layouts with data in XML

ViewBinding is faster than DataBinding in build time because it does n't use annotation processors.

Upvotes: 1

DevSrSouza
DevSrSouza

Reputation: 125

I think you will not use anymore, just if you want? But I believe that is not because of the Synthetic Accessors, it's because of the Data Bindings and the announced this year, View Binding

Upvotes: 0

Related Questions