Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android browser's screen.width, screen.height & window.innerWidth & window.innerHeight are unreliable

I'm working on a web app that is targeted to browsers on desktop, tablet and smartphone.

The web app has a light box implemented using Colorbox with an iframe. When the browser window is resized or the tablet/phone has it's orientation changed, Javascript code queries the window dimensions so that it can resize some elements of the light box.

The issue I'm having is that everything works fine on desktop (Windows: IE, Firefox, Chrome, Mac: Safari), iPad & iPhone but not on the Android smartphone (HTC) and Android Emulator.

Android always returns different values for screen.width, screen.height, window.innerWidth & window.innerHeight when they're queried from inside the window's resize event, which fires multiple times.

Why is the Android browser returning such a wide variance in values and how can I reliably detect the width and height of the browser window?

like image 465
John Mills Avatar asked May 16 '12 00:05

John Mills


People also ask

How can I get mobile screen width and height?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.

How do I make my Android apps fit all screen sizes?

Simple, use relative layout with the set margin code. Set the margins between each text view, button, etc and it will look the same on every phone. android:layout_marginTop="10dp" // change Top to Bottom, Left or Right for what you need.


2 Answers

On Android, window.outerWidth and window.outerHeight are reliably the screen size. Depending on your version of Android, innerWidth/Height is usually incorrect.

Here's a really good writeup on the situation.

like image 89
six8 Avatar answered Sep 26 '22 14:09

six8


Below is differentiation based on readings with Samsung Tab running Android 4.1

  • screen.height - gives actual device height including task bar and title bar

  • window.innerHeight - gives the height excluding task bar, title bar and address bar(if visible)

  • window.outerHeight - gives the height excluding task bar and title bar height, (no matter address bar is visible or hidden, outerHeight include the address bar height.)
like image 33
Ratnakar Avatar answered Sep 24 '22 14:09

Ratnakar