7

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.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Did you ever solved the warning also? `Warning:Application namespace for attribute app:bindColor will be ignored.` – Calin Sep 11 '17 at 04:52
  • Oh, nevermind, this guy solved it https://stackoverflow.com/questions/35313466/android-databinding-custom-binding-adapter-warning – Calin Sep 11 '17 at 04:55

1 Answers1

13

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.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Please mark your answer as answered, so the question doesn't appear anymore on the unanswered tab – Tseng Feb 06 '16 at 13:27