paxdiablo
paxdiablo

Reputation: 881113

How to find usable display resolution in Android?

Let's say we have an Android application which wants to totally manage the display area, so it can adjust sizes of its subcontrols (at runtime, no XML - we need to actively modify subcontrol sizes on the fly).

This application will run in landscape orientation only and no keyboard input so we don't expect the usable area to ever change.

The sizes of what we will display will be calculated based on this usable area. For example, one item is to take up exactly the left half of the usable area, the right half will be divide amongst other items in strict ratios. Even font sizes will be adjusted so that they fit correctly into the items.

My initial thought was to provide main.xml with nothing but a grid layout with no content but configured to expand to the maximum size (with fill_parent or something similar). We'll be using the grid layout to position things, but the control we exert over sizes will dictate how the layout manager displays things.

Hopefully, we could then query the grid layout at the appropriate time (after init, before draw) to get the resolution and then adjust all future draws based on that.

Is there a better way to do this? I realise the XML is meant to separate content from presentation but this particular client wants total control over layout and will limit, if necessary, the devices on which the code can run (it's an in-house app so they can dictate that).

One other thought I had was to add all the subcontrols in the XML with bogus sizes (everything will always be there, it's just the sizes that may change). However, by doing that, I'm not sure if I'll lose the ability to get the usable area and hence intelligently adjust the sizes.

Upvotes: 1

Views: 1927

Answers (1)

Kelvin Trinh
Kelvin Trinh

Reputation: 1248

Are you mixing up between resolution and density. What is "usable display resolution" ?. Each device has only one resolution in pixels:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

final int height = dm.heightPixels;
final int width = dm.widthPixels;

But what you display depends on your resolution ratio affected by density and display size.

(UPDATED)

(1) to see start in Full screen mode and hide all title bar or notification bar:

    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

(2) To get view Rect:

    Rect rect = new Rect();
    Window window = getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int titleBarHeight = contentViewTop - statusBarHeight;

Upvotes: 3

Related Questions