1

I am starting to work with the new data binding API. I want to bind on a TextView a custom property where I change the text and background at once. As far I understood the api there is a @BindingMethod annotation for this, but the documentation is there a little weak.

I want something like this:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="Important"
    tools:background="@drawable/bg_badge_important"
    tools:textColor="#fff"
    android:id="@+id/badge"
    custom:badge="@{item.badge}"/>

Where this item.badge is a string like important or notice. For i18n I cannot use this string directly but this will help me to choose the right text and the right background image.

Can you give me a small example or a reference how to use this attribute? I just found one answer on Stack Overflow, but this does not answer my question.

2
  • You wrote "for the hole time I saved" in your candidate speech. Instead of "whole time". Commented Nov 16, 2015 at 21:37
  • Oh no, not again. Thank you! Commented Nov 16, 2015 at 21:38

1 Answer 1

2

Just create a public static method on your codebase and annotate with @BindingAdapter.

Here is an example for fonts:

https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb

Edit by rekire I ended in using this code:

@BindingAdapter({"bind:badge"})
public static void setBadge(TextView textView, String value) {
    switch(value) {
        case "email":
            textView.setText(R.string.badge_email);
            textView.setBackgroundResource(R.drawable.bg_badge_email);
            break;
        // other cases
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh Boy is the world small, I was in her talk in London, where she show that code too. I'll check that tomorrow.
I added the code I'm using now. Thank you for pointing out that google+ posting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.