1

I'm trying to set a variable (buttonText) of customView from main_activity.xml but I got this error:

attribute buttonText (aka com.example.testbinding:buttonText) not found. error: failed linking file resources.

customView.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >
    <data>
       <variable
            name="buttonText"
            type="String" />
    </data>

    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:text="@{buttonText}" />
    </LinearLayout>
</layout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    <data>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.example.testbinding.CustomView
            android:id="@+id/customView"
            ...
            app:buttonText = "Test button text"
            ...
        >

        </com.example.testbinding.CustomView>
    </android.support.constraint.ConstraintLayout>
</layout>

In this post I found:

In your Custom View, inflate layout however you normally would and provide a setter for the attribute you want to set:

My CustomView class have a public setter for buttonText property:

    public class CustomView extends LinearLayout {

        private CustomViewBinding binding;
        private String buttonText;

        public CustomView(Context context) {
            super(context);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            binding = DataBindingUtil.inflate(inflater, R.layout.custom_view, this, true);
        }

        public String getButtonText() {
            return buttonText;
        }

        public void setButtonText(String buttonText) {
            this.buttonText = buttonText;
        }
    }

Can anyone tell me what's wrong with it? Or it impossible without BindingAdapter?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

binding.setbuttonText("text_button");

instead of

    public String getButtonText() {
        return buttonText;
    }

    public void setButtonText(String buttonText) {
        this.buttonText = buttonText;
    }
Dhara Jani
  • 461
  • 3
  • 10
0

Seems I found what was wrong.

If I use a variable declared in parent view - everything is ok. But if I use a value right in property it's don't work.

wrong:

app:buttonText = "Test button text"
app:progressValue = "50"

correct:

<variable
name="viewModel"
type="com.example.testbinding.ViewModel"/>

app:buttonText = "@{viewModel.buttonText}"
app:progressValue = "@{viewModel.progressValue}"

Of course, viewModel must have a defined setter for these properties.