0

I have designed code where after the completion of the video the visibility of the RelativeLayout where the video view is placed should become GONE.

vid.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        rel_withvideo_food.setVisibility(View.GONE);
        mp.stop();
    }
});

This code is working on Samsung Galaxy S2 but not on Tab. When I play another video, the RelativeLayout visibility is not getting GONE.The next video plays on the backside of the previous video.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
Abhi
  • 433
  • 2
  • 7
  • 17
  • Does [this](http://stackoverflow.com/questions/11236336/setvisibilityview-visible-doesnt-always-work-ideas) help? – Marius Jun 19 '14 at 06:22
  • what? Marius I am not getting Your point. – Abhi Jun 19 '14 at 06:26
  • Your code had syntax issues, even after somebody previously fixed some of the formatting. The parentheses and braces were unbalanced. I tried to clean it up. Please make sure that you post code that could be built. – Reto Koradi Jun 19 '14 at 06:28

1 Answers1

0

SurfaceView is the sole culprit (of course, this also applies to GLSurfaceView, RSSurfaceView and VideoView, all of which inherits from SurfaceView). It exposes lots of weird behaviours when dealing with other views on top of it. Playing with View.setVisibility() is one of those issues. Clearly, SurfaceView has not been designed to be used with other views (even though the official doc says it ought to be) but as a standalone view for videos, games or OpenGL stuffs.

For the visibility issue, I've found that using View.GONE instead of View.INVISIBLE resolve it. If you don't want to use View.GONE, try changing the focus for example (and back to the one that had focus before), or changing other states. The goal is to wake up the underlying UI system somehow.

In short: when something weird happens with your views and you have a SurfaceView (or subclass) somewhere, try replacing it with something else so you don't lose hours searching what you're doing wrong when you're doing it right (and no false beliefs). This way, you know SurfaceView is to blame and you can hack around it with beautiful comments to piss on it without qualms.

As answered here.

EDIT:

Get a copy of LayoutParams for later use:

RelativeLayout.LayoutParams rllp = (RelativeLayout.LayoutParams) vid
            .getLayoutParams();

Remove VideoView completely

((ViewGroup) vid.getParent()).removeView(vid);

Recreate VideoView with same LayoutParams when needed.

vid = new VideoView(someContext);
vid.setLayoutParams(rllp);
Community
  • 1
  • 1
Marius
  • 810
  • 7
  • 20