5

I have an custom attribute from a custom view defined line this:

<declare-styleable name="ExampleView">
    <attr name="order">
        <enum name="byValue" value="0" />
        <enum name="byKey" value="1" />
    </attr>
    <!-- and some more attributes -->
</declare-styleable>

Android Studio detects this and offers me a autocompletion, which is great. So the xml attribute will look like app:order="byValue". However since I want to use a BindingAdapter from the data binding API, I need to use it with an @ sign like this: app:order="@{byValue}", unfortunately this does not compile.

Then I tried to use a constant which I use internally too like this: app:order="@{com.example.views.ExampleView.ORDER_BY_VALUE}", but this does not compile too. I can just use app:order="@{0}", sure this works because it is defined like that, however it is not intuitive why I am using 0 there.

Any idea how can I write a more readable code to solve this issue?

rekire
  • 47,260
  • 30
  • 167
  • 264
  • I think I got the same issue so to add some information, the error log I get is basically this : "android data binding cannot find setter". And only if I want to use something with an @{}. Hope you will find a solution. – MHogge Jul 14 '16 at 08:13
  • I have to try it later again. Maybe the import tag is the trick. I read something about it. – rekire Jul 14 '16 at 09:01
  • @rekire were you able to find a solution for this? – Emil S. May 19 '20 at 14:56
  • Wow after 4 years a comment to this old question. Sorry I cannot even remember this question. – rekire May 19 '20 at 17:58

1 Answers1

1

It is necessary to create code for the enum values:

object Order {
    const val BY_VALUE = 0
    const val BY_KEY = 1
}

Import the class / object holding these enums to your XML:

<import type="com.example.Order" />

Then you can reference them:

app:order="@{Order.INSTANCE.BY_KEY}"
Miloš Černilovský
  • 3,846
  • 1
  • 28
  • 30
  • Well, I don't know if that is correct, but for an answer which is waiting so long you get the accepted state for that. – rekire Dec 10 '20 at 13:08