3

Please get me correctly over here :

I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :

      root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout  

       android.view.Display display = getWindowManager().getDefaultDisplay();
    int height = Display.getHeight(); // I know this is deprecated have hence used 
      int width = Display.getWidth(); // DisplayMetrics
    int childWidth, childHeight;

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    //int density = metrics.densityDpi;
    height = metrics.heightPixels;  //480
    width = metrics.widthPixels;    //320

This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.

Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.

ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.

Tvd
  • 4,463
  • 18
  • 79
  • 125

3 Answers3

3

SOLUTION :

In my onCreate(), I added the following lines :

    setContentView(R.layout.mainpage);

    root = (LinearLayout) findViewById(R.id.mainroot);    
    root.post(new Runnable() {
        public void run() {
            Rect rect = new Rect();
            Window win = getWindow();
            win.getDecorView().getWindowVisibleDisplayFrame(rect);
            int statusHeight = rect.top;
            int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            titleHeight = contentViewTop - statusHeight;
            Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);

            // CALCULATE THE SIZE OF INNER LAYOUTS
            calculateChildSize();
        }
    });

With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.

Good Point is this code works for all density's.

Hope this helps others too.

FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities. BUT

Can the user change the phone's density at runtime. If so, then the Activity's onCreate will be called again or not ? If not, then can I trap the density change event where I can add this code and make the current activity to refresh.

Any idea suggestions for improving is appreciated.

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • I don't think the user can change the device density at runtime, although with the introduction of compatibility mode in 3.2 I'm not sure how that would affect it. As for your measurements, if you don't really need the size of the "status bar" and "title bar" but only really want the usable size for your views, then why don't you just do this in your run() method: `run() { sRootHeight = root.getHeight(); }` where `sRootHeight` is a static class variable so your other activities can reference it. – Tony Chan Feb 03 '12 at 02:00
  • @Turbo, I actually got to have 2 rows of 3 buttons each. Based on space available, I calculate the height & width for the buttons considering the spaces between them. I have added 2 rows of 3 buttons each in XML, root.getHeight(); gives me the std sizes of buttons. If the screen is is portriat mode the height is more than width & vice-versa in Landscape mode. Based on the height available I got to set the height of the buttons. If in 3.2 the compatibility moe may be affected to some extend, then in further version, it might be available completely also. What do you say ?? – Tvd Feb 03 '12 at 05:53
  • I haven't played with the compatibility mode so I'm not too familiar with it's affects. But using the root.post() method of getting your view height is pretty safe I think, since no matter what the screen size, you'll be able to calculate your needed space at runtime. If you are concerned about Compatibility mode [here is how you can disable it](http://developer.android.com/guide/practices/screen-compat-mode.html#Disable) – Tony Chan Feb 11 '12 at 09:18
  • @Tvd will this exclued action bar height if there is action bar in view. – Dory Nov 20 '13 at 09:39
0

There is very easy method for that. doOnLayout() method is called as soon as layout is measured and ready:

rootView.doOnLayout {
    rootView.getMeasuredWidth()
    rootView.getMeasuredHeight()
}
Samir Alakbarov
  • 1,120
  • 11
  • 21
0

//Where rootView is the object for the root view of your application final int viewWidt = rootView.getMeasuredWidth();

Nitin
  • 1,451
  • 13
  • 17