How to make WebView display web contents made for large screens

alexb

Member
Licensed User
Longtime User
I have searched the forum for an answer to this question but I could not find one, hope somebody knows the solution:

When using WebView to display the contents of a website - for example of a daily newspaper - targeted websites often automatically redirect to pages made for small mobile devices. This is fine and practical when having a small WebView running just on an Android phone. However when using the same WebView on a large Android tablet (e.g. 1024x600) this looks odd. In fact on such large screen devices it would be much better to display the contents made for regular notebook/desktop screens.

I there a way to configure WebView so that WEB contents made for large screens is displayed and can the behaviour be changed dynamically within the session of a program?:sign0163:
 

warwound

Expert
Licensed User
Longtime User
Hi.

I think you need to look at the user agent that the browser is identified as by the webserver.

If you open the native Android browser and in the address bar enter about:debug, now go to the browser settings.
You should see a few more options available and one of those will be UAString.
On my ZTE Blade (with a 2.2 custom ROM) i can choose between 'Android', 'Desktop' and 'iPhone'.

I'm pretty sure this is the setting you'd need to change in the WebView but can't offer any help as to how to do it.

Maybe if the user changes the UAString in the native browser then that setting will apply to a B4A WebView - i'll leave you to test that.

More info: android uastring - Google Search

Martin.
 
Upvote 0

alexb

Member
Licensed User
Longtime User
Martin, thanks for your interesting comment. I have looked up the settings of the standard Android Browser and indeed I found an option 'Desktop User Agent' that decides in which mode (Desktop or Mobile) the WEB pages will be opened. However, the option was already on my Archos 101 activated(Web pages were always displayed in Desktop mode in the standard Browser) and further tests with it also didn't have any impact on the WebView. So it seems that this parameter has no effect on how contents is displayed within the WebView. Pity!

Erel, can you comment on this? Would it be possible maybe to 'tune' the WebView with some Java code and Agrahams reflector library???

Thanks!

alex
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Try this code.
B4X:
   Dim Obj1 As Reflector
   Obj1.Target = Webv
   Msgbox(Obj1.ToString, "Target")
   Obj1.Target = Obj1.RunMethod("getSettings")
   Msgbox(Obj1.ToString, "Settings")
   
   Msgbox(Obj1.RunMethod("getUserAgentString"), "User agent")
   
   Obj1.RunMethod2("setUserAgentString", "Desktop", "java.lang.String")
 
Upvote 0

alexb

Member
Licensed User
Longtime User
Agraham, sorry fo the late reply, haven't been able to work on any code until just now. IT WORKS! - GREAT!

THANK YOU!:sign0098:
 
Upvote 0

Tyler

Member
Licensed User
Longtime User
Try this code.
B4X:
   Dim Obj1 As Reflector
   Obj1.Target = Webv
   Msgbox(Obj1.ToString, "Target")
   Obj1.Target = Obj1.RunMethod("getSettings")
   Msgbox(Obj1.ToString, "Settings")
 
   Msgbox(Obj1.RunMethod("getUserAgentString"), "User agent")
 
   Obj1.RunMethod2("setUserAgentString", "Desktop", "java.lang.String")
Hey Agraham, just wondering what other possible values there are besides "Desktop"? I would assume "Mobile" but am unsure. Currently checking developer.android.com but no luck so far.
 
Last edited:
Upvote 0
Top