14

Android Studio 3.0.

Here my custom method:

public static int getTileWidthDpInScreen(Context context) {
    // some code here
    return tileWidthDp;
}

here my xml file with data binding code :

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="com.myproject.android.customer.util.GUIUtil" />

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="@{GUIUtil.getTileWidthDpInScreen()}"
            android:layout_height="@dimen/preview_image_height"
            android:scaleType="centerCrop"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />       


    </android.support.constraint.ConstraintLayout>

</layout>

And as result I get error:

e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
data binding error msg:cannot find method getTileWidthDpInScreen() in class com.myproject.android.customer.util.GUIUtil

This error because I'm not pass context in method getTileWidthDpInScreen().

How I get context in xml and pass it in method: getTileWidthDpInScreen()?

4
  • 1
    stackoverflow.com/questions/32374483/… Commented Nov 29, 2017 at 8:55
  • 4
    OK. It's work: 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. So as result here work code: android:layout_width="@{GUIUtil.getTileWidthDpInScreen(context)}" Commented Nov 29, 2017 at 9:08
  • HAPPY TO HELP YOU @alex Commented Nov 29, 2017 at 9:09
  • 3
    Possible duplicate of Android DataBinding where to get context? Commented Nov 29, 2017 at 9:13

2 Answers 2

20

The Android documentation say :

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

So the solution is : android:layout_width="@{GUIUtil.getTileWidthDpInScreen(context)}

Sign up to request clarification or add additional context in comments.

Comments

6

You just have to import <import type="android.content.Context" /> in your xml data imports.

Then, you can access context for your databinding simply with context.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <import type="android.content.Context" />

        <import type="com.myproject.android.customer.util.GUIUtil" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="@{GUIUtil.getTileWidthDpInScreen(context)}"
            android:layout_height="@dimen/preview_image_height"
            android:scaleType="centerCrop"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

1 Comment

It is not necessary to import Context, data binding already adds the variable context to the scope

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.