Ad

Child Clickable/parent Longclickable

I have a FrameLayout with TextView in it.

<FrameLayout
    android:id="@+id/fl_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackground">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:linksClickable="true"
        android:autoLink="email|web|phone"/>
</FrameLayout>

My TextView has autoLink enabled for email/web/phone, as you can see. But my FrameLayout has LongClickListener in code.

flContainer.setOnLongClickListener { 
        // some work
    }

But when I try to longtap to parent FL though the child TextView, it doesn't work, because TV intercepts click and doesn't allow parent to get longclick. I know the simple solution - just apply the same longClickListener to TextView, but I have android:background="?attr/selectableItemBackground" on my FL, so it should have ripple effect when I click it, but TV consumes it. Is it possible to achieve this?

Ad

Answer

Use android:addStatesFromChildren="true" in your parent layout. As mentioned in the documentation,

Sets whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.

You can find about it here:

android.view.ViewGroup

Ad
source: stackoverflow.com
Ad