2

I have a layout like this:

LinearLayout
    RelativeLayout(id = test)
        FrameLayout (From a LIB. id = lib)

I tried to add onTouchListener() to test but it doesn't work.

I tried:

@OnTouch(R.id.test)
public boolean test(View v,MotionEvent e) { NOT WORK }

And i tried a simple listener:

    test.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
NOT WORK
                    return false;
                }
            });

I tried to add listeners to FrameLayout but doesn't work too.

I don't want to modify library. Anyone have idea to use OnTouch event?

<RelativeLayout
    android:id="@+id/camera_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:layout_weight="1">

        <com.flurgle.camerakit.CameraView
            android:id="@+id/camera"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"
            app:ckCropOutput="false"
            app:ckFacing="back"
            app:ckFlash="off"
            app:ckFocus="continuous"
            app:ckJpegQuality="100"
            app:ckMethod="standard"/>

    <TextView

In CameraView:

focusMarkerLayout.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent motionEvent) {
            int action = motionEvent.getAction();
            if (motionEvent.getAction() == MotionEvent.ACTION_UP && mFocus == CameraKit.Constants.FOCUS_TAP_WITH_MARKER) {
                focusMarkerLayout.focus(motionEvent.getX(), motionEvent.getY());
            }

            mPreviewImpl.getView().dispatchTouchEvent(motionEvent);
            return true;
        }
    });

Thanks.


Solution:


LinearLayout
    RelativeLayout(id = test)
        CustomRelativeLayout()
            FrameLayout (From a LIB. id = lib)

Create CustomRelativeLayout

public class CustomRelativeLayout extends RelativeLayout{
    public CustomRelativeLayout(Context context) {
        super(context);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return false;
    }
}

Problem:

Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CameraView:dispatchTouchEvent (return true) STOPPED everything

Solution:

Touch 
-> Activity:dispatchTouchEvent 
-> LinearLayout:dispatchTouchEvent 
-> CustomRelativeLayout:dispatchTouchEvent 
-> CustomRelativeLayout:onTouch
-> LinearLayout:onTouch
-> Activity:onTouch (IT WORK :))
ketom
  • 1,904
  • 4
  • 14
  • 25

2 Answers2

4

Most possibly FrameLayout consumes the touch event. Once the touch event is consumed by any view, that event won't be dispatched to the parent. If the FrameLayout has a click listener attached to it, then the MotionEvent won't reach RelativeLayout.

You can subclass RelativeLayout, override ViewGroup#dispatchTouchEvent(MotionEvent) and track the MotionEvent that will be dispatched to children of that layout (in your case the FrameLayout).

In that case your view hierarchy would be this one:

<LinearLayout>
    <com.example.CustomRelativeLayout>
        <FrameLayout>
    </com.example.CustomRelativeLayout>
</LinearLayout>
azizbekian
  • 53,978
  • 11
  • 145
  • 225
  • I Did my Custom layout with @Override public boolean dispatchTouchEvent(MotionEvent ev) { return false; } but its not work – ketom Jun 14 '17 at 10:15
  • That will only help you to debug `MotionEvent`s and see what's happening, it wasn't supposed to help you in any ways. Also, returning `false` won't allow to dispatch that `MotionEvent` to children, instead return `true` in order to let the `FrameLayout` handle the click. – azizbekian Jun 14 '17 at 10:17
  • The CustomLayouts dispatch is run when i touch the screen. – ketom Jun 14 '17 at 10:23
  • Thank you so much – ketom Jun 14 '17 at 11:35
  • Custom layout solution works but if `dispatchTouchEvent` returns false, `FragmeLayout` can not take touch event. In my scenario `FragmeLayout` contains fragment so I slide the view which over the `FrameLayout`, the fragment can not handle touch events. – Cagdas Jan 06 '21 at 12:52
0

I think your RelativeLayout is overridden by FrameLyout that's why touch is not working. Post your layout xml code

Dhanumjay
  • 522
  • 6
  • 17