DingleNutZ
DingleNutZ

Reputation: 163

Getting screen height returns a larger screen than the physical screen can display

When I get the screen size for my Android app, it returns a variable which in every case is too large for the screen to display. I use the following code to get screen size.

final int windowHeight = getResources().getDisplayMetrics().heightPixels;
final int windowWidth = getResources().getDisplayMetrics().widthPixels;

Is there any way to compensate for it? Or to even fix my problem?

Also, is there a way to turn the battery/time bar off in the app? I think that might be the reason I'm having trouble, but I don't actually know. That's why I am asking.

Upvotes: 0

Views: 148

Answers (2)

weakwire
weakwire

Reputation: 9300

Notice that you might want density points instead of pixels. If you get the dimensions in pixels here's how you convert them. But you can get the dimensions in dp too

        /*  
         * Get dps from pixel.
         */
        float scale = getResources().getDisplayMetrics().density;

        public static float dpFromPixels(int pixels) {
            float dp = (float) (pixels / scale + 0.5f);
            return dp;
        }

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401172

To disable the battery/time bar, I use this in my AndroidManifest.xml file, for the <application> tag :

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar" 
    ...
    >


And, to get the screen's size, here's what I have in my Activity :

    Display display = getWindowManager().getDefaultDisplay(); 
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

Upvotes: 1

Related Questions