mikro098
mikro098

Reputation: 2343

Using set doesn't affect view with Data Binding

I'm trying to change different EditText properties using data binding but it doesn't seem to be working.

custom_edittext.xml:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/descriptionTextEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:ems="12"
            android:inputType="textMultiLine"
            android:text="@{textVar}"/>
    </RelativeLayout>
</layout>

My class extends RelativeLayout

public class CustomEditText extends RelativeLayout {
public CustomEditText(Context context) {
        super(context);
        init(context, null);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
}

and my init:

private void init(Context context, AttributeSet attrs){
         View.inflate(context, R.layout.custom_edittext, this);

         CustomEdittextBinding stomEdittextBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_edittext, null, false);

         Drawable drawableButton = context.getResources().getDrawable(R.drawable.ic_action_name);//context.getResources().getDrawable(R.drawable.ic_action_name);
         customEdittextBinding.setTextVar("new text"); //it should change the text, shouldn't it?
         }

my main activity: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    customEditText = findViewById(R.id.custom);
}

and inside the activity_main.xml I've added my CustomEditText like this but there is an error - The following classes could not be found.

<com.example.xxx.app.CustomEditText
        android:id="@+id/custom"
        android:layout_width="344dp"
        android:layout_height="50dp"
        android:paddingLeft="100dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"></com.example.xxx.app.CustomEditText >

Upvotes: 2

Views: 199

Answers (1)

mikro098
mikro098

Reputation: 2343

Ok, I've solved this problem by adding addView(customEdittextBinding.getRoot()); after instantiating CustomEdittextBinding.

Upvotes: 0

Related Questions