Android: Adding View/Managing Layout

In this topic, you will learn how to manage the layout of a View added to the BrightcoveVideoView.

Understanding layouts

Before working with the Brightcove Native SDK for Android, it is a good idea to understand how to manage and customize layouts in your Android app.

Start by reviewing the Android Developer User Interface guide.

Overriding layout params

The BrightcoveVideoView is the parent class of the BrightcoveExoPlayerVideoView.

If you’re adding a View to the BrightcoveVideoView, then you need to override the setLayoutParams() method on the View child to apply only the layout params that make sense for the View child.

Here is an example with the setLayoutParams() method:

@Override
public void setLayoutParams(ViewGroup.LayoutParams layoutParams) {
    Log.v(TAG, "setLayoutParams: " + layoutParams);
    super.setLayoutParams(layoutParams);
    if (layoutParams != null) {
        int childCount = getChildCount();
        FrameLayout.LayoutParams frameLayoutParams =
            new FrameLayout.LayoutParams(layoutParams.width, layoutParams.height);
        frameLayoutParams.gravity = Gravity.CENTER;
        for (int i = 0; i < childCount; i++) {
            getChildAt(i).setLayoutParams(frameLayoutParams);
        }
    }
}

This allows the SurfaceView and the ImageView to get the layout params specified for the BrightcoveVideoView.

Although it is possible to apply the layout params only to the SurfaceView and ImageView children, it would be difficult for additional children to react when the BrightcoveVideoView has its layout params updated.

Brightcove suggests overriding setLayoutParams() in the LinearLayout child so that it passes through the width, but leaves the height and gravity unmodified.

When to add a View

Applications to add a View in the BrightcoveExoPlayerVideoView can be varied depending on the objective to add an object in front of the Player. For example, you may use views to add/remove TextViews to display the captions, or to add a layout for the controller in the App.

The BrightcoveExoPlayerVideoView variant extends from the BaseVideoView which extends the Android FrameLayout. This means you could add a view to the BrightcoveExoPlayerVideoView the same way you would do it to a FrameLayout.

Here is an example in the activity_main.xml file of an Android App:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <com.brightcove.player.view.BrightcoveExoPlayerVideoView
        android:id="@+id/brightcove_video_view"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:layout_gravity="center_horizontal|top">
        <RelativeLayout
            android:id="@+id/rl_elephant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|bottom"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text=“HELLO WORLD”
                android:textColor="@color/white"
                >
        </RelativeLayout>
    </com.brightcove.player.view.BrightcoveExoPlayerVideoView>
</LinearLayout>