'Cannot find the setter for attribute 'android:layout_width' with parameter type int on android.widget.ImageView

In app/build.gradle:

dataBinding {
        enabled = true
}
kapt "com.android.databinding:compiler:3.0.1"

In layout I have two images.

I want to set width only for first image.

Here is the XML layout:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <import type="com.myproject.android.customer.util.GuiUtil" />
    </data>
            <ImageView
            android:id="@+id/imageViewPhoto"
            android:layout_width="@{GuiUtil.getTileWidthDpInScreen(context), default=@dimen/preview_image_height}"
            android:layout_height="@dimen/preview_image_height"/>

            <ImageView
            android:id="@+id/imageViewFavorite"
            android:layout_width="28dp"
            android:layout_height="28dp"/>    

</layout>

Here is the adapter's code:

    @BindingAdapter("layout_width")
    public static void setLayoutWidth(View view, int width) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);
}

Here is the method GuiUtil.getTileWidthDpInScreen:

public class GuiUtil {    
    public static int getTileWidthDpInScreen(Context context) {
        // some logic that return int value
   }

But I get this error:

:app:transformDataBindingWithDataBindingMergeArtifactsForDebug UP-TO-DATE
:app:kaptDebugKotlin
e: java.lang.IllegalStateException: failed to analyze: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:layout_width' with parameter type int on android.widget.ImageView.
file:myproject\app\src\main\res\layout\preview_offer_item.xml
loc:26:36 - 26:74
****\ data binding error ****    
    at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57)


Solution 1:[1]

Unfortunately, this syntax doesn't support with Android Databinding by default. You can use @BindingAdapter instead, here is full code below (Kotlin):

    @BindingAdapter("layoutWidth")
fun setLayoutWidth(layout: ViewGroup, dimen: Float) {
    val layoutParams = layout.layoutParams
    layoutParams.width = dimen.toInt()
    layout.layoutParams = layoutParams
}

in your XML layout file use "app:layoutWidth", you can use it even with additional syntax, e.g.:

android:layout_width="match_parent"   //required
android:layout_height="@dimen/dp_102"   //required
app:layoutWidth="@{variable.predict ? @dimen/dp_86 : @dimen/dp_102}"            

Solution 2:[2]

In your XML It should be like this

app:layout_width="@{GuiUtil.getTileWidthDpInScreen(context), default=wrap_content}"

Solution 3:[3]

Try using this for your BindingAdapter:

@BindingAdapter("android:layout_width")

Solution 4:[4]

adding: app:layoutWidth in layout with required values worked for me: like:

android:layout_width="wrap_content"   
android:layout_height="wrap_content"   
app:layoutWidth="@{testBoolean == true ? @dimen/value_1 : @dimen/value_2}"   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Lvov
Solution 2
Solution 3 George Mount
Solution 4 Sumit T