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 !
Data Binding Library Part of Android Jetpack. 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.
Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.
Data binding gives you everything that view binding does. So, if you have enabled data binding for the project, just use data binding. " seems kind of silly to have even created viewBinding after data binding has already been established" -- it is a matter of build performance.
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
}
}
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}"
... />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With