Check click outside of View

this is my layout:

<?xml version="1.0" encoding="utf-8"?><!-- Style Theme.Transparent -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutVideo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:weightSum="1"
    android:focusable="true"
    android:clickable="true">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.75"
        android:clickable="true"
        android:focusable="true" />

</LinearLayout>

      

I have set onclicklistener to parent and child view:

linearLayout.setOnClickListener(myOnlyhandler);
videoView.setOnClickListener(myOnlyhandler);

      

I only get click events for watching videos. I want to know if the user will click in view or outside (parent) and perform different actions. How do I get this behavior?

Thank you Tat

+3


source to share


1 answer


I recently implemented a base class Activity

that offers this kind of functionality. You just register a custom OnTouchOutsideViewListener

one that is notified after the user touches the appearance after installing the listener. Just call setOnTouchOutsideViewListener

into your activity instance.

You can paste this code into an existing activity class, or create a base activity class that can be reused throughout your project.



class YourActivity extends Activity {

private View mTouchOutsideView;

private OnTouchOutsideViewListener mOnTouchOutsideViewListener;

/**
 * Sets a listener that is being notified when the user has tapped outside a given view. To remove the listener,
 * call {@link #removeOnTouchOutsideViewListener()}.
 * <p/>
 * This is useful in scenarios where a view is in edit mode and when the user taps outside the edit mode shall be
 * stopped.
 *
 * @param view
 * @param onTouchOutsideViewListener
 */
public void setOnTouchOutsideViewListener(View view, OnTouchOutsideViewListener onTouchOutsideViewListener) {
    mTouchOutsideView = view;
    mOnTouchOutsideViewListener = onTouchOutsideViewListener;
}

public OnTouchOutsideViewListener getOnTouchOutsideViewListener() {
    return mOnTouchOutsideViewListener;
}

@Override
public boolean dispatchTouchEvent(final MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        // Notify touch outside listener if user tapped outside a given view
        if (mOnTouchOutsideViewListener != null && mTouchOutsideView != null
            && mTouchOutsideView.getVisibility() == View.VISIBLE) {
            Rect viewRect = new Rect();
            mTouchOutsideView.getGlobalVisibleRect(viewRect);
            if (!viewRect.contains((int) ev.getRawX(), (int) ev.getRawY())) {
                mOnTouchOutsideViewListener.onTouchOutside(mTouchOutsideView, ev);
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}

/**
 * Interface definition for a callback to be invoked when a touch event has occurred outside a formerly specified
 * view. See {@link #setOnTouchOutsideViewListener(View, OnTouchOutsideViewListener).}
 */
public interface OnTouchOutsideViewListener {

    /**
     * Called when a touch event has occurred outside a given view.
     *
     * @param view  The view that has not been touched.
     * @param event The MotionEvent object containing full information about the event.
     */
    public void onTouchOutside(View view, MotionEvent event);
}    

}

      

+8


source







All Articles