1

The whole thing looks like this:

+---------+---------+
|   Mon   | 10:00AM |
+---------+---------+

It's a LinearLayout with two TextViews. I want click events to go through 'Mon' part and change background of the LinearLayout on click. '10:00AM' still needs to accept separate click events.

The XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="@drawable/button_filled_white"
    android:clickable="true"
    >
    <TextView android:text="MON"
                 android:layout_width="0dp"
                 android:layout_height="match_parent"
                 android:layout_weight="1"
                 android:clickable="false"
                 android:enabled="false"
                 android:focusable="false"
        />
    <TextView android:text="10:00AM"
                 android:layout_width="0dp"
                 android:layout_height="match_parent"
                 android:layout_weight="1"
                 android:id="@+id/txtTime"
        />
</LinearLayout>

To let click events to go thorough 'MON' TextView, I tried setting clickable, focusable and enabled to false in a various combination but still, the background of LinearLayout doesn't change.

If I remove child TextViews, the LinearLayout is clickable and I can see the background changing its color when clicked:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="@drawable/button_filled_white"
    android:clickable="true"
    />

I'm aware of ViewGroup.onInterceptTouchEvent() but I'm looking for a XML way since handling ViewGroup.onInterceptTouchEvent() requires situation specific and view id specific implementations.

Vincent
  • 3,191
  • 3
  • 29
  • 35
  • Check this answer: http://stackoverflow.com/questions/4415528/how-to-pass-the-onclick-event-to-its-parent-on-android – Sam Mar 30 '16 at 00:08
  • Hi @Sam, I read the answer and I see 3 answers in there. For #1 answer, I'm aware of ViewGroup.onInterceptTouchEvent() but because it requires situation&view id specific implementations, so I would like to know if there's a XML way - specifying a view to ignore all touch event or something like that. For #2 answer, I tried setting focusable, clickable to false but it doesn't work in this situation. For #3, calling parent.performClick() is not exactly what I want, as I want down and up and events to occur separately so its background change accordingly. – Vincent Mar 30 '16 at 00:31

2 Answers2

0

you can try to set

android:focusable="false"

to your 'Mon' child view

kalin
  • 3,546
  • 2
  • 25
  • 31
  • Hi @djodjo, I just tried setting focusable, but for some reason that doesn't work although I'm really expecting it to work... what gives? – Vincent Mar 30 '16 at 00:37
  • I did more tests, and it works in some situation but not in others although the layouts are very similar. I will try to find the reason. – Vincent Mar 30 '16 at 00:44
  • I found out why. The linear layout was actually a children of NestedScrollView, so all my previous attempts has failed. – Vincent Mar 30 '16 at 01:05
0

Sorry to answer my own question, but to help people with same problem, I will give more details.

The root cause was NestedScrollView.

I actually composited the LinearLayout inside NestedScrollView and that prevented clickable & focusable = false to work correctly. Setting to true actually works as expected (changing background & etc). But setting to false works only half as expected - ignores click, but doesn't pass click events to parent.

So if anyone is having same problem, take a look at your parent(or parent's parent) container. If it's scrollable container, you will have more challenges.

Vincent
  • 3,191
  • 3
  • 29
  • 35
  • 1
    Did you find any solutions when inside of a NestedScrollView? Running into this problem right now- I have a NSV with a LinearLayout inside it and TextViews inside that. Only the part of the LinearLayout outside the text views is clickable. – Gabe Sechan Apr 04 '17 at 16:35