3

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.

  • it may be duplicate https://stackoverflow.com/questions/4297763/disabling-of-edittext-in-android/29433912 – Amjad Khan Mar 21 '18 at 05:04
  • @AmjadKhan, no it's not. Disabling EditText is not the same thing with passing click actions to the parent view – Farid Oct 04 '19 at 07:03

4 Answers4

2

Programatically

//disable click action
editText.setFocusable(false);
editText.setEnabled(false);


//enable click action
editText.setFocusable(true);
editText.setEnabled(true);

xml:

android:focusable="false"
android:enabled="false"
Venky G
  • 180
  • 7
2

Try this in EditText :

 android:focusable="true"
 android:focusableInTouchMode="true" 

Programatically :

editText.setFocusable(false);

===============EDIT===============

 if (!isFocuable) {
                    binding.edtTitle.setClickable(false);
                    binding.edtTitle.setFocusable(false);
                    binding.edtTitle.setFocusableInTouchMode(false);
                    binding.edtTitle.setOnTouchListener(null);
                    binding.edtTitle.setOnClickListener(null);
//                    binding.edtTitle.setEnabled(false);
                } else {

                    binding.edtTitle.setClickable(true);
                    binding.edtTitle.setFocusable(true);
                    binding.edtTitle.setFocusableInTouchMode(true);
                    binding.edtTitle.setOnTouchListener(this);
                    binding.edtTitle.setOnClickListener(this);
}

===============EDIT===============

Add in edittext

android:duplicateParentState="true"

If this not works then try with this line also

android:duplicateParentState="true"
android:clickable="false"
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
0

Here is a work around.

// Inside HeadingEditText.kt
binding.edtTitle.setOnClickListener {
 this@HeadingEditText.performClick()
}

Set the OnClickListener at your Activity

edt_country.edtTitle.setOnClickListener {
  // foo action
}
Myrick Chow
  • 332
  • 1
  • 6
  • 16
0

THIS IS NOT A GOOD SOLUTION

JUST USABLE IN A CUSTOM-VIEW USING A FrameLayout

on your parent (ViewGroup) of contained EditText, override onInterceptTouchEvent method like this:

override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
    return true
}

I use EditText inside a FrameLayout and override the method inside my FrameLayout and all of them used inside a recycler list item (xml)... and Worked fine click listener of recycler root item view...

ultra.deep
  • 1,699
  • 1
  • 19
  • 23