B4A Library Little WebView Extension v1.20

Surfing the internet over 3GP/UTMS/... is pretty slow and pretty expensive in my country. I want to have a tiny, fast web browser without not needed functions to keep data traffic as small as possible.

The WebView does not have a lot of properties to get control of traffic releated stuff. That's why i wrote this little library. The attachement contains a fully working example and shows all functions.

What you can do with it:
------------------------
- Get and set the state of accepting cookies
- Get and set the state of loading images
- Get and set the state of enabling javascript
- Get and set the state of enabling plugins
- Get the progress of current page loading
- Get the title of the current page
- Scrolling the current page half the page or full up and down
- Zoom in and out

I tested it a long time, it works perfect on my tablet with Android 3.2 and Smartphone with Android 2.3.7 (Custom Rom)

The meaning of the scrolling and zooming functions is simpel. I hold the tablet in one hand and don't want to wipe with my fingers all the time over the display. So I made a few buttons on the side of the webview. Now i can surf the internet with tapping a few buttons :)

Here is a screenshot of my selfmade tablet webbrowser:
http://www.thalmy.ch/diesunddas/minibrowser.jpg

Whats coming next:
-------------------
Get the favicon of the current page. This works right now, but i can not display the image because favicons are ICO bitmaps. I have to find out how I can convert them to BMP bitmaps. And I also want to read the OS Browser bookmarks wich are stored in an internal database.
 

Attachments

  • WebViewXtenderExample.zip
    279.6 KB · Views: 2,673

NeoTechni

Well-Known Member
Licensed User
Longtime User
Can you find a way to get the number of pages, so you can make an accurate scrollbar that shows where in the page you are?
 

Roeschti

Member
Licensed User
Longtime User
Yes, there is always only 1 page...so there are 3 options:
Top, middle, bottom

From the original documentation:

pageDown(boolean bottom)
Scroll the contents of the view down by half the page size
 

Roeschti

Member
Licensed User
Longtime User
I updated the library to v1.40

NEW in this release:

getHost:
return host name of given URL

getTitle:
Returns title of current page

getFaviconFromUrl:
Returns the favicon (a bitmap, not the ICO Bitmap!) of a given URL. You can specify a default icon, if URL has no favicon. I use it to show the bitamp in a imageview.

getFaviconFromUrl2:
Returns the favicon (a bitmap, not the ICO Bitmap!) of a given URL and does auto converting if URL has a favicon. If not, it returns default bitmap

scrollTo:
Set the scrolled position of your view in given x and y positions as int values.

scrollBy:
Move the scrolled position of your view in given x and y positions as int values.

getScrollX:
Return the scrolled left position of this view. This is the left edge of the displayed part of your view.

getScrollY:
Return the scrolled top position of this view. This is the top edge of the displayed part of your view

isHorizontalScrollBarEnabled:
Indicate whether the horizontal scrollbar should be drawn or not. The scrollbar is not drawn by default.

setHorizontalScrollBarEnabled:
Define whether the horizontal scrollbar should be drawn or not

isVerticalScrollBarEnabled:
Indicate whether the horizontal scrollbar should be drawn or not.

setVerticalScrollBarEnabled:
Define whether the vertical scrollbar should be drawn or not

setScrollBarStyle:
Specify the style of the scrollbars.

What is the meaning of this scroll functions? I hate to scroll all the time down on many pages, if they have a large header like Basic4android (Basic for Android) - Rapid Application Development :) if you wanna surf the forum here, you have to scroll down third part of the display to see the first thread, and this every time. So I scroll down once to the beginning of the forum, read with "getScrollX" the x position and store it together with the host name. From this moment on after page load is finished, it scrolls automaticly down to the stored position with "scrollTo". This works in all directons (left, right, up and down)

Sorry for my bad english, I was never in the mood to learn it correctly :)
 

Attachments

  • WebViewExtended_v1.40_lib.zip
    6.3 KB · Views: 955
Last edited:

Roeschti

Member
Licensed User
Longtime User
Can you find a way to get the number of pages, so you can make an accurate scrollbar that shows where in the page you are?

Yes it is now possible with v1.4

Scroll down with pageDown(True), read the X-Pos with getScrollX so you have the height of the page. Now u can do what ever you want with this values.
 

moster67

Expert
Licensed User
Longtime User
Thanks for sharing this library. :sign0098:

It will surely be useful later on. Really nice with those "extra" you added, especially the scrolling.

The zoom-functions in your sample-app did not work. I need to test it on another phone to see if it depends on my phone or not.

I couldn't see your library in the WIKI-list of available libraries. Why don't you ask Erel to give you access so you may add your library to the list?

Keep up the good work! Hope to see more nice libraries from you.
 

Roeschti

Member
Licensed User
Longtime User
@Erel:
Got it, thanks a lot!

@moster67:
Well, I just forgot to ask Erel about the wiki :eek:
Zooming is normally a question about the page, not the device. There are some pages wich can't be zoomed, even with the OS browser. Next release of the library contains a property "isZoomable" that returns true if so , or false if page can not be zoomed.
 

Inman

Well-Known Member
Licensed User
Longtime User
Great work. I always wondered if it was possible to know the exact scroll position of a web page. Now it is possible, thanks to your work.
 

Roeschti

Member
Licensed User
Longtime User
New Release v1.50

I added some more functions and added the lib to the list / wiki

New in this release:

getScale:
Return the current scale of the WebView

setInitialScale(int scaleInPercent):
Set the initial scale in percent for the WebView. Useful if your page / loaded html does not fit exaclty to the WebView

canZoomIn:
Returns TRUE if the WebView can be zoomed in

canZoomOut:
Returns TRUE if the WebView can be zoomed out

resumeTimers:
Resume all layout, parsing, and JavaScript timers for all webviews. This will resume dispatching all timers. Prevent your app from hanging :)

clearFocus:
Removes the focus of an given view (WebView, EditText, Button...) i use this if i fill a EditText with code without showing the keyboard. For example if you have an EditText, fill it with something like EditText.Text = "Hooldrio", then the EditText keeps the focus and the keyboard shows up. So after I filled the EditText I use the clearFocus(MyEditText) to remove the focus, so the keyboard does not pop up. I'm not sure if this works on all devices, but it does on my tablet with API 13 / Android 3.2 :)

I did not have updated the example from the first post.
 

Attachments

  • WebviewExtended_v1.50_lib.zip
    7.2 KB · Views: 1,208

peacemaker

Expert
Licensed User
Longtime User
How about catching standard HTTP errors in WebView ? 404 or other errors.
 

Roeschti

Member
Licensed User
Longtime User
This is really funny cause I work on the same problem right now :D

I found a solution but I don't like it. You can use the HTTP lib and do it as follows:

Let's say you want to check http://www.filpro.de/gcode/nixda.htm
This produces a 404 error, cause the page nixda.htm doesn't exist.

Dim hc as HttpClient
dim strUrl as String
strUrl = "http://www.filpro.de/gcode/nixda.htm"

SendRequest(strUrl, 99)

Sub SendRequest(strURL As String, intID As Int)
Dim req As HttpRequest
req.InitializeGet(strURL)
hc.Execute(req, intID)
End Sub

Now the "witchcraft stuff"

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim intRC as int 'Holder for the Retrun Code

If TaskId = 99 Then
intRC = Response.StatusCode

If intRC <> 404 , 500..... Then webView1.loadURL(strUrl)
End if
End Sub

A list of the common http status code can be found here:
HTTP/1.1: Status Code Definitions

To check the return code you can use some IF statements or Select or just
if intRC > 200 then Problem :D

You see why I don't like this solution, you have to laod every URL twice, first for a pre-check then if everything is ok load it again in the webview.

I will try to find out how I can implement this in my library.
 

warwound

Expert
Licensed User
Longtime User
I've been following this request for a way to detect if a 404 error occurs in a WebView and it's not as simple as we'd like...

If you try to load a webpage that does not exist then in the WebView PageFinished Sub there is no indication that a 404 file not found error has occurred.

A Google search shows others have asked this same question: android webview detect 404 error - Google Search.

Some suggestions are to add a WebViewClient to the WebView and override the onReceivedError() method.

But after a little googling it appears that the onReceivedError method does not handle HTTP errors such as 404 File not found.
It handles other errors: How can i check from android webview if a page is a "404 page not found" - Stack Overflow.

Opinion on that page differs - some say onReceivedError handles HTTP errors and others say that although the official documentation implies that it does the truth is that it does NOT!

The B4A WebView already has a WebViewClient added to it by default.
That default WebViewClient does not override the onReceivedError method.
It does however override the onPageFinished, shouldOverrideUrlLoading and onReceivedHttpAuthRequest methods.

If a library added a new WebViewClient to a WebView then i think that new WebViewClient would replace the existing default WebViewClient - so the new WebViewClient would have to replicate the existing overridden methods of the default B4A WebViewClient to ensure compatibility with existing B4A code.

It looks to me that using an HttpClient to check for the existence of a webpage before loading it into a WebView is the only way - but i'd like to be proved wrong!

Martin.
 

rbsoft

Active Member
Licensed User
Longtime User
Hi Roeschti,

thanks for this useful library.
It might be a good idea to upload the latest library always to the first message in the thread. Just an idea...

Rolf
 

Roeschti

Member
Licensed User
Longtime User
GetProgress is implemented since v1.2

HitTestResult: I will implement this soon.
 

Gargoyle

Member
Licensed User
Longtime User
Is there any way to get the whole scroll height of a webview and what value a pageDown would scroll by?

Also if I do a pageDown (from top), and then get the getScrollY the value still reads as 0 - as I think the view position hasn't re-positioned quickly enough, so is there a way to delay the reading of the value until after the scrolling.

B4X:
WebViewXtended1.pageDown (WebView1, True)
ToastMessageShow("Y=" & WebViewXtended1.getScrollY(WebView1),False)
 

Arun

Member
Licensed User
Longtime User
Error compiling example

Thanks for the effort you have put into this library and a bigger thankyou for offering it free.

I needed a way to show actual progress of loading a URL and thought of using this lib but the example is giving me the following error when I compile and run as is

B4X:
Compiling code.                         0.14
Compiling layouts code.                 0.02
Generating R file.                      0.47
Compiling generated Java code.          Error
javac 1.7.0_03
src\thalmy\webviewxtended\main.java:227: error: cannot find symbol
public thalmy.webviewxtended.xtender _wvxtender = null;
                            ^
  symbol:   class xtender
  location: package thalmy.webviewxtended
1 error

What am i missing? I am using a 7" tablet running ICS

Thanks in advance!
 

RJB

Active Member
Licensed User
Longtime User
Hi, I can't get scrollby to work, i.e. wvXtender.scrollTo(WebView1, 0, 10) moves the display as expected and increasing the 10 to 20 moves it more, again as expected, but wvXtender.scrollby(WebView1, 0, 10) doesn't seem to do anything. I'm presuming it should move the display in a similar way to scrollto but relative to the existing position rather than to an absolute position?
Any suggestions?
Thanks
 
Top