-2

I am using this thick but it returns "0"

    View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
    int viewHeight = content.getHeight();

How to do so?

Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37

3 Answers3

3
root = (ViewGroup)findViewById(R.id.root);
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();
        int  titleHeight = contentViewTop - statusHeight;
        Log.w("dimens_tag", "title = " + titleHeight + " status bar = " + statusHeight);
}
});
Sergii
  • 1,521
  • 3
  • 24
  • 37
Dory
  • 7,462
  • 7
  • 33
  • 55
  • don't forget about pad's bottom bar! int bottomH=0; int resourceId = getResources().getIdentifier("status_bar_height","dimen", "android"); if (resourceId > 0) bottomH=getResources().getDimensionPixelSize(resourceId); – djdance Jun 18 '14 at 18:16
  • Thanks! this solved my problem. Why this only works when wrapped using the post() method? – Ran Dec 01 '14 at 06:30
  • @Ran Because calling in onCreate will be too early. It may return you 0.So it should be called after some delay,when onCreate method will finish rendering views and views are visible. Hope I was able to justify your doubt. Happy Coding :) – Dory Dec 01 '14 at 07:00
0

based on Dory's answer I explored next:

            Window win = getWindow();
            topBarHeight = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();;
            Rect rect = new Rect();
            win.getDecorView().getWindowVisibleDisplayFrame(rect);
            if (rect.top==0){
                //pad
                int resourceId = getResources().getIdentifier("status_bar_height","dimen", "android");
                if (resourceId > 0)
                    bottomBarHeight=getResources().getDimensionPixelSize(resourceId);
            } else
                bottomBarHeight=0;
djdance
  • 3,110
  • 27
  • 33
-2
//Returns the size of the entire window, including status bar and title.
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);

//Try to find the dimension of the view without the status/title bars.
int iViewHeight = mvMountain.getHeight();
int iViewWidth = mvMountain.getWidth();
Abby Chau Yu Hoi
  • 1,378
  • 3
  • 15
  • 37
  • "Try to find???" Pretty useless response. The code above will not provide you with the activity's dimensions. – Johann Jul 31 '13 at 09:39