Reputation: 201
I am attempting to dynamically set the size of an Android VideoView. I have looked on StackOverflow as well as the internet; and the best solution I found was from here. I have put my implementation below:
public class ResizableVideoView extends VideoView {
public ResizableVideoView(Context c) {
super(c);
}
private int mVideoWidth = 100;
private int mVideoHeight = 100;
public void changeVideoSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
// not sure whether it is useful or not but safe to do so
getHolder().setFixedSize(width, height);
forceLayout();
invalidate(); // very important, so that onMeasure will be triggered
}
public void onMeasure(int specwidth, int specheight) {
Log.i("onMeasure","On Measure has been called");
setMeasuredDimension(mVideoWidth, mVideoHeight);
}
public void onDraw(Canvas c) {
super.onDraw(c);
Log.i("onDraw","Drawing...");
}
}
The video resizes correctly on the Android emulator as well as on a Motorola Droid X; but on a Motorola Droid, the VideoView resizes but the video playing in the VideoView does not resize. On the Motorola Droid, if the VideoView is set to a larger dimension than the video playing, a black background appears in the VideoView with the video playing in the top left corner of the VideoView on top of the black background.
How does one resize a VideoView properly in Android?
Thanks, Vance
Upvotes: 8
Views: 12003
Reputation: 2736
My implementation works like this:
RelativeLayout.LayoutParams videoviewlp = new RelativeLayout.LayoutParams(newwidth, newheight);
videoviewlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
videoviewlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
videoview.setLayoutParams(videoviewlp);
videoview.invalidate();
By invalidating the videoview, you will force it to redraw the whole videoview using the new LayoutParams (and the new dimensions).
Upvotes: 3