Levanphong7887
Levanphong7887

Reputation: 643

How to get screen resolution in Android Honeycomb?

I want to get real resolution of screen on Android Honeycomb.

Here's my code

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

My device is Asus Transformer TF101 with size are 1280x800.

But above code make w = 1280 and h = 752 (That i want is 800 not 752).

I know h < 800 because it's subtracted for status bar.

Have any way to get real height of screen?

Many thanks!

Upvotes: 12

Views: 12304

Answers (10)

StephenDonaldHuffPhD
StephenDonaldHuffPhD

Reputation: 958

This is another viable solution for measuring the screen size (API 11+):

public static ABDimension calculateScreenDimensions(Activity activityGet)
{
    ABDimension abdReturn = new ABDimension();

    try
    {
        Rect rectGet = new Rect();
        Window winGet = null;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        int nStatusBarHeight = 0;
        int nContentViewTop = 0;
        int nTitleBarHeight = 0;
        int nScreenHeight = 0;

        if((activityGet != null) && (rectGet != null) && (displayMetrics != null))
        {
            winGet = activityGet.getWindow();
            if(winGet != null)
            {
                winGet.getDecorView().getWindowVisibleDisplayFrame(rectGet);
                nStatusBarHeight = rectGet.top;
                nContentViewTop = winGet.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                nTitleBarHeight = nContentViewTop - nStatusBarHeight;

                activityGet.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                int screenHeight = displayMetrics.heightPixels;
                abdReturn.nWidth = displayMetrics.widthPixels;
                abdReturn.nHeight = screenHeight - (nTitleBarHeight + nStatusBarHeight);
            }
        }
    }
    catch(Exception e)
    {
        appContext.showMessage(false,"Error","[calculateScreenDimensions]: "+e.toString());
    }

    return abdReturn;
}

Where the ABDimension class contains six integers labeled nWidth, nHeight, nMarginLeft, nMarginTop, nMarginRight and nMarginBottom. This version accounts for common Android decor components like the TitleBar/StatusBar.

Upvotes: 0

StephenDonaldHuffPhD
StephenDonaldHuffPhD

Reputation: 958

A function to get the screen size at API11 level minimum; also includes methods for higher level APIs (change function to use the selected API, its function call and its return object type):

public int getScreenOrientation()
{
    int nOrientation = Configuration.ORIENTATION_UNDEFINED;

    try
    {
        DisplayMetrics displayMetrics = null;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if(windowManager != null)
        {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            if(defaultDisplay != null)
            {
                defaultDisplay.getMetrics(displayMetrics);//API11
                //defaultDisplay.getRectSize(rectGet);//API13
                //defaultDisplay.getSize(pointGet);//API13
                //defaultDisplay.getCurrentSizeRange(pointGet,pointGet);//API16
                //defaultDisplay.getRealSize(pointGet);//API17
                //defaultDisplay.getRealMetrics(displayMetrics);//API17
                if((displayMetrics.widthPixels == displayMetrics.heightPixels) || (displayMetrics.widthPixels < displayMetrics.heightPixels))
                {
                    nOrientation = Configuration.ORIENTATION_PORTRAIT;
                }
                else
                {
                    nOrientation = Configuration.ORIENTATION_LANDSCAPE;
                }
            }
        }
    }
    catch(Exception e)
    {
        showMessage(false,"Error","[getScreenOrientation]: " + e.toString());
    }

    return nOrientation;
}

Note: Configuration.ORIENTATION_SQUARE is deprecated, so here it is replaced to default to ORIENTATION_PORTRAIT.

Upvotes: 0

Sam Lu
Sam Lu

Reputation: 3506

Starting Andorid 3.2, the height of system status bar is not included in DisplayMetrics's height, you have to use undocumented APIs (Display.getRawWidth() and Display.getRawHeight()) to get the physical screen width or height.

Here is the example to show you how to get the physical screen width or height.

Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
int nW = (Integer)mGetRawW.invoke(dp);
int nH = (Integer)mGetRawH.invoke(dp);

UPDATED: For API 13-16, you have to use the above code to get real width/height. For API 17+, you can now use the new public API, Display.getRealSize().

Upvotes: 15

Fuzzical Logic
Fuzzical Logic

Reputation: 13015

The answer you are getting, as properly deduced is because of the status bar. All you have to do is get rid of the status bar before your window display initiates. And then you can reset the status bar before you set the content view of the activity.

The reason to do it this way is that getting rid of the status bar affects your view drawing unless you handle all measure, layout and draw dynamically. Doing this is the middle of your runtime will cause the status bar to disappear, and then reappear if you want it to, resulting in confusion from users.

To Hide the StatusBar:

In your onCreate():

final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Add the flag to the Window Attributes
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
//Disassociate Display from the Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Now your Default Display should work correctly

Still in your onCreate():

Display display = getWindowManager().getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();

Now before you set the Content

Again, in your onCreate():

final WindowManager.LayoutParams attrs = getWindow().getAttributes();
//Show the statubar
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
// Reassociate.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

Finally:

setContentView(r.layout.myView);

The previous code segments will work almost anywhere, actually. I'm just thinking about your user experience. Feel free to place them whereever, of course. These are functional segments pulled from one of my projects. I've seen techniques similar in some Home Launchers as well. Note: depending on the Android version, you might have to do the status bar stuff in onWindowAttached(). If you do, make sure you still call super.onWindowAttached().

Another Technique: Of course, if you want to do this anyway, you could always set the attribute of the activity this way in your manifest.

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

Upvotes: 4

doppelhelix
doppelhelix

Reputation: 77

Perhaps you can add the height of the status bar. Height of statusbar?

   Rect rectgle= new Rect();
   Window window= getWindow();
   window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
   int StatusBarHeight= rectgle.top;
   int contentViewTop= `enter code here`
      window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
   int TitleBarHeight= contentViewTop - StatusBarHeight;

   Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);

Upvotes: 0

Raz
Raz

Reputation: 9058

You can extend a layout which will be your top layout (which will be set to fill_parent width and height) and override the onSizeChange(int width, int height, int oldwidth, int oldheight). The width and height is the real width and height of your display.

Upvotes: 0

vicentazo
vicentazo

Reputation: 1799

In the question Height of statusbar? explain how you get the statusbar height. Having the statusBarHeight value, you can do:

Display display = getWindowManager().getDefaultDisplay();
int h = display.getHeight() + statusBarHeight;

Upvotes: 0

Pointer Null
Pointer Null

Reputation: 40380

Here you are, with little trick and assumption that screen decorations are on top/bottom and never on left/right:

Display d = getWindowManager().getDefaultDisplay();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
int h = d.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int w = d.getWidth();

Upvotes: 12

Victor Wong
Victor Wong

Reputation: 2486

I have a Xoom in hand, and used getWindowManager().getDefaultDisplay() to retrieve the dimension, it returns me 1280 x 800 which is the full screen size. I wonder how to get the resolution minus navigation bar with API before version 3.0.

Upvotes: 5

luvieere
luvieere

Reputation: 37494

Use the DisplayMetrics structure, describing general information about the display, such as its size, density, and font scaling.

The code used to get the display's height is as follows:

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

Log.d("log", "OOO " + metrics.heightPixels);

It's supposed to return the absolute height of the display, in pixels.

Upvotes: 4

Related Questions