how to define resource for the float type and use it in the databinding for `layout_constraintWidth_percent`?

constraint layout example
constraint layout guideline
constraint layout in android tutorials point
constraintlayout version
androidx constraintlayout
androidx constraintlayout dependency
objectanimator example

if using

app:layout_constraintWidth_percent="@{vm.type == Type_1 ? 0.76F : 0.79F}"

it works fine. but using

app:layout_constraintWidth_percent="@{vm.type == Type_1 ? @dimen/type_2_percent : @dimen/type_1_percent}"

got crash with

android.content.res.Resources$NotFoundException: Resource ID #0x7f0700ac type #0x4 is not valid

And other items using databinding with resource id work fine as well.

how to define resource for the float type and use it in the databinding for layout_constraintWidth_percent?

Here is the related code:

<dimen name="type_1_width">55dp</dimen>
<dimen name="type_1_height">48dp</dimen>
<dimen name="type_2_width">73dp</dimen>
<dimen name="type_2_height">63dp</dimen>

<!-- define for float -->

<item name="type_1_percent" format="float" type="dimen">0.76</item>
<item name="type_2_percent" format="float" type="dimen">0.79</item>


@JvmStatic @BindingAdapter("app:layout_constraintWidth_percent")
fun setLayoutConstraintWidthPercent(view: View, width: Float) {
    (view.layoutParams as? ConstraintLayout.LayoutParams)?.apply { this.matchConstraintPercentWidth = width }


<layout xmlns:android=""


        <import type="android.view.View" />
            type="com.viewmodel.ListViewModel" />



            android:visibility="@{(vm.count > 0) ? View.GONE : View.VISIBLE}"

                android:layout_width="@{vm.type == Type_1 ? @dimen/type_1_width : @dimen/type_2_width, default=wrap_content}"
                android:layout_height="@{vm.type == Type_1 ? @dimen/type_1_height : @dimen/type_2_height, default=wrap_content}"

                app:layout_constraintWidth_percent="@{vm.type == Type_1 ? @dimen/type_2_percent : @dimen/type_1_percent}"
                    // app:layout_constraintWidth_percent="@{vm.type == Type_1 ? 0.76F : 0.79F}" <=== using float directly works
                android:text="@{vm.type == Type_1 ? @string/type_1_text : @string/type_2_text}"

Floats aren't working. There is no type="float" for dimensions. If you specify type="dimen" then the binding expects a dimension and not a float. (The 0x04 Resource type in the error is "float".)

I think the way to do this is to define your dimensions as fractions as follows:

<item name="type_1_fraction" type="fraction">25%</item>
<item name="type_2_fraction" type="fraction">75%</item>

Then you can use the following in the layout:

app:layout_constraintWidth_percent="@{vm.type == 1 ? @fraction/type_1_fraction : @fraction/type_2_fraction}"

The adapters should work as is.

Recently Active 'android-constraintlayout' Questions - Page 2, how to define resource for the float type and use it in the databinding for ` layout_constraintWidth_percent`? � android-constraintlayout android-databinding . Data binding works well with LiveData that's used with ViewModel objects. Now that you've added data binding to the ViewModel objects, you're ready to incorporate LiveData. In this task, you change the GuessTheWord app to use LiveData as the data-binding source to notify the UI about changes in the data, without using the LiveData observer methods.

@Cheticamp solution works, put the generated binding code here for help understanding why using the type=fraction:

the generated binding code:

val theFloat = ((vmType_1) ? (textBody.getResources().getFraction(R.fraction.type_1_fraction, 1, 1)) : (textBody.getResources().getFraction(R.fraction.type_2_fraction, 1, 1)));

com.utils.BindingAdapterUtils.setLayoutConstraintWidthPercent(this.textBody, theFloat);

The Resources::getFraction():

 public float getFraction(@FractionRes int id, int base, int pbase) {
    final TypedValue value = obtainTempTypedValue();
    try {
        mResourcesImpl.getValue(id, value, true);
        if (value.type == TypedValue.TYPE_FRACTION) {
            return TypedValue.complexToFraction(, base, pbase);
        throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id)
                + " type #0x" + Integer.toHexString(value.type) + " is not valid");
    } finally {

ConstraintLayout, There are currently various types of constraints that you can use: Relative If side margins are set, they will be applied to the corresponding constraints (if they exist) (Fig. will set the maximum size for this dimension; layout_constraintWidth_percent and Load a layout description file from the resources. Chris Hunt wrote: I suppose there might be circumstances where one might use a BINARY_FLOAT or BINARY_DOUBLE, but I can't think of any. BINARY_DOUBLE and BINARY_FLOAT support +Inf, -Inf, and NaN, so you see them in DBs supporting engineering or scientific data, where these values may need to be represented. Also, as they are native, they can give considerable performance improvement during

  1. define your resource as integer:
 <item type="integer" name="button_with_percent" format="float">0.3</item>
  1. apply to your layout:

Property Animation Overview, The ValueAnimator class lets you animate values of some type for the duration of an animation by specifying a set of int , float , or color values� In this example, the data type of the column f1, f2 and f3 is FLOAT(1), FLOAT(4), and FLOAT(7). So the corresponding precision in decimal digits of the column f1, f2, and f3 is 1 (1 * 0.30103), 2 (4 * 0.30103), and 3 (7 * 0.30103). In this tutorial, you have learned about the Oracle FLOAT data type and how to apply it to define float columns

android-constraintlayout, При использовании app:layout_constraintWidth_percent="@{vm.type == Type_1 ? 0.76F и использовать его в привязке данных для ` layout_constraintWidth_percent`? Resources$NotFoundException: Resource ID #0x7f0700ac type #0x4 is not valid define for float --> <item name=“ type_1_percent” format="float"� Define font families and include custom fonts in XML. Saved in res/font/ and accessed from the R.font class. More Resource Types Define other primitive values as static resources, including the following: Bool XML resource that carries a boolean value. Color XML resource that carries a color value (a hexadecimal color). Dimension

android-constraintlayout- Program QA, how to define resource for the float type and use it in the databinding for ` layout_constraintWidth_percent`?. 2020-01-17 android-constraintlayout android- � How to: Create a Binding in Code. 03/30/2017; 2 minutes to read +9; In this article. This example shows how to create and set a Binding in code.. Example. The FrameworkElement class and the FrameworkContentElement class both expose a SetBinding method.

Binding Adapters with Kotlin: Part 1, textString is a String , data binding will look for a method on TextView with method that is used to manipulate how some user defined attributes map In Kotlin, the basic types such as Double , Float , Long , Int , Short and� With both PercentFrameLayout and PercentRelativeLayout being deprected in 26.0.0, you need to use ConstraintLayout to size your TextView with percentage values. ConstraintLayout is really powerful tool to build Responsive UI for Android platform, you can find more details here Build a Responsive UI with ConstraintLayout .

  • Can't you just specify <dimen name="percent_width">0.78f</dimen>?
  • @Cheticamp, it does not compile.
  • it does not compile. with the type="fraction" the "@dimen/type_1_fraction" is not resolved: error: cannot find symbol variable type_1_fraction
  • @lannyf The reference should be @fraction/type_1_fraction not @dimen/type_1_fraction.
  • my bad, it worked with @fraction/type_1_fraction. Interesting the 25% is transformed to float. Thanks!