Adjust sizes of Activity’s window

An android.view.Window class underlies an Activity view content. Sometimes you need to strictly define its sizes say on Tablet devices, but for some reason it becomes quite difficult. Given a complex layout it could be difficult to define which view is actually responsible for resulting sizes of the window.

There is a way to strictly define the width and the height of a window programmatically:

public static void adjustWindowSizes(
    Activity activity,
    @DimenRes int widthDimensionResId,
    @DimenRes int heightDimensionResId) {
    WindowManager.LayoutParams params =
        activity.getWindow().getAttributes();
    Resources res = activity.getResources();
    params.width = (int) res.getDimension(widthDimensionResId);
    params.height = (int) res.getDimension(heightDimensionResId);
    activity.getWindow().setAttributes(params);
 }
Adjust sizes of Activity’s window

Leave a comment