Reputation: 3329
I'm struggling with Android's Data Binding in Android Studio. I get two error messages in the IDE and I don't know how to solve these errors: Cannot resolve symbol 'BR'
and Cannot resolve symbol @{data.visible ? View.VISIBLE : View.GONE}
.
my.namespace
, package namespace.my.databindingtest
)dataBinding { enabled = true }
to the app module's build.gradle file (and did gradle sync)Data
class which extends BaseObservable
and has the properties text
(String) and visible
(Boolean)activity_main.xml
: Wrapped in layout
tag, added data
section and used data.text
and data.visible
properties.MainActivity
classbuild.gradle (Module: app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "namespace.my.databindingtest"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
Data.java (package namespace.my.databindingtest
):
package namespace.my.databindingtest;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
public class Data extends BaseObservable {
private String text;
private boolean visible;
public Data(String text, boolean visible) {
this.text = text;
this.visible = visible;
}
@Bindable
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
notifyPropertyChanged(BR.text);
}
@Bindable
public boolean getVisible() {
return this.visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
notifyPropertyChanged(BR.visible);
}
}
activity_main.xml (res/layout):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="data" type="namespace.my.databindingtest.Data"/>
<import type="android.view.View"/>
</data>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="namespace.my.databindingtest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.text}"
android:visibility="@{data.visible ? View.VISIBLE : View.GONE}"/>
</RelativeLayout>
</layout>
MainActivity.java (package namespace.my.databindingtest
):
package namespace.my.databindingtest;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import namespace.my.databindingtest.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Data data = new Data("Hello world!", true);
binding.setData(data);
}
}
In the Data.java
file, BR
is highlighted in red with the hint Cannot resolve symbol 'BR'
:
In the activity_main.xml
file, @{data.visible ? View.VISIBLE : View.GONE}
is highlighted in red with the hint Cannot resolve symbol @{data.visible ? View.VISIBLE : View.GONE}
:
So how can I solve these errors?
Upvotes: 4
Views: 2839
Reputation: 763
You can do your UI logic in MV(ModelView).I mean Data.java file
public class Data extends BaseObservable {
private String text;
private boolean visible;
public Data(String text, boolean visible) {
this.text = text;
this.visible = visible;
}
@Bindable
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
notifyPropertyChanged(BR.text);
}
@Bindable
public boolean getVisible() {
return this.visible ? View.VISIBLE : View.GONE;
}
public void setVisible(boolean visible) {
this.visible = visible;
notifyPropertyChanged(BR.visible);
}
}
In your xml file you can add it like this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.text}"
android:visibility="@{data.visible}"/>
Please dont do your UI logic in view please do it in MV.
Upvotes: 0
Reputation: 19
In
Android Studio 2.1 Preview 5
Build #AI-143.2730271, built on March 31, 2016
JRE: 1.7.0_80-b15 amd64
JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation
this 3 steps helped me to recover:
Upvotes: 0
Reputation: 21
I had the same problem with BR symbol. I "solved" it by just restarting Android Studio :/ (I'm a bit new to Android and IntelliJ) It seems like that something failed when processing the @Bindable that should create the BR symbols...
Upvotes: 2
Reputation: 1
try to change RelativeLayout to LinearLayout. I have also problems to get the data binding ready to run but I think I've read somewhere that RelativeLayout is not supported yet
Upvotes: 0