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.
Related
val inputUnderLineColor = MutableLiveData(R.color.red2)
app:backgroundTint="#{viewModel.inputUnderLineColor}"
I want to set the EditText UnderLine color value depending on the state, but I get the following error
Cannot find a setter for <android.widget.EditText app:backgroundTint> that accepts parameter type 'androidx.lifecycle.MutableLiveData<java.lang.Integer>'
How do you solve this ??
You can use it like below ContextCompat.getColor()
<layout>
<data>
<import type="android.support.v4.content.ContextCompat"/>
<variable name="viewModel" type="com.example.myapp.yourObject" />
</data>
...
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{data.text}"
android:textColor="#{ContextCompat.getColor(context, viewModel.inputUnderLineColor)}" />
</layout>
1. Make sure to import ContextCompat as shown above.
2. You can automagically 'context' as a method parameter for ContextCompat.getColor() because it will be automatically resolved to the view's context.
I have a view that uses DataBinding to display data from a ViewModel. The view has a button that is hidden when the view is in landscape mode. At the moment this works by having two layoutfiles, but I would like to only have one as it's a nightmare to refactor. But how to go about this using DataBinding and BindingAdapters?
How do you enable the following bindingexpression for a given view?
android:visibility="#{isLandscapeMode ? View.INVISIBLE : View.VISIBLE}"
EDIT:
My extension property definition (ViewUtils.kt):
val View.isLandscapeMode: Boolean
get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
And my layout:
<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.view.View"/>
<import type="com.example.base.views.ViewUtilsKt" />
<variable
name="isLandscapeMode "
type="java.lang.Boolean"/>
<variable
name="viewmodel"
type="com.example.ui.task.TaskViewModel" />
</data>
...
<ImageButton
...
android:visibility="#{isLandscapeMode ? View.INVISIBLE : View.VISIBLE}"
...
/>
This causes a compile error: Cause: not a valid name: isLandscapeMode
You have to check in your binding adapter which phone orientation is currently on. Here is another post where you can find answer to your question how to detect orientation of android device?
EDIT:
You need to detect orientation and save it as boolean value. Later you have to pass that variable to your adapter which in this case will be boolean.
<data>
<import type="android.view.View"/>
<variable
name="isLandscapeMode"
type="boolean"/>
</data>
For others if they need same behavior. There are two solutions.
First solution (create a BindingAdapter and use it on the UI element of your choice):
#BindingAdapter("bind:visibleInLandscapeMode")
fun View.setVisibleInLandscapeMode(visible: Boolean) {
Timber.d("setVisibleInLandscapeMode() returns ${resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE}")
visibility = if (visible && (resources.configuration.orientation != Configuration.ORIENTATION_LANDSCAPE)) VISIBLE else INVISIBLE
}
In your XML layout:
<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"
xmlns:bind="http://schemas.android.com/apk/lib/com.example">
...
<ImageButton
...
bind:visibleInLandscapeMode="false"
...
/>
Second solution (create a bindingexpression that changes the visibility of the UI element):
<ImageButton
...
android:visibility="#{context.getResources.getConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE ? View.INVISIBLE : View.VISIBLE}"
...
/>
Remember the correct imports:
<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.view.View"/>
<import type="android.content.res.Resources" />
<import type="android.content.res.Configuration" />
...
</data>
...
Btw. be careful when using Databinding with AndroidAnnotation. See issue here and here.
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.
<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  
I am using the latest data binding in android using android studio 2.1.
using the visibility tag as describe in the below code getting an error as
java.lang.RuntimeException: Found data binding errors.
/ data binding error ****msg:Identifiers must have user defined types from the XML file. View is missing it
file:D:\HP\HealthPortal_Android\Code\app\src\main\res\layout\cardview_image_twotextview.xml
loc:68:90 - 68:93
\ data binding error
<TextView
android:id="#+id/card_sub_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/card_title"
android:layout_marginLeft="#dimen/carview_margin"
android:layout_toRightOf="#+id/card_image"
android:text="#{toolsAndTrackersCards.subtitle}"
android:textColor="#color/black"
android:textSize="20sp"
android:visibility="#{toolsAndTrackersCards.subtitle.equals(#string/Empty_String) ? View.VISIBLE : View.GONE}"
/>
Done some google not abel to find the solution. The #string/Empty_String is define as empty string "" in string.xml file. where I am doing wrong.
Android data binding, Radio Button not updating
Add this to your cardview_image_twotextview.xml:
<data>
<import type="android.view.View" />
<!--your variables-->
</data>
To hide a view if the string is empty, use the below expression in data binding
<data>
<import type="android.view.View"/>
<variable
name="item"
type="com.test.model.Item" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{item.title}"
android:visibility='#{item.title.equals("") ? View.GONE : View.VISIBLE}'/>
NOTE: need to use outer single quote string in order to use double quote to
represent the empty string
If you want to check for null and empty, use the below code :
<data>
<import type="android.view.View"/>
<import type="android.text.TextUtils"/>
<variable
name="item"
type="com.test.model.Item" />
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{item.title}"
android:visibility="#{TextUtils.isEmpty(item.title) ? View.GONE : View.VISIBLE}"/>
Zero or more import elements may be used inside the data element.
These allow easy reference to classes inside your layout file, just
like in Java.
you need to import View class inorder to use its properties.
<data>
<import type="android.view.View"/>
</data>
you can also refer official DataBinding guideline.