4

The xml layout file looks like:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  android:id="@+id/my_rootViewID"
    <LinearLayout>

        <ImageView/>

        <TextView/>

        <ImageView/>

    </LinearLayout>
</FrameLayout>

I have tried this:

my_view = findViewById(R.id.my_rootViewID);
my_view.setOnClickListener( new My_OnClickListener() );

My problem: This works only with the ImageViews, but the TextView is not clickable. I could set OnClickListener specifically to the TextView as well, but I have many TextViews (in other cases), so adding theese listeners would be very slow, and elaborate.

Marton_hun
  • 584
  • 2
  • 7
  • 18
  • Add an android:click attrubute to everything in the view. The reason what you have is failing is that textviews process their own click events for text selection, so the touches aren't passed up the chain. You can try setting android:textIsSelectable=false on the textview, that might make it return false from the touch handler instead of true and make it work. – Gabe Sechan Aug 01 '13 at 16:40
  • add android:addStatesFromChildren="true" to your FrameLayout and try again – Blackbelt Aug 01 '13 at 16:43
  • Add the attribute `android:clickable="true"` to the TextView. – Cornholio Aug 01 '13 at 16:43
  • Do you want to click Listener for whole screen or individually for imageview, textview like this. – Bebin T.N Aug 01 '13 at 16:54
  • I finally noticed that there is a faulty extra line: android:inputType="textMultiLine" in the TextView section. I deleted this line, and that solved the problem. The reason could be something that Gabe Sechan described, maybe. Thank you for your tips. – Marton_hun Aug 01 '13 at 17:11

2 Answers2

2

You can do this:

ViewTreeObserver viewTreeObserver = myView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {...}

From the docs of ViewTreeObserver:

A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change

Ahmad
  • 69,608
  • 17
  • 111
  • 137
0

You can add a transparent view over your layout and set listener for this view. It will intercept all click events.

esentsov
  • 6,372
  • 21
  • 28