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 set a onclicklistener to parent and child view:
linearLayout.setOnClickListener(myOnlyhandler);
videoView.setOnClickListener(myOnlyhandler);
I get only click events for video view. What I want is to know if the user clicks into the video view or outside (parent) and do different actions. How do I get this behavior?
Thanks Tata
A while ago I've implemented an Activity
base class that offers such functionality. You simply register a custom OnTouchOutsideViewListener
that gets notified once the user touches outside your view after having installed a listener. Simply call setOnTouchOutsideViewListener
on your activity instance.
You can paste this code into your existing activity class or create a base activity class that you can reuse 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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With