Android Question Webview -Dynamic Font Change -Like CTRL + /- in PC Browser

kps

Member
Hello all,

Is there a way to change the font size in Webview, like we do CTRL +/- in a PC browser?
All my searches in the forum pointed me only to change the font through HTML or CSS.

Pinch zooming throws the content out of the boundaries.
It doesn't happen in CTRL +/-
The font size changes, the html text neatly readjusts itself and the whole page stays in view all the time whether you zoom in or out.

Is there some way to implement this in B4A?

Regards,
KPS
 

JohnC

Expert
Licensed User
Longtime User
One of these may work:

1) Webview.Zoom(ZoomIn as Boolean) 'make sure 'zoomenabled = true
2) WebviewSettings lib has wvs.setDefaultFontSize(wv,FontSize)
3) Webviewsettings lib has wvs.SetDefaultZoom(Close, far, medium)
 
Upvote 0

kps

Member
@JohnC

I tried out all the suggestions.

1) Webview.Zoom(ZoomIn as Boolean) 'make sure 'zoomenabled = true
It has the same effect as clicking the + / - buttons on Webview

2) WebviewSettings lib has wvs.setDefaultFontSize(wv,FontSize)
Nothing happens !

3) Webviewsettings lib has wvs.SetDefaultZoom(Close, far, medium)
Nothing happens !

This is my code for testing it out.

B4A:
Sub btnZoomIn_Click
    'WebViewSettings1.setDefaultZoom(WebView1,"CLOSE")
    WebViewSettings1.setDefaultFontSize(WebView1,20)
End Sub

Sub btnZoomOut_Click
    'WebViewSettings1.setDefaultZoom(WebView1,"FAR")
    WebViewSettings1.setDefaultFontSize(WebView1,5)
End Sub

Sub btnMedium_Click
    'WebViewSettings1.setDefaultZoom(WebView1,"MEDIUM")
    WebViewSettings1.setDefaultFontSize(WebView1,12)
End Sub

After observing that clicking on the buttons does not help, I shifted the code to Activity_Create, before the Webview1.LoadUrl.

B4A:
Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("1")

    'WebView1.Initialize("")

    'Activity.AddView(WebView1, 0, 0, -1, -1)

    WebView1.ZoomEnabled=True

    'WebViewSettings1.setDefaultZoom(WebView1,"CLOSE")

    'WebViewSettings1.setDefaultZoom(WebView1,"FAR")

    'WebViewSettings1.setDefaultFontSize(WebView1,22)

    WebViewSettings1.setDefaultFontSize(WebView1,5)

    WebView1.LoadUrl("https://www.b4x.com/android/forum/threads/webviewsettings.12929/")


    Activity.AddMenuItem("Zoom In", "ZoomIn")

    Activity.AddMenuItem("Zoom Out", "ZoomOut")       


End Sub

Ran it in Debug mode (with breakpoints ).
Uncomment a line, SAVE, Back Key and RESUME.

No change in behaviour. No zoom in or out.

Anything wrong in my approach ?

Regards,
KPS
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
There are a TON of settings in WebViewSettings. Some interact and depend on others, so it can be confusing.

Try these settings before and after other settings:

B4X:
wvs.setLoadWithOverviewMode(wv,True)    'TRUE this works best to do full screen for chrome browser

And this code I think will effect how much of a jump the zoom in/out will do:

B4X:
Sub SetZoomRate(rate As Int, wv As WebView)
    Dim r As Reflector
    r.Target=wv
    r.RunMethod2("setInitialScale",rate,"java.lang.int")
End Sub
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Please explain a little more of exactly you want to do:

1) You want to adjust the font size *after* the page loads?
2) Do you want the user to be able to adjust the font size while running your app?
3) In my Post #4, I think that option #2 and #3 need to be set *before* you load the page.

As a last resort if you can't figure out a way to adjust the font size while the page is loaded, then you could use the settings (that need to be set before a page is loaded), then simple reload the page to see the effects of the change. Its not an ideal solution, but better then nothing.
 
Upvote 0

kps

Member
@JohnC

Thank you for your response.
I am yet to try your suggestions in post #6. Will do my homework and respond.

In the meanwhile, my response to your post #7

1) You want to adjust the font size *after* the page loads?

Yes.
If you are reading this page in PC, just do a CTRL + (PLUS). You can also say CTRL = (since you don't have to press SHIFT).
You can keep doing it until the text becomes large enough to read comfortably. While the text becomes larger and larger, it does not go out of screen boundary. Instead, it wraps around.
For people with poor eyesight, this is an important feature.

2) Do you want the user to be able to adjust the font size while running your app?
Yes.
My app would open some HTML content in Webview. After the page loads with default font, the user may increase or decrease the font size to his comfort level.

3) In my Post #4, I think that option #2 and #3 need to be set *before* you load the page.

That's what I suspected and I did that too.

B4A:
'WebViewSettings1.setDefaultZoom(WebView1,"CLOSE")
WebViewSettings1.setDefaultFontSize(WebView1,22)
WebView1.LoadUrl("https://www.b4x.com/android/forum/threads/webviewsettings.12929/")

The page gets loaded when you execute "WebView1.LoadUrl", right ?

Regards,
KPS
 
Upvote 0

kps

Member
It appears that Webview obeys the Text Size setting in Phone -> Display ->Text Size Settings.

I have to find out how to override that using WebViewSettings.

Regards,
KPS
 
Upvote 0

kps

Member
A friend in another forum tells me it is a feature called "Text Reflow" and pointed me to this.

It appears it is a unique feature of Opera browser in Android phones.
Neither Firefox nor Chrome has it.
I suppose we can eventually figure out a way to incorporate it in my app.

Regards,
KPS
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
A friend in another forum tells me it is a feature called "Text Reflow" and pointed me to this.
I don't believe that is not a "standard" that browsers are "required" to support, so it's up to the browser maker to support it or not. But, unfortunetely it looks like WebView does not support that feature.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Upvote 0

JohnC

Expert
Licensed User
Longtime User
As a last resort if you can't figure out a way to adjust the font size while the page is loaded, then you could use the settings (that need to be set before a page is loaded), then simple reload the page to see the effects of the change. Its not an ideal solution, but better then nothing.
This might be the only solution that will sort of address your need.
 
Upvote 0

kps

Member
@JohnC

I tried the "setLoadWithOverviewMode" suggested in #6. I don't see any difference in behaviour.

As suggested in #13, I do use the LoadUrl statement after every setting change to reload the page.
I do not know if there is any other way to "reload" the page.

I could not try the SetZoomRate function as I am not familier with Reflection library or anything java related.
I did not know what value to use for "rate" parameter in RunMethod2

Thank you for taking all the trouble.

I have to explore the "inject Javascript" approach suggested by Erel in #2.
I have to search for some examples as I have no knowledge of JavaScript.

Regards,
KPS
 
Upvote 0
Top