Hector Miguel Soto
Hector Miguel Soto

Reputation: 63

dataBinding in Android Studio 3.0

I'm a new in this but this time my Android Studio 3.0 (The lastest version) doesn't create the class ActivityMainBinding for reasons that I don't know. I follow the guide: https://developer.android.com/topic/libraries/data-binding/index.html I add the property

dataBinding.enabled = true 

To the file build.grandle and sync my project and in MainActivity.java I can't create a instance type of MainActivityBinding because it doesn't exist. My file's name is activity_main.xml and thus

MainActivityBinding binding = MainActivityBinding.inflate(getLayoutInflater());

gave a error "Cannot resolve symbol MainActivityBinding"

Upvotes: 0

Views: 1361

Answers (4)

NANDINI MAURYA
NANDINI MAURYA

Reputation: 11

Make sure you add this feature in your build.gradle:

buildFeatures{
    viewBinding true
}

If your class name is MainActivity then:

ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

else if name is ActivityMain then:

MainActivityBinding binding = MainActivityBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

Upvotes: 0

yoAlex5
yoAlex5

Reputation: 34301

Generated classes become available in compile time. Please follow:

  1. Build -> Clean
  2. Build -> Make Project

After these steps you are able to import generated packages

Upvotes: 0

Hector Miguel Soto
Hector Miguel Soto

Reputation: 63

I fixed with the following: 1.- To the file activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- I Added this lines
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
 <layout>-->

2.- Remove

dataBinding.enabled = true 

and sync your project and add it again, sync it with this that class will appear.

Upvotes: 1

R. Hanza
R. Hanza

Reputation: 151

DataBinding class will be generated based on your xml file name. It is clearly mentioned in doc https://developer.android.com/topic/libraries/data-binding/index.html.

By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was main_activity.xml so the generate class was MainActivityBinding

If your xml name is activity_main.xml than DataBinding class name will be ActivityMainBinding.

If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

Upvotes: 2

Related Questions