I want to create a custom element containing an image view and an edit text.
The code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="@+id/editTextWithIconImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"/>
<EditText
android:id="@+id/editTextWithIconEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_centerVertical="true"
android:textSize="@dimen/text_size"
/>
</merge>
And the view implementation:
public class EditTextWithIcon extends LinearLayout {
private EditText editText;
private ImageView imageView;
public EditTextWithIcon(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.EditTextWithIcon, 0, 0);
String hint = a.getString(R.styleable.EditTextWithIcon_custom_hint);
boolean focusable = a.getBoolean(R.styleable.EditTextWithIcon_focusable, true);
int iconSrc = a.getResourceId(R.styleable.EditTextWithIcon_icon_src, -1);
a.recycle();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.edit_text_with_icon, this, true);
editText = findViewById(R.id.editTextWithIconEditText);
imageView = findViewById(R.id.editTextWithIconImage);
editText.setHint(hint);
editText.setFocusable(focusable);
imageView.setImageResource(iconSrc);
}
public void setText(String text){
editText.setText(text);
}
public String getText(){
return editText.getText().toString();
}
}
The attribute I created like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="EditTextWithIcon">
<attr name="custom_hint" format="string" localization="suggested" />
<attr name="text" format="string" localization="suggested" />
<attr name="focusable" format="boolean" localization="suggested" />
<attr name="icon_src" format="reference" localization="suggested" />
</declare-styleable>
</resources>
Everything works like expected besides the two way binding of the text property. When I try to access it from my view like app:test="@={viewModel.title}"
I receive the following error:
Cannot find the getter for attribute ‘app:test’ with value type java.lang.String
Do you have any idea why the getter method is not recognized but the setter is?