Search code examples
androidandroid-videoviewmediacontroller

How to get the Height of the VideoView included in the Activity


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.


Solution

  • You are trying to get width/height before the view has been drawn.

    Try this:

    vv.post(new Runnable() { 
    @Override
      public void run() {
        // get height and width here
      }
    });
    

    view.post(Runnable) causes the runnable to be added to the message queue. The runnable will run on the UI thread.