10

I noticed a problem on Android's default browser, where 100% width may actually go past the edge of the screen. Here's a minimal test case:

  <div class='separator' width=100% style='border: 2px;padding: 2px;border-style: solid;'>&nbsp;</div>
  <div class='separator' width=100% style='border: 2px;padding: 2px;border-style: solid;'>New & improved div</div>
  <div class='separator' width=100% style='border: 2px;padding: 2px;border-style: solid;'>another working one</div>
  <div class='separator' width=100% style='border: 2px;padding: 2px;border-style: solid;'>another</div>

This works as expected on a desktop browser, but in Android webkit browser, the first div goes way off the screen, and doesn't change width when zooming in and out. The divs after it work correctly.

Update: I've tested this on 2.3, 3.0, 3.1, and a newly created 2.2 emulator, they all fail to size it correctly. It looks like other people have noticed this, see here and here. Anyone know a good workaround for this bug?

NoBugs
  • 9,310
  • 13
  • 80
  • 146

3 Answers3

10

Just add this meta tag inside your html head tag .I've checked it with android 2.1 browser and it works fine on zoom.

<meta name="viewport" content="width=device-width" />
Sandeep
  • 843
  • 1
  • 9
  • 18
  • That seems to work well, but it doesn't let you zoom out as you normally could. At default zoom you can zoom in, but not zoom out. – NoBugs Jul 05 '11 at 06:57
  • 1
    praise to you. this thing took me a day and a night of trying until i found this. – Etienne678 Apr 05 '13 at 23:51
1

In my WebView, I was able to fix the widths by turning off wide viewport.

webview.getSettings().setUseWideViewPort(false);

This disables horizontal scroll unless absolutely necessary, and div widths and zoom work as expected. Obviously this will only work with a custom WebView, maybe there is a more generic way to do this with something like <meta name="viewport" ...> ?

NoBugs
  • 9,310
  • 13
  • 80
  • 146
0

use

layout_width="fillparent"

:)

Friz14
  • 278
  • 3
  • 11
  • That may be helpful if the WebView were not taking up the full width, but that's not my problem. This is a problem with the HTML width, and it even happens in the default browser app. – NoBugs Jul 08 '11 at 17:22