1

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.

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50

5 Answers5

6

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.

Hemanth
  • 2,717
  • 2
  • 21
  • 29
  • 2
    I voted your answers up for suggesting the Runnable option and being able to justify this choice. Thank you. – 2Dee Jan 15 '15 at 11:40
2

I have tested code this code at my end. Just Before onCreate() ends. just write these lines.

VideoView videoView = (VideoView) findViewById(R.id.videoView);
videoView.post(new Runnable() {

            @Override
            public void run() {

                Log.e("",""+videoView.getHeight()+" "+videoView.getWidth());
            }
        });

If you have any query please let me know.

droidd
  • 1,361
  • 10
  • 15
  • Runnable run on UI thread not on another thread. Yes. I always use this at my end and this is a tested code. – droidd Jan 15 '15 at 11:25
  • This is correct, but it's sad you could not explain *why* it works, like arol_123 did. – 2Dee Jan 15 '15 at 11:35
1

You can call getWidth and getHeight in onCreate, but you cannot be sure the View is fully created and as a consequence, those methods will return 0. As an alternative, you can use a ViewTreeObserver :

VideoView vv = (VideoView)findViewById(R.id.video1);
ViewTreeObserver vto = vv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        // Use getHeight here
        ViewTreeObserver obs = vv.getViewTreeObserver();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }
    }

});

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, and getHeight returns the correct measurement.

vv.post(new Runnable() { 
    @Override
    public void run() {
        // Use getHeight here
    }
});
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • Now, why you edited your answer. You just copy paste other answwer. can you explain? – droidd Jan 15 '15 at 11:34
  • Yes, I can, what do you need explanation for ? I investigated the Runnable option to see *why* it was working, something that you didn't do even if you were suggesting that solution. I saw it was indeed a working solution and added that to my answer to provide the most complete answer possible. – 2Dee Jan 15 '15 at 11:36
  • I just want to say. Others has already give the solution on runnable. then you edit after seeing there solutions. – droidd Jan 15 '15 at 11:38
  • At least I understand what I'm doing, which is more than you can say. U mad ? – 2Dee Jan 15 '15 at 11:39
0

Try this code also check this link for calculating the convenient dimensions

DisplayMetrics dm; //Declare

//Update this line code

dm = new DisplayMetrics(); 
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels;
int width = dm.widthPixels;

//try this line code too

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(uriString);
mp.prepare();
int width = mp.getVideoWidth();
int height = mp.getVideoHeight();

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)

Community
  • 1
  • 1
sherin
  • 1,091
  • 2
  • 17
  • 27
0
VideoView vv = (VideoView)findViewById(R.id.video1);

ViewTreeObserver vto = vv.getViewTreeObserver();

vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
 public boolean onPreDraw() {
     vv.getViewTreeObserver().removeOnPreDrawListener(this);
     finalHeight = vv.getMeasuredHeight();
     finalWidth = vv.getMeasuredWidth();
     Log.d("debug" , "" + "Height: " + finalHeight + " Width: " + finalWidth);
     return true;
                  }
                   });
King of Masses
  • 18,405
  • 4
  • 60
  • 77