http://www.codeproject.com/Tips/313848/Get-actual-screen-size-for-the-application-layout


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"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainroot"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:paddingTop="35dip" 
  android:paddingBottom="35dip" 
  android:paddingLeft="35dip" 
  android:paddingRight="35dip"
  android:gravity="center" >
 
  .....
  .....
</LinearLayout> 
 
In onCreate(), to get the: 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage);
                
        // Get the layout id
        root = (LinearLayout) findViewById(R.id.mainroot);
        root.post(new Runnable() { 
        public void run() { 
            Rect rect = new Rect(); 
            Window win = getWindow();  // Get the Window
            win.getDecorView().getWindowVisibleDisplayFrame(rect); 
            // Get the height of Status Bar 
            int statusBarHeight = rect.top; 
            // 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();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);   
            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;
           root.setLayoutParams(rootParams);      
        } 
       }); 
       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 root.post(Runnable): After onCreate is completed, then only Activity can know the size of Window and its contents. So once onCreate() is executed, root.post() executes and we get the contents of window decorations through which we get the decorations height and statusbar height. 
 
Benefits 
  • 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.
 
Tip
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 root.post() and then can know the layoutHeight and work further.

License

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.