1

I'm showing a video in a videoview:

In my XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:gravity="center"
  android:layout_height="fill_parent">

    <VideoView
    android:layout_gravity="center"
    android:id="@+id/videoview_player"
    android:layout_width="wrap_content"
     android:layout_height="wrap_content" />

</LinearLayout>

It looked like this works perfect. The video resizes itself so the screen is filled, while the video remains it's aspect ratio.

But on tablets, after like 30 - 60 seconds in portrait orientation, the video stretches to full screen. (It doesn't maintain the aspect ratio, it stretches the height)

Like this:

enter image description here

Oritm
  • 2,113
  • 2
  • 25
  • 40

2 Answers2

1

I ended up subclassing the VideoView:

public class PlayerVideoView extends VideoView{

    private int mForceHeight = 0;
    private int mForceWidth = 0;
    public PlayerVideoView(Context context) {
        super(context);
    }

    public PlayerVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PlayerVideoView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setDimensions(int w, int h) {
        this.mForceHeight = h;
        this.mForceWidth = w;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mForceWidth, mForceHeight);
    }
}

Where in the onCreate i set the dimensions

videoView.setDimensions(800, 600);

Then in the

@Override
public void onConfigurationChanged(Configuration newConfig) {..}

I manually set the dimensions for portrait or landscape, depending on the users device orientation.

Oritm
  • 2,113
  • 2
  • 25
  • 40
0

The issue is solved just by removing the video view alignment from the parent and making it centre in the Relative layout.

    <RelativeLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >

    <VideoView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/videoView" />

</RelativeLayout>
pro_hussain
  • 141
  • 8