How to start using view binding //Android Kotlin Dev Diary

First, open your app/build.gradle file

Inside the android{} block of app/build.gradle, add the following:

buildFeatures { viewBinding = true }

Sync project with gradle files.

In your activity or fragment, define the following class-wide property:

Replace the FragmentorActivityNameBinding above with the appropriate binding class name. Tip: The binding class name is based on the XML file’s name. For example, if your XML file name is activity_main.xml, the binding class name should be ActivityMainBinding. Or, if your file name is fragment_main.xml, the binding class name should be FragmentMainBinding.

In your activity, the onCreate function should look like this:

Otherwise, if using view binding in a fragment, your onCreateView should look like this:

Related Posts:

Posts that may be related to "How to start using view binding //Android Kotlin Dev Diary":

Catzie

A Filipino programmer with a variety of interests such as baking, singing, making up silly song/rap lyrics, K-pop, drawing, creating unique dessert flavors, obsessing about finding out how some things works, board games, anime, video games, and forgetting things that usually go in her long list of interests. Running small-time online dessert shops Cookies PH and Catzie's Cakery.

Leave a Reply

Your email address will not be published. Required fields are marked *