Android - Issue With OnClickListener and OnLongClickListener

I first inflate a Layout and later add a View to it using addView(). The layout has an OnLongClickListener attachted to it.

If I now add an OnClickListener to the inner view, the OnLongClickListener does no longer fire. How can I fix this?

Sample Code:

View someLayout =  getLayoutInflater().inflate(R.layout.some_layout, null);
someLayout.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View arg0) {
            ...
        return true;
        }
});

TextView innerView = new TextView(this);

innerView.setText("JustSomeTextThatDoesNotMatter");
innerView.setOnClickListener(new OnClickListener() {    
    @Override
        public void onClick(View arg0) {
            ...
    );
}
});

((FrameLayout) view.findViewById(R.id.framelayout)).addView(innerView);

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="300dp"
        android:layout_height="wrap_content">
    </FrameLayout>

</LinearLayout>

Any Ideas? This really seems rather odd.

Update: The same thing happens when I use an onTouchListener instead of an onLongClickListener, the OnClickListener in the inner view consumes all touch events.

Is this by design? What I want basically is the same functionality that the ContextMenu provides to a ListView.

Answers


A clickable View consumes all touch events and prevents them from being returned to the parent. Therefore the only way to register for long clicks on the whole area of a layout containing clickable Views is to also register the listener on every clickable View inside the layout.


Need Your Help

Google Play Services in emulator, implementing Google Plus login button etc

android google-plus google-play google-play-services

When integrating a Google Plus login button in an Android app, I'm observing some differences in how the button appears on an actual device and in the emulator. I am aware of the fact that the Google

Custom control structures in Scala?

scala functional-programming control-structure

There are a number of times I've run into a simple pattern when programming in Java or C++ for which a custom control structure could reduce the boilerplate within my code. It goes something like:...