Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding: How pass Context in xml to method?

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()?

like image 728
Alex Avatar asked Nov 29 '17 08:11

Alex


People also ask

How do you pass context in DataBinding?

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 .

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.

What is XML data binding why it is used explain with example?

XML data binding is the process of representing information in an XML document as an object in computer memory (deserialization). With XML data binding, applications access XML data direct from the object instead of using the Document Object Model (DOM) to retrieve it from the XML file.

What is data binding method?

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.


2 Answers

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)}

like image 79
Jéwôm' Avatar answered Sep 20 '22 16:09

Jéwôm'


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>
like image 27
Morgan Koh Avatar answered Sep 23 '22 16:09

Morgan Koh