javascript - Adjust font size of Android WebView -
how adjust font size of android webview
? following appears have no effect:
private void fontsizeplus() { fontsize = (fontsize < font_size_max) ? fontsize + font_size_increment : fontsize; this.changefontsize(fontsize); } private void fontsizeminus() { fontsize = (fontsize > font_size_min) ? fontsize - font_size_increment : fontsize; this.changefontsize(fontsize); } private void changefontsize(int value) { string js = "document.getelementsbytagname('body')[0].style.webkittextsizeadjust= '" + value + "%';"; mwebview.loadurl("javascript:(function() { " + js + " })()"); }
where webview
, constants have been initialized follows:
private final static int font_size_default = 100; private final static int font_size_min = 50; private final static int font_size_max = 150; private final static int font_size_increment = 5; private int fontsize = font_size_default; private webview mwebview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.index); mwebview = (webview) findviewbyid(r.id.webview); mwebview.getsettings().setjavascriptenabled(true); mwebview.loadurl("file:///android_asset/index.htm"); }
@rob's answer , comment pointed me in right direction.
first make sure font sizes relative default font size. if use absolute values following not work on elements.
then:
@override protected void oncreate(bundle savedinstancestate) { ... mwebview = (webview) findviewbyid(r.id.webview); fontsize = mwebview.getsettings().getdefaultfontsize(); ... } private void fontsizeplus() { fontsize++; this.changefontsize(fontsize); } private void fontsizeminus() { fontsize--; this.changefontsize(fontsize); } private void changefontsize(int value) { mwebview.getsettings().setdefaultfontsize(value); }
by changing value of default font size relative font sizes adjusted accordingly. gives greater control websettings.settextsize (websettings.textsize t)
.
Comments
Post a Comment