I need to get the Height of the VideoView of my activity in android, I am using the following commands, but it gives me the Height as 0.
VideoView vv = (VideoView)findViewById(R.id.video1);
String surl="SOME_LINK.mp4";
Uri uri=Uri.parse(surl);
MediaController mc = new MediaController(this);
vv.setVideoURI(uri);
vv.setMediaController(mc);
mc.setMediaPlayer(vv);
mc.setAnchorView(vv);
int vvHeight=vv.getMeasuredHeight();
Log.d("VideoView Height:",vvHeight+""); // The Logcat output : 0.
int VVheight=vv.getHeight();
Log.d("The Height of VideoView is :",VVheight+""); // The Logcat output : 0
Can Someone tell me what I am making an mistake.
Try this code also check this link for calculating the convenient dimensions
DisplayMetrics dm; //Declare
//Update this line code
//try this line code too
NOTE: If you use android:layout_width:match_parent and `android:layout_height:match_parent to fill all screen width and height and to avoid video stretching and to maintain the aspect ration you should use a video with dimensions ratio 16:9
You need to run in view.post(Runnable)
I have tested code this code at my end. Just Before onCreate() ends. just write these lines.
If you have any query please let me know.
You can call
getWidth
andgetHeight
inonCreate
, but you cannot be sure theView
is fully created and as a consequence, those methods will return 0. As an alternative, you can use aViewTreeObserver
:Alternatively, you can post a
Runnable
on your View. It will be put at the end of the queue of the UI thread, so when it is run, the layout has been measured, andgetHeight
returns the correct measurement.You are trying to get width/height before the view has been drawn.
Try this:
view.post(Runnable)
causes the runnable to be added to the message queue. The runnable will run on the UI thread.