This is my xml file and as you can see it is nested many times by Linear layout.
What i want to acheive is when I click on the area of llOptionA(First
Linear Layout) i will get notified by a toast.
I have also put a toast on llOptionA.setonclickListener()
But when i click on the text it does nothing.
then I also set onclicklisteners on each of them giving me different toasts -> svTest ,layout_inner ,tvOptionA. and also i clicked everywhere to see which part is showing which toast.
<LinearLayout
android:id="@+id/llOptionA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff00ff"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="5dp" >
<HorizontalScrollView
android:id="@+id/svTest"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/layout_inner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvOptionA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="A - Option A "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
First a LinearLayout does not listen for click events by default even when you set an OnClickListener. You also need to add this attribute to your LinearLayout
llOptionA:Second a click starts at the highest level, TextView
tvOptionA, and works it’s way down until a View consumes this event. So before it reachesllOptionAyour HorizontalScrollView intercepts the click in it’s default OnTouchListener and doesn’t pass it down to yourllOptionA… You can listener for a click event with an OnTouchListener in your HorizontalScrollView to call the appropriate method.Third perhaps this is a simplified layout, but the LinearLayout
layout_inneronly has one child and therefor is not necessary, you can simply use this:Of course
llOptionAonly has one child so you could simplify it more:Addition from comments
Here is how to put it all together: