Many times, we want to set up our layout size dynamically. For that, we need to know the screen size programmatically before showing the Activity, i.e., in onCreate() method. Knowing screen size or dimension is very easy and 2 line code, but that doesn't mean that our application layout size will also be the same.
To get the actual size available to our layout, we need to know the sizes of screen, titleBar and statusBar. Finally, deduct height of titleBar & statusBar from screen and that's how we can get the actual height for our layout.
The XML file has the main layout id as "root" of type LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
  android:gravity="center" >
In onCreate(), to get the: 
    public void onCreate(Bundle savedInstanceState) {
        // Get the layout id
        root = (LinearLayout) findViewById(; Runnable() { 
        public void run() { 
            Rect rect = new Rect(); 
            Window win = getWindow();  // Get the Window
            // Get the height of Status Bar 
            int statusBarHeight =; 
            // Get the height occupied by the decoration contents 
            int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); 
            // Calculate titleBarHeight by deducting statusBarHeight from contentViewTop  
            int titleBarHeight = contentViewTop - statusBarHeight; 
            Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop); 
            // By now we got the height of titleBar & statusBar
            // Now lets get the screen size
            DisplayMetrics metrics = new DisplayMetrics();
            int screenHeight = metrics.heightPixels;
            int screenWidth = metrics.widthPixels;
            Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);   
            // Now calculate the height that our layout can be set
            // If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also 
            int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
            Log.i("MY", "Layout Height = " + layoutHeight);   
           // Lastly, set the height of the layout       
           LinearLayout.LayoutParams rootParams = (android.widget.LinearLayout.LayoutParams)root.getLayoutParams();
           rootParams.height = layoutHeight;
       Log.i("MY", "FINISHED OnCreate");
The Output of logs:
01-12 12:28:41.916: INFO/MY(198): FINISHED OnCreate
01-12 12:28:42.196: INFO/CPP(198): titleHeight = 40 statusHeight = 25 contentViewTop = 65
01-12 12:28:43.196: INFO/CPP(198): Actual Screen Height = 480 Width = 320 
01-12 12:28:43.196: INFO/CPP(198): Layout Height = 415
Reason for adding the code in After onCreate is completed, then only Activity can know the size of Window and its contents. So once onCreate() is executed, executes and we get the contents of window decorations through which we get the decorations height and statusbar height. 
  • Code is added in onCreate(), so whenever the screen rotates/orientation changes, it executed and we get the latest window sizes.
  • Regardless of density, the code works on all densities and manages accordingly. Different density has different sizes of titlebar, status bar and content views. 
  • Whenever layout needs to know its size dynamically to set heights of its children or so, this can be the best option.
If the same code needs to be used in multiple Activities, add the code in a static method of a class and call thatstatic method in and then can know the layoutHeight and work further.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

All Time Programming
Software Developer (Senior) 
India India
I am a Freelance Software Developer and a Consultant. I provide service in Desktop, Client-Server, Database, Web and Mobile application developments. I like working on challenging projects and implementing new technologies and platforms.
I had started my career in IT development from DBase, COBOL, C and reached today to Java, C#, .NET, Android, BlackBerry (RIM), Windows Mobile, etc.