<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.text.Html"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="#{Html.fromHtml(`some message from model` + ` `)}"/>
</RelativeLayout>
</layout>
Here I am trying to leave some blank space after text in TextView, but it is showing this error.
Error:Execution failed for task ':app:dataBindingProcessLayoutsDebug'.
The entity "nbsp" was referenced, but not declared.
I have tried with simple space in xml, but it doesn't leave blank spaces android:text="#{'some message from model' + ' '}"
Remove the #nbsp and try with it hexa(decimal) value  .
because in XML the main five entity are below :
quet("), amp(&), apos('), It(<) and gt(>) In which the #nbsp to not there so you need to use it hexa(decimal) value  .
If We have to put Space in XML using HTMl we use   But in Xml it give this error :
The entity "nbsp" was referenced, but not declared.
Because the Main five entity of XMl is I define Upper so you have to use it hexa(decimal) value..
so   you have to  . but again & is XMl Entity so you have convert & to & so it will work.
In SHORT :
  means  
Related
vm has a isWin value which can have only 0 or 1.
I'd like to take advantage of Data Binding for this.
I access vm in the <data> and I connected like this:
#{vm.isWin == 1 ? #drawable/win_true : #drawable/win_false}
However, this seems doesn't work.
I get this with red underline:
'!=', '%', '==', '(', ..., '>=', '>>' or '>>>' expected, got ':'
This is reference by Google.
full activity_main.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="vm"
type="io.monolabs.asscnfc.vm.ResultViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src = "#{vm.isWin == 1 ? #drawable/win_true : #drawable/win_false}"/>
</LinearLayout>
</layout>
I had also face this issue. resolved by removing 'is' keyword from variable name.javabeans trying generate same name getter and setter. As 'is' comes before method name only.
I want my view visibility to be dependent on condition behaviour so I am using ObservableField and with databinding trying to change view visibility but getting issue like "Identifiers must have user defined types from the XML file. InputType is missing it"
Code:
Kotlin File
var showView: ObservableField<Boolean>? = ObservableField(false)
//API call response
showView.set(true)
Layout File:
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="#{viewModel.showView ? View.VISIBLE : View.GONE}"/>
How to apply databinding with Observablefield of type boolean? I have used it for string text also and it's working but not working with boolean conditional statement.
I am not sure if that's the case here, but this error message is usually displayed when you reference a type in your binding expressions that hasn't been declared in the <data> section of your layout. The same way you declare the View type as an import, you should declare the type InputType.
<data>
<!-- Maybe an import for InputType is missing here? -->
<import type="android.view.View" />
<variable
name="viewModel"
type="com.yourpackage.YourViewModel"/>
</data>
In my task app I want to use databinding for the task overview. I've grouped tasks in three categories: overdue, today and future.
My task_overview.xml defines the following viewmodel (which contains three arraylists with the different tasks)
<data>
<variable
name="viewModel"
type="de.taskapp.ui.taskOverview.viewmodel.TaskOverviewViewModel" />
</data>
A few steps later I include the layout for the three task lists with the include tag like this
<include layout="#layout/layout_task_list"
android:id="#+id/overdued_tasks_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/headline"/>
<include layout="#layout/layout_task_list"
android:id="#+id/todays_tasks_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/overdued_tasks_list"/>
<include layout="#layout/layout_task_list"
android:id="#+id/future_tasks_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/todays_tasks_list"/>
It would be awesome if I could do something like this to pass the different lists from the viewmodel to the layout_task_list.xml via the include tag
app:entries="#{viewModel.todaysTasks}" // for the todays task list case
My layout_task_list.xml looks like this
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="tasks"
type="android.databinding.ObservableArrayList" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/subheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/SubheadlineWithBackground"
tools:text="Heute"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/spacing_large"
android:layout_marginRight="#dimen/spacing_large"
android:nestedScrollingEnabled="false"
app:entries="#{tasks}" //recyclerviewbinding is already there
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="#id/subheading"/>
</android.support.constraint.ConstraintLayout>
As you may suggest...the code is not working. This error message is shown
data binding error ****msg:Cannot find the setter for attribute 'app:entries' with parameter type android.databinding.ObservableList<de.taskapp.data.entity.Task> on de.molske.einfachmachen.databinding.LayoutTaskListBinding. file:/Users/app/src/main/res/layout/layout_task_overview.xml loc:38:40 - 38:60 ****\ data binding error ****
Okay yeah its telling me that he cannot find the setter for app:entries...but isn't there a "beautiful" way to pass the three lists to the three include-layouts? I don't like the idea to copy and paste the same layout three times just to be able to pass the different lists.
Thanks in advance.
(ps.: I know that there is the possibility to solve this by java / android code but I would like to know if there is a databind way to do this ;-)
Okay, classic me. After a few more googling I found the solution. Adding the next lines to the data-section of the included file everything works fine!
<import type="java.util.List"/>
<import type="de.molske.einfachmachen.data.entity.Task"/>
<variable name="title" type="java.lang.String"/>
<variable
name="tasks"
type="List<Task>"/>
It seems that it's important to define the exact type of the tasks variable. Because its type is a genereic < and > instead of < and > for defining the type.
I have a spinner now and I want to load the data to the spinner with data binding feature. I tried to find the way to declare the string array in the XML(https://developer.android.com/topic/libraries/data-binding/index.html#expression_language) but no satisfied result is found.
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:apps="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.util.SparseArray"/>
<variable
name="statuslist"
type="SparseArray<String>"/>
</data>
...
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spn_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/spinner1"
android:layout_below="#+id/btn2"
android:entries="#{statuslist}"
apps:adapter="#{statusAdapter}"/>
</layout>
When it is build, the following error is shown.
Error:Execution failed for task ':app:dataBindingProcessLayoutsDebug'.
org.xml.sax.SAXParseException; systemId: file:/C:/Users/../app/build/intermediates/res/merged/debug/layout/testdialog.xml; lineNumber: 24; columnNumber: 30; The value of attribute "type" associated with an element type "null" must not contain the '<' character.
However, there is something confused.
When I tried to use SparseArray as shown on website. I found a "Cannot resolve sysmbol" in Android Studio XML Editor.
I do not know is it the correct way to setup the array value for the spinner since there is no official instructions for the spinner data bindingin the website.
have you import the SparseArray ?
try this :
<data><import type="android.util.SparseArray"/></data>
This is a very simple way to setup a spinner with a string-array from your resource's array.xml
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_array_items"/>
The spinner_array_items should contain a list of items to be found in the spinner dropdown.
Change SparseArray<String> to SparseArray<String>.
Well my implementation logic is based on your's but used ArrayList instead SparseArray.
XML:
<data>
<import type="java.util.ArrayList" />
<variable
name="cities"
type="ArrayList<String>"
/>
</data>
<!-- Location -->
<Spinner
android:id="#+id/dd_city"
android:entries="#{cities}"
style="#style/dropdown"
/>
Now, we just need to bind our ArrayList to cities variable through Binding Class.
At least data will be bind : ).
TC.
I have created a custom bindingAdapter in android and when i pass in the color i want the color to change, this is actually for a test im working on just to make sure it works. Here is the code:
here is my view Model for the data binding:
public class User {
public ObservableInt visible;
public User(int visible) {
this.visible=new ObservableInt(visible);
}
#BindingAdapter({"app:bindColor"}) //notice the bindColor custom attribute
public static void setTextColor(TextView view,String color) {
if("green".equals(color))
view.setTextColor(Color.parseColor("#63f421"));
}
}
Now in my xml file which is binded to this model im expected to pass in a color so that the setTextColor method can use it:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class="MainActivityBinder">
<variable name="user" type="com.example.android.floatingactionbuttonbasic.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_one"
android:text="my first textview"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_two"
android:text="my second textview"
android:visibility="#{user.visible}"
app:bindColor="#{'green'}" //see im passing in the green string here
android:textColor="#android:color/holo_green_dark"/>
</LinearLayout>
</layout>
I am getting the error at runtime time of:
Error:(27, 65) error: package com.example.android.floatingactionbuttonbasic.databinding does not exist
Warning:Application namespace for attribute app:bindColor will be ignored.
Error:(24, 33) Identifiers must have user defined types from the XML file. een is missing it
Error:Execution failed for task ':Application:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
if i take out the bindingAdapter stuff it works perfectly with the other databinding stuff. Its just this custom binding thats not working. My project is titled floatingactionbuttonbasic btw.
I was able to get this to work. The issue was how i was passing in the string. It should have the `` around it.
app:bindColor="#{`green`}"
you can also do this :
app:bindColor='#{"green"}'
But what seems to not be allowed is this notation:
app:bindColor="#{'green'}"
i wrote a blog about it to help others if interested.