-2

I have survey about this subject in a day. What I mean is how to show toast when tapping the Videoview for a while.

Below is what I've found,

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

detect double tap (Double click) or long click in a videoview

But these really can't solve my problem.I really don't know what has happend? And is there any function can fire up long pressing event in the video view?

here's my code

these two event really can't work.

    mVideoView.setOnLongClickListener(new OnLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {
            Log.e("devon","onitemlongclick");
            return true;
        }

        @Override
        public boolean onLongClick(View v) {
            Log.e("devon","onLongClick");
            return true;
        }

    });

need help !!!thanks!

Community
  • 1
  • 1
Ashton
  • 2,425
  • 2
  • 22
  • 26
  • Put it in a layout, and make the layout FOCUS_BLOCK_DESCENDANTS in your `setDecendantFocusability()` call. Then capture the long click in the layout wrapper, and act accordingly... – Shark Jul 24 '13 at 10:19

2 Answers2

3
  • add OnLongClickListener in your setupViewComponent call
  • try using onTouch
  • try attaching the OnLongClickListener to the videoview's surface
  • try wrapping the videoview with a transparent imageview/something that grabs focus, and use that as your 'touching pad'
  • post logcat.
Shark
  • 6,513
  • 3
  • 28
  • 50
  • don't know how. how do I try using onTouch in setOnLongClickListener?? can't override this, and how do I try attaching the OnLongClickListener to the videoview's surface?? – Ashton Jul 24 '13 at 11:09
  • well my good man, you're gonna have to learn to code some day, might as well start today. Pretty much all that's left to do now is to code it up, run it a few times, see what works and what doesn't, slightly refine and run again. So, to answer your question - you do not 'attach' an `onTouch()` in the `setOnLongClickListener()`, you 'attach' it via `setOnTouchListener()`. – Shark Jul 26 '13 at 14:36
0

This is a sample example on how to create your own TouchListsners for managing Click and LongClick on VideoView. In this example I pass to the listeners the idex of the data clicked and the index of the view (in behind I have several VideoView in arrays maps to data list, like in an Adapter)

/**
 * Simple OnTouchListenerIndexed with Indexes for VideoView ClickListeners 
 * You have to handle longClick and click by yourself 
 */
private abstract class OnTouchListenerIndexed implements OnTouchListener {
    private static final int LONG_CLICK_DURATION=600;//in millis
    int dataIndex = INDEX_NOT_DEFINED;
    int imageViewIndex = INDEX_NOT_DEFINED;
    long timeActionDown;
    AtomicBoolean stillNotConsumed=new AtomicBoolean(true);
    AtomicBoolean actionDone=new AtomicBoolean(false);

    public OnTouchListenerIndexed(int dataIndex, int imageViewIndex) {
        this.dataIndex = dataIndex;
        this.imageViewIndex = imageViewIndex;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            timeActionDown=System.currentTimeMillis();
            stillNotConsumed.set(true);
            actionDone.set(false);
            //launch LongClick in 1s
            v.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(stillNotConsumed.get()){
                        stillNotConsumed.set(false);
                        actionDone.set(true);
                        onLongTouch(dataIndex, imageViewIndex);
                    }
                }
            },LONG_CLICK_DURATION);
            //consumed
            return true;
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            long timeActionUp=System.currentTimeMillis();
            stillNotConsumed.set(false);
            if(actionDone.get()){
                //do nothing
                return true;//you have consumed it
            }else {
                actionDone.set(true);
                //Check Click or LongClick
                if (timeActionUp - timeActionDown > LONG_CLICK_DURATION) {
                    //une seconde plus tard
                    return onLongTouch(dataIndex, imageViewIndex);
                } else {
                    return onTouch(dataIndex, imageViewIndex);
                }
            }

        }else{
            //don't consume it
            return false;
        }
    }


    public abstract boolean onTouch(int dataIndex, int imageViewIndex);
    public abstract boolean onLongTouch(int dataIndex, int imageViewIndex);
}