Android: Why can't I give an onClickListener to a VideoView?

AndroidAndroid Videoview

Android Problem Overview


I have written these lines of code:

 mVideoView = (VideoView) findViewById(R.id.video_view);
    mVideoView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("LOG_TAG, click");
        }
    });

However, when I run my application, the click event is never called.

So I wonder, is it impossible to register an OnClickListener on a VideoView? And, if so, why is that the case?

Android Solutions


Solution 1 - Android

use VideoView.setOnTouchListener(..) it works for VideoView

Solution 2 - Android

Here's how I solved the pause/play of VideoViews using onTouch:

// Class variables
private boolean bVideoIsBeingTouched = false;
private Handler mHandler = new Handler();

vvVideo.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (!bVideoIsBeingTouched) {
        bVideoIsBeingTouched = true;
	if (vvVideo.isPlaying()) {
	    vvVideo.pause();
	} else {
	    vvVideo.resume();
	}
	mHandler.postDelayed(new Runnable() {
	    public void run() {
	        bVideoIsBeingTouched = false;
	    }
        }, 100);
    }
    return true;
    }
});

Solution 3 - Android

I know this is old but I used this:

	mVideoView.setOnTouchListener(new View.OnTouchListener()
    {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			Log.i(TAG, "Video 1 clicked, starting playback");
			
			return false;
		}
    });

Solution 4 - Android

I know this is and old question, but here is what I did:

Since setOnClickListener is not been triggered, I created my own class which extends VideoView

public class VideoViewCustom extends VideoView{

and Overrided the onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent ev) {

	if(ev.getAction() == MotionEvent.ACTION_DOWN){
		Log.d(TAG, "ACTION_DOWN");
	}
	
	return super.onTouchEvent(ev);
}

and now I can get the onClick event with the MotionEvent.

Hope this helps someone!

Solution 5 - Android

This is probably long overdue, nonetheless of some help to those who may run into a similar problem. The way I got around this problem was by laying a transparent image view right on top of the video view, then listening to onClick events on the image view, and doing whatever it was I wanted to do with the video view afterwards.

Solution 6 - Android

I realize this is an old question but thought I'd chime in with an easy workaround. I can't answer why this doesn't work - seems like a big oversight in my opinion. But an easy workaround is to place your VideoView as the sole View inside a FrameLayout, and set an OnClickListener on the layout. Not ideal, but it works.

Solution 7 - Android

The VideoView is a wrapper containing MediaPlayer and SurfaceView. You can interact with through MediaController or writing your own SurfaceView and implement onClick events.

Solution 8 - Android

You can well use a button which is transparent on the video view if you want a specific part of the video on touch to do something.

Solution 9 - Android

You can solve this issue through cover layout. I used the linearlayout.

            <LinearLayout
                android:id="@+id/video1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center">
                <VideoView
                    android:id="@+id/video2"
                    android:layout_width="370dp"
                    android:layout_height="180dp"
                    android:layout_gravity="center"
                    app:elevation = "0dp"
                    android:background="@mipmap/video"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:visibility="visible"
                    />

            </LinearLayout>

and try it in source file.

video1.setOnClickListener {
                if(Video.isPlaying) {
                    Video.pause()
                }
                else {
                    Video.start()
                }
}

Solution 10 - Android

You might try Gesture Overlay View

You should be able to overlay this view on top of another view in order to get touch events.

Hope this helps!

Solution 11 - Android

It is possible I have did this with onSetClickListener. And Here's a code for your help:

mVideoView.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Here you put a condition for playing a video when you click on your video view.//
            if(my_video.isPressed())
            {   
                my_video.start();
            } else {
                Toast.makeText(getApplicationContext(), 
                    "Not able to run This Video!", 
                    Toast.LENGTH_LONG).show();
            }
        }
    });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFabianView Question on Stackoverflow
Solution 1 - AndroidnewdevView Answer on Stackoverflow
Solution 2 - AndroidDanView Answer on Stackoverflow
Solution 3 - AndroidnawlrusView Answer on Stackoverflow
Solution 4 - AndroidLarry MustaineView Answer on Stackoverflow
Solution 5 - AndroidMichael MView Answer on Stackoverflow
Solution 6 - AndroidmszaroView Answer on Stackoverflow
Solution 7 - AndroidnahwarangView Answer on Stackoverflow
Solution 8 - AndroidMounaView Answer on Stackoverflow
Solution 9 - AndroidDev SolutionView Answer on Stackoverflow
Solution 10 - AndroidCodemanView Answer on Stackoverflow
Solution 11 - Androiduser1300242View Answer on Stackoverflow