Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dimen resource to layout using Android Binding Library

I'm using Android's binding library and I'm trying to add or remove the margin on a TextView depending on the value of a boolean. If it's true, I want the TextView to have a margin on the right and no margin on the left and if not then the opposite. All the other resources work fine but when I compile the code I get this error about the margins on the TextView: Cannot find the setter for attribute 'android:layout_marginRight' with parameter type float.

Can anyone spot the error?

This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="comment" type="mx.com.corpogas.dataModels.FeedbackComment"/>
        <import type="android.view.View"/>
    </data>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@{comment.isMine ? @drawable/comment_background_white : @drawable/comment_background_green}"
            android:textSize="15sp"
            android:textColor="@{comment.isSent ? (comment.isMine ? @color/colorPrimaryDark : @android:color/white) : @color/unsent_text}"
            android:layout_marginRight="@{comment.isMine ? @dimen/feedback_comment_margin : @dimen/feedback_comment_no_margin}"
            android:layout_marginLeft="@{comment.isMine ? @dimen/feedback_comment_no_margin : @dimen/feedback_comment_margin}"
            android:text="@{comment.content}"/>


</layout>

And this are my margins:

<dimen name="feedback_comment_margin">16dp</dimen>
<dimen name="feedback_comment_no_margin">0dp</dimen>

When I remove the margins the program compiles and runs perfectly.

like image 477
Carlos J Avatar asked Jan 15 '16 18:01

Carlos J


2 Answers

Data Binding for layout properties is not supported though you could technically add them yourself. The problem is these can be easily abused with people trying to animate them. 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);
}
like image 154
Mukesh Rana Avatar answered Oct 21 '22 09:10

Mukesh Rana


Here's an example in Kotlin. Set margins dynamically in ContraintLayout via Android data-binding library. dimens.xml is used because placing values directly in the binding expression result in a syntax error: msg:Syntax error: extraneous input 'p' expecting {}

layout:

<android.support.constraint.ConstraintLayout
     [...]
     android:layout_marginBottom="@{viewModel.isMarginVisible ? @dimen/default_margin : @dimen/no_margin}"
</>

MarginBindingAdapter:

/**
 *  Data Binding adapters for UI margins
 */
@JvmStatic
@BindingAdapter("android:layout_marginTop")
fun ViewGroup.setMarginTopValue(marginValue: Float) =
    (layoutParams as ViewGroup.MarginLayoutParams).apply { topMargin = marginValue.toInt() }
@JvmStatic
@BindingAdapter("android:layout_marginBottom")
fun ViewGroup.setMarginBottomValue(marginValue: Float) =
    (layoutParams as ViewGroup.MarginLayoutParams).apply { bottomMargin = marginValue.toInt() }

@JvmStatic
@BindingAdapter("android:layout_marginStart")
fun ViewGroup.setMarginStartValue(marginValue: Float) =
        (layoutParams as ViewGroup.MarginLayoutParams).apply { leftMargin = marginValue.toInt() }

@JvmStatic
@BindingAdapter("android:layout_marginEnd")
fun ViewGroup.setMarginEndValue(marginValue: Float) =
        (layoutParams as ViewGroup.MarginLayoutParams).apply { rightMargin = marginValue.toInt() }
like image 30
kosiara - Bartosz Kosarzycki Avatar answered Oct 21 '22 10:10

kosiara - Bartosz Kosarzycki