Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 260
Fabian Avatar asked May 30 '11 11:05

Fabian


People also ask

Does Android have OnClickListener?

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.

Why my setOnClickListener is not working?

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.

What is set onClick listener?

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.

How do you implement setOnClickListener?

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.


2 Answers

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;     } }); 
like image 39
Dan Avatar answered Sep 19 '22 14:09

Dan


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

like image 105
newdev Avatar answered Sep 20 '22 14:09

newdev