Android DataBinding where to get context?

22,350

Solution 1

Thought I should answer instead of putting in a comment. You'll have more options when rc2 is released. In rc1, you can pass the context in a variable to the Binding, then pass it as a parameter to the method. Alternatively, you can create a custom attribute for data binding:

@BindingAdapter({"timeMillis", "dateFlags"})
public static void setDateText(TextView view, int timeMillis, int dateFlags) {
    view.setText(DateUtils.formatDateTime(view.getContext(), timeMillis,
                 dateFlags));
}

And then use it in your TextView:

<TextView ... app:timeMillis="@{timeVar}" app:dateFlags="@{dateFlags}"/>

Solution 2

Also you can do something like this in your view using the current view context as parameter.

...
android:text="@{yourModelHere.yourModelMethodHere(context)}"
...

Solution 3

A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.

In other words, every time you need to pass the context just use "context" as in @{Object.method(context)}.

Share:
22,350
bakua
Author by

bakua

Updated on September 01, 2021

Comments

  • bakua
    bakua almost 3 years

    I have TextView for showing time. I want to use Android's DataBinding plugin.

    For formatting time I am using DateUtils.formatDateTime(context, int, int) method which takes Context instance. Is it possible to get context include element? Or do I have to use old school way?

    Thanks

    • George Mount
      George Mount almost 9 years
      You can pass it as a variable to the data binding. In rc2, you'll have other options related to the DataBindingComponent. Alternatively, you can create a BindingAdapter that operates on multiple attributes.
  • Adam S
    Adam S over 8 years
    Is there a superior solution to this? I have a similar situation where I need a context (I need a localised value for a formatting string) and would like to know how I can have the framework supply it. I've implemented this solution, which works, but loses much of the clarity the databinding gives. Edit: This commit is what I'm working on.
  • George Mount
    George Mount over 8 years
    The context is now available as an automatic variable "context". However, for your specific needs, there is a better solution. String formatting is part of the expression language, so use something like @{@string/myformat(var1, var2)}
  • Adam S
    Adam S over 8 years
    Great, thanks! I'll experiment with that this evening. Is there anywhere I can follow these changes? The docs don't mention this and I'm struggling to find a change log anywhere.
  • dominik4142
    dominik4142 about 8 years
    where did you get that from?
  • epool
    epool about 8 years
    @dominik4142 You can check it out here. A special variable named context is generated for use in binding expressions as needed. The value for context is the Context from the root View's getContext(). The context variable will be overridden by an explicit variable declaration with that name.
  • Hien Nguyen
    Hien Nguyen over 7 years
    @GeorgeMount I haven't understood your answer yet, please help me answer it in my question. Thanks :( stackoverflow.com/questions/42881529/…
  • Muhammad Younas
    Muhammad Younas about 7 years
    is there any special word for activity
  • epool
    epool about 7 years
    @MuhammadYounas there is not a special word for Activity, why would you want your activity in your xml? if you really need it you could cast the context to the activity where the view is living I guess, but not sure if that is a good practice.