7
votes

I'm trying to bind the width and height of my view but I can't see where's the problem.

I found this on this google issue

To implement these for your application, create a binding adapter:

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

So I created my Binding Adapter like this :

public class SimpleBindingAdapter {

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

    @BindingAdapter("android:layout_height")
    public static void setLayoutHeight(View view, int height) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }

    //Others methods...
}

And then try to set my width and height like this :

<View
        android:layout_width="@{paramsMessage.width}"
        android:layout_height="@{paramsMessage.height}"
... />

Where paramsMessage.width is a public int attribute.

But I get this error :

Caused by: java.lang.RuntimeException: Binary XML file line #25: You must supply a layout_width attribute. at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:607) at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6761) at android.view.ViewGroup$MarginLayoutParams.(ViewGroup.java:6930) at android.widget.RelativeLayout$LayoutParams.(RelativeLayout.java:1244) at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1084) at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:83) at android.view.LayoutInflater.rInflate(LayoutInflater.java:820) at android.view.LayoutInflater.inflate(LayoutInflater.java:511) at android.view.LayoutInflater.inflate(LayoutInflater.java:415) at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:116) at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:88) at be.standard.appbusiness.tutorials.home.TutorialHomeFragment.onCreateDialog(TutorialHomeFragment.java:35) at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:308) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234) at android.app.Activity.performStart(Activity.java:6258) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)  at android.app.ActivityThread.access$900(ActivityThread.java:172)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:5832)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

  I would appreciate any helps on this, thank you !

4

4 Answers

5
votes

This is late but for anyone who need it i did the following:

I set the layout_width and layout_height to 0dp in my view.

<ImageView
    android:layout_width="0dp"
    android:layout_height="0dp" />

You can use minWidth / minHeight with databinding or create your own properties for setting width and height by binding.

<ImageView
     android:layout_width="0dp"
     android:layout_height="0dp"
     android:minWidth="@{controller.getDeviceWidthDP()}"
     android:minHeight="@{controller.getDeviceWidthDP()}" />

Create the BindingAdapter for each property, here i'm usin Kotlin.

object ViewBindings{
    @JvmStatic
    @BindingAdapter("android:minWidth")
    fun setLayoutWidth(view: View, width: Float) {
        val layoutParams = view.layoutParams
        layoutParams.width = (width * view.resources.displayMetrics.density).toInt()
        view.layoutParams = layoutParams
        view.invalidate()
    }

    @JvmStatic
    @BindingAdapter("android:minHeight")
    fun setLayoutHeight(view: View, height: Float) {
        val layoutParams = view.layoutParams
        layoutParams.height = (height * view.resources.displayMetrics.density).toInt()
        view.layoutParams = layoutParams
        view.invalidate()
    }
}

In this example i use an extension function that gets the device width and height dimensions.

fun Context.getDeviceDimensions(): Pair<Float, Float> {
    var widthHeight = Pair(0.0F, 0.0F)
    resources.displayMetrics.let {
        val dpHeight = it.heightPixels / it.density
        val dpWidth = it.widthPixels / it.density
        widthHeight = Pair(dpWidth, dpHeight)
    }

    return widthHeight
}

And i create a controller class and use the <data> tag for include it in my layout.

class SomeController(val someFragment: SomeFragment){
    fun getDeviceWidthDP(): Float{
        val width = someFragment.context.getDeviceDimensions().first
        return width
    }
}
0
votes

I think that the problem is not with data binding. layout_width and layout_height are attributes which should be given on inflate time, unless, you'll get this crash. BindingAdapter's methods are called after the view is been inflated already, which is too late :(

0
votes

You can prevent the crash by setting a default value in the xml for the layout_width and layout_height properties.

<View
        android:layout_width="@{paramsMessage.width, default=@dimen/default_width}"
        android:layout_height="@{paramsMessage.height, default=@dimen/default_height}"
... />
-2
votes

Th Binding adapter methods must be in the class that you passed as a data, in your case the code must be in the class of this object paramsMessage