I have a custom View
which contains a LinearLayout
in turn containing a TextView
and and an EditText
. When I will try to click on my custom view if I have pressed on the TextView
the LinearLayout
will receive the click but if I have pressed on the EditText
it will not.
Below is a simplified version of my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/md_green_800" />
</LinearLayout>
So when I will press on the EditText
I want the parent view (the LinearLayout
) to receive the click.
I have tried below code:
private void init(Context context, AttributeSet attrs) {
LayoutInflater inflater = LayoutInflater.from(context);
binding = LayoutHeadingEdittextBinding.inflate(inflater, this, true);
TypedArray array = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.headingtext,
0, 0);
if (attrs != null) {
try {
String hint = array.getString(R.styleable.headingtext_field_hint);
String name = array.getString(R.styleable.headingtext_field_name);
String text = array.getString(R.styleable.headingtext_field_text);
Boolean isFocuable = array.getBoolean(R.styleable.headingtext_field_focus, true);
if (!isFocuable) {
binding.edtTitle.setClickable(false);
binding.edtTitle.setFocusable(false);
binding.edtTitle.setFocusableInTouchMode(false);
binding.edtTitle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((View) v.getParent()).performClick();
}
});
// binding.edtTitle.setEnabled(false);
} else {
binding.edtTitle.setClickable(true);
binding.edtTitle.setFocusable(true);
binding.edtTitle.setFocusableInTouchMode(true);
}
binding.edtTitle.setHint(hint);
binding.tvTitle.setText(name);
binding.edtTitle.setText(text);
} finally {
array.recycle();
}
}
}
And this is how i want to use it:
<packagename.customView.HeadingEditText
android:id="@+id/edt_country"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/mobile_or_wechat"
android:onClick="@{()->frag.onCountryClick(countryList)}"
app:field_focus="false"
app:field_hint="@string/country"
app:field_name="@string/country"
app:field_text='@{basicinfo.country_name!=null?basicinfo.country_name:""}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edt_address" />
But it is still not working So i think issue with databinding and click of my customview.