How to Get View Size in Android

Determining the Size of an Android View or Screen at Run Time

For efficient bitmap handling or dynamic View creation in an App the area that a widget or layout is using needs to be known. If no fixed sizes are allocated at design time the size of a View may not be known until an App is executed. This is because of the wide range of display sizes that Android supports. The example code snippets in this articles shows how to read the screen size and the size of Views as the App runs. To run the example code you will need to create a new Android project (those new to Android programming can view the article Your First Android Hello World Java Program to see how), we called our App View Size.

In the layout designer for activity_main.xml (or whatever you called your layout) add another TextView, called textXY, next to the existing Hello world! widget. Change the Text on the first TextView to X,Y. Add this code to the oncreate method in MainActivity.java (or whatever class you are using), you will need an imports for TextView and DisplayMetrics. :

This is the code running on an AVD with a 320×480 screen:
Size of Android Screen

He is the layout used for this screen:

Drop an ImageView onto the layout, here its uses the ic_launcher.png icon file, you may use your own image. The size of a View can be retrieved using the getWidth and getHeight  methods. Change the code in the onCreate to set the TextView to the ImageView’s width and height (an import for View is required):

Opps! It is giving us 0,0 for the ImageView size, even though we can see that it is not 0,0:

Android Incorrect View Size

This is because in onCreate the screen has not yet been laid out so the size of the ImageView has not been determined hence the getWidth() and getHeight() methods are returning zero. In fact they will likely return zero in onStart() and onResume(). You need to override onWindowFocusChanged() to get the ImageView sizes:

Android ImageView Size

The same code can be used to get the size of the View the widgets sit in, notice that for the screen XML the RelativeLayout was given an id (@+id/screen) which means the base View’s width and height can be grabbed (change R.id.imageView1 to R.id.screen) :

Android Layout Size

Notice it is 50 pixels less than the screen size because of the notification bar.

To get the the size of a View or widget at soon as it is known (rather than waiting for the onWindowFocusChanged event) attach a listener to its ViewTreeObserver. Do this by writing a class that implements ViewTreeObserver.OnGlobalLayoutListener in the Activity’s class. This new class will have a onGlobalLayout method that gets the View or widget dimensions that can then be stored for later use (here they are displayed as before). Here is the example source code for the entire MainActivity.java file to show this way of getting the ImageView’s width and height:

In a future article we will look at efficiently loading bitmaps using a View’s dimensions.

See Also

fitSystemWindows(Rect insets) – http://developer.android.com/reference/android/view/View.html#fitSystemWindows%28android.graphics.Rect%29

2 thoughts on “How to Get View Size in Android

  1. Pingback: Android Bitmap Loading for Efficient Memory Usage |Tek Eye

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.