Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Keyboard shrinking the viewport and elements using unit vh in CSS

I am experiencing a very strange and unique issue.

All my pages are using vh and vw CSS units instead of px due to the nature of the project.

Issue: On Android tablets, when you touch the input field the default keyboard pushes the view port which is causing the page and all the elements in the page to shrink.

On ipad this issue does not exist since the keyboard overlaps the screen and does not push the screen.

Looking for any solution to avoid the Android keyboard not to push the viewport of the browser and keep the original size.

Note: The only option i am left with is to avoid keyboard to push viewport, i won't be able to change the CSS units or use xml, manifest. These are web pages which experiencing this issue.

like image 607
Manu Avatar asked Oct 06 '15 06:10

Manu


1 Answers

I know this is an old question, but I had the exact same problem in my app. The solution I found was fairly simple. (My app is in Angular so I put this in the app.component's ngOnInit function, but document.ready() or any other "initialization complete" callback should work just fine with the proper experimentation)

setTimeout(function () {         let viewheight = $(window).height();         let viewwidth = $(window).width();         let viewport = document.querySelector("meta[name=viewport]");         viewport.setAttribute("content", "height=" + viewheight + "px, width=" + viewwidth + "px, initial-scale=1.0");     }, 300); 

This forces the viewport meta to explicitly set viewport height, whereas hardcoding

<meta name="viewport"  content="width=device-width, height=device-height, initial-scale=1"> 

doesn't work because the device-width and device-height change when Android's soft keyboard is opened.

like image 128
Tyler Merle Avatar answered Sep 27 '22 19:09

Tyler Merle