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);
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 statusBarHeight = rect.top;
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight = contentViewTop - statusBarHeight;
Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop);
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);
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
Log.i("MY", "Layout Height = " + layoutHeight);
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 that
static
method in
root.post()
and then can know the
layoutHeight
and work further.