Detect Zoom Event for WebView

cbanks

Active Member
Licensed User
Longtime User
How do I detect if a user tapped the + button on a WebView to zoom in? Thanks.
 

warwound

Expert
Licensed User
Longtime User
It works!

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("")
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebViewExtras1.Initialize(WebView1)
   
   Dim WebViewClient1 As WebViewClient
   WebViewClient1.Initialize("WebViewClient1")
   
   WebViewExtras1.SetWebViewClient(WebViewClient1)
   
   WebViewExtras1.LoadUrl("http://google.co.uk/")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub WebViewClient1_ScaleChanged(OldScale As Float, NewScale As Float)
   Log("WebViewClient1_ScaleChanged from "&OldScale&" to "&NewScale)
End Sub

A pinch zoom causes no log messages but a click on the WebView zoom buttons causes log messages such as:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
WebViewClient1_ScaleChanged from 1 to 1.25
WebViewClient1_ScaleChanged from 1.25 to 1
WebViewClient1_ScaleChanged from 1 to 1.0302197933197021
WebViewClient1_ScaleChanged from 1.0302197933197021 to 1.0563380718231201
WebViewClient1_ScaleChanged from 1.0563380718231201 to 1.07758629322052
WebViewClient1_ScaleChanged from 1.07758629322052 to 1.121076226234436
WebViewClient1_ScaleChanged from 1.121076226234436 to 1.14503812789917
WebViewClient1_ScaleChanged from 1.14503812789917 to 1.1700468063354492
WebViewClient1_ScaleChanged from 1.1700468063354492 to 1.1980830430984497
WebViewClient1_ScaleChanged from 1.1980830430984497 to 1.2396694421768188
WebViewClient1_ScaleChanged from 1.2396694421768188 to 1.25

All of those log messages are from a single click on a zoom button - as the WebView animates and zooms the scale changes repeatedly.

The other solution would be to disable the WebView's built in zoom controls and create your own - leaving you free to do whatever you want when clicked.

Demo project and beta version of my new WebViewExtras attached.

Martin.
 

Attachments

  • 20130604 ScaleChanged.zip
    42.9 KB · Views: 286
Upvote 0
Top