3

I'm having a lot of trouble trying to position the MediaController right where I want it.

What I really want to do is always show the MediaController above the VideoView, aligned with the bottom.

I've been trying to do this for the last three days without success. I've tried getting the height of the MediaController but it is always zero until it is shown the first time at the bottom of the screen.

I have overriden the MediaController to override setAnchorView so that VideoView doesn't overwrite it.

I've read the source code of both VideoView and MediaController and I've also trying getting the height of MediaController using reflection over the private fields, also without success before the component is shown the first time.

How can I know the height of the MediaController without it being displayed to the user?

Henrique Rocha
  • 1,737
  • 1
  • 19
  • 29
  • The controls at the bottom of the video is the default behaviour, isn't it? Can you share some code since I think you're trying to do something you shouldn't be? You can't measure the controls without showing them since they're not part of any view hierarchy (same goes for any View, really). – Delyan May 08 '12 at 16:29
  • I gave up what I was trying to do since it is not possible to know the height of the MediaController before it is displayed. I will move to a YouTube approach where you don't have a MediaController and only show a ProgressBar and play/pause on touch event. – Henrique Rocha May 10 '12 at 14:49

1 Answers1

0

Put your video view in a FrameLayout and set its height to wrap_content so that the parent resizes to the child (video view) height. This step is crucial because the MediaController view is added, by default, to bottom of video view parent, if anchored to the video view. Hence, parent's height must match child's height to make media controller visible at bottom of video view.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

Then in your activity class, use the following code to add the media controller:

MediaController mediaController = new MediaController(VideoPlayerActivity.this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
Iffat Fatima
  • 1,610
  • 2
  • 16
  • 27