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?
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Show activity on this post.
OnClickListener and wires the listener to the button using setOnClickListener(View. OnClickListener) . As a result, the system executes the code you write in onClick(View) after the user presses the button. The system executes the code in onClick on the main thread.
To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
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; } });
use VideoView.setOnTouchListener(..)
it works for VideoView
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