Force webview to display desktop sites - android

I have the below webview client which sets the user agent to a desktop browser when we are viewing a page that does not contain the word google in the URL. (Also does other stuff but that all works fine).
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.contains("google")) {
String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
webView.getSettings().setUserAgentString(newUA);
view.loadUrl(url);
}else {
webView.getSettings().setUserAgentString(null);
view.loadUrl(url);
}
return true;
}
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
String page = webView.getUrl();
if (!(page.contains("google"))){
grabit.setVisibility(View.VISIBLE);
}else{
grabit.setVisibility(View.GONE);
}
webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('"+CFG.Bookmarklet+"');");
progressBar.setVisibility(View.INVISIBLE);
if (webView.canGoBack()){
left.setImageResource(R.drawable.ic_arrowleft);
}else{
left.setImageResource(R.drawable.ic_arrowleft_gray);
}
if (webView.canGoForward()){
right.setImageResource(R.drawable.ic_arrowright);
}else{
right.setImageResource(R.drawable.ic_arrowright_gray);
}
}
});
This issue with this is that while on some sites it works perfectly others it does not work and on some it seems to just change the view port.
A few examples are:
> Argos - shows mobile
> Tesco - shows mobile but view port has changed
> Amazon - works
> John Lewis - shows mobile but view port has changes
> Play.com - works
So is there something I am missing? Another way the websites it does not work on are checking the browser to decide what to display?
It would seem that the 'show desktop version' in Chrome works fine for these sites.. so prehaps chrome does something else to?
Thanks

You can use setDesktopMode(true) from this WebView subclass or read how it's implemented in detail.
This way, you don't have to set a fixed user-agent string. Moreover, setting the user-agent string only is usually not enough. It depends on the site, of course, since every website uses their own way of determining whether the client is on mobile or desktop.

You Can try this
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0");

The only solution which worked for me (javascript will be executed many times, but this is the only working solution for now)
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
view.evaluateJavascript("document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));", null);
}
You can set desktop UA string too
webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

Related

Enabling desktop site in webview (android app) [duplicate]

I've done quite a lot of research on Stack Overflow and a lot of Google research but nothing I find is actually working out for me. I want the site to view the desktop site instead of the mobile site. How do I do this? I want it to directly go to the Desktop site.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.apotter96.webs.com/");
}
Change the user agent of webview
String newUA="Foo/"; // Change this to desired UA
like
String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0";
mWebView.getSettings().setUserAgentString(newUA);
This method helps you to set DesktopMode on webview
public void setDesktopMode(WebView webView,boolean enabled) {
String newUserAgent = webView.getSettings().getUserAgentString();
if (enabled) {
try {
String ua = webView.getSettings().getUserAgentString();
String androidOSString = webView.getSettings().getUserAgentString().substring(ua.indexOf("("), ua.indexOf(")") + 1);
newUserAgent = webView.getSettings().getUserAgentString().replace(androidOSString, "(X11; Linux x86_64)");
} catch (Exception e) {
e.printStackTrace();
}
} else {
newUserAgent = null;
}
webView.getSettings().setUserAgentString(newUserAgent);
webView.getSettings().setUseWideViewPort(enabled);
webView.getSettings().setLoadWithOverviewMode(enabled);
webView.reload();
}
Call it like that
Mobile mode : setDesktopMode(webView, false);
Desktop mode : setDesktopMode(webView, true);
For Kotlin:
fun setDesktopMode(webView: WebView, enabled: Boolean) {
var newUserAgent: String? = webView.settings.userAgentString
if (enabled) {
try {
val ua: String = webView.settings.userAgentString
val androidOSString: String = webView.settings.userAgentString.substring(
ua.indexOf("("),
ua.indexOf(")") + 1
)
newUserAgent = webView.settings.userAgentString.replace(androidOSString, "(X11; Linux x86_64)")
} catch (e: Exception) {
e.printStackTrace()
}
} else {
newUserAgent = null
}
webView.settings.apply {
userAgentString = newUserAgent
useWideViewPort = enabled
loadWithOverviewMode = enabled
}
webView.reload()
}
You can use WebView to show view as Desktop Site with fit in mobile display.
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
The only solution which worked for me (javascript will be executed many times, but this is the only working solution for now)
#Override
public void onLoadResource(WebView view, String url) {
view.evaluateJavascript("document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));", null);
}
You can set desktop UA string too
webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
Some sites don't use User Agent to determine if then have to show the mobile or the desktop version of the Page.
Some pages uses screen size to do this.
I build an app to use a page in desktop mode, but it doesn't work properly. Always show the mobile version because the page uses screen size and not User Agent String.
A little update to accepted answer.This is the new string. Wrote this because someone had an issue of "Update Browser" in the comments.
String newUA= "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
mWebView.getSettings().setUserAgentString(newUA);
If you have update browser error you can try this to set apple safari UA or replace UA with Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
This worked 100% for me.
webview =(WebView)findViewById(R.id.webView);
webview.getSettings().setMinimumFontSize(12);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(false);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setScrollbarFadingEnabled(false);
String newUA= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Safari/602.1.50";
webview.getSettings().setUserAgentString(newUA);
webview.loadUrl("https://solveforum.com");
You need to change the user agent : http://developer.android.com/reference/android/webkit/WebSettings.html#setUserAgentString(java.lang.String)
Here is an example :
Loading html data in WebView
After long search this worked for me -
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
Try with this
String ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
This worked for me
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("https://web.whatsapp.com/");
String userAgent = webView.getSettings().getUserAgentString();
try {
String androidString = webView.getSettings().getUserAgentString().
substring(userAgent.indexOf("("),userAgent.indexOf(")")+ 1);
userAgent = webView.getSettings().getUserAgentString().replace(androidString,"X11; Linux x86_64");
}catch (Exception e){
e.printStackTrace();
}
webView.getSettings().setUserAgentString(userAgent);
webView.reload();
The easiest way is in Java:
- Mobile / Phone Mode
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setInitialScale(110);
Desktop Mode
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);

Webview InputText not supported in 2.1 and 2.2

In Android we have an url
"http://offers.bartizan.com/we-want-ileads-at-our-shows"
which contains lot of data.
We load this url in webview, In android 2.3 and onwards its working fine means diplay all content of url data but in android 2.1 and 2.2 not display Input Text form.
Here is my code which i used to load url in webview.
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
webView.enablePlatformNotifications();
webView.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_DEFAULT);
webView.loadUrl("http://offers.bartizan.com/we-want-ileads-at-our-shows");
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//if(url.contains("http://www.youtube.com/embed/")){
Log.d("WBC", "This is a YouTube link: " + url);
Log.e("url---Video->", url);
view.loadUrl(url);
return true;
} });
class MyJavaScriptInterface {
public void showHTML(String html) {
String htmlString = html;
Log.e("<---MyJavaScriptInterface---showHTML----->", htmlString);
}
}
Below form not display in android OS. 2.1 and 2.2
First name *
Last name
Email address *
List the names of your shows here. Please include the show manager name, email and phone. *
1) Showing blank area above image
2) Textbox we require

How to extract text from html page?

How to extract text from html page? For example the web page is the link http://www.atempodihockey.it/campionati/campionati-hil/serie-a1-2013-2014/calendario.html from I want to take the text. I must have the name of the team and the resoult of the match
I think below code can help u
webView = (WebView) findViewById(R.id.webterms);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings()
.setUserAgentString(
"Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
after creating your webview load your url or html page
webView.addJavascriptInterface(new MyJavaScriptInterface(),"HTMLOUT");
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url1) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
webView.loadUrl("javascript:window.HTMLOUT.processHTML(document.documentElement.innerText);");
}
});
webView.loadUrl(url);
Then create a class which has a one method for processing your html
class MyJavaScriptInterface {
public void processHTML(String html) {
if (null != html && html.trim().length() > 0) {
System.out.println("your Html ->" + html);
}
}
For this purpose, you can use HtmlAgilityPack
Do it as follwing...
Add reference of HtmlAgilityPack in your project.
using HtmlAgilityPack;
and then put the url to get the full page
HtmlWeb webGet = new HtmlWeb();
HtmlDocument document = webGet.Load("http://www.atempodihockey.it/campionati/campionati-hil/serie-a1-2013-2014/calendario.html");
From the html of 'document' variable you can get your expected text

Android webView blinks

The webview in my app (where i present some RSS news) uses to blink when i scroll,or sometimes if i read the article.
How can i fix it?
the problem is the same in my 4.1.1 device and in the emulator.
this is my webview:
final WebView desc = (WebView) view.findViewById(R.id.desc);
desc.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// super.onPageFinished(view, url);
desc.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('img')[0].style.display = 'none'; "
+ "})()");
}
});
// Set webview properties
WebSettings ws = desc.getSettings();
ws.setSupportZoom(true);
// ws.setDisplayZoomControls(true);
ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
ws.setLightTouchEnabled(false);
ws.setPluginState(PluginState.ON);
ws.setJavaScriptEnabled(true);
ws.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");
// ws.setLoadsImagesAutomatically(false);
// desc.requestFocus(View.FOCUS_DOWN);
desc.loadDataWithBaseURL("", DESC,
"text/html", "UTF-8", null);
put android:hardwareAccelerated="false" to that activity.
I hope it may helps
Thanks,
Chaitanya.K
when you set android:hardwareAccelerated="false" to that activity,you'd better not use animation in that activity.otherwise animation will be so bad.
you could set webview.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
it also close hardwareAccelerated

Webview not being seen as a mobile browser

In my webview I am going to http://www.nsopw.gov/Core/OffenderSearchCriteria.aspx
when this is done via the android browser it is seen as a mobile site.
But in my app in a webview it is seen as not a mobile browser therefore not being redirected to the mobile version.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.getSettings().setSupportZoom(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.getSettings().setDomStorageEnabled(true);
String[] loading = getResources().getStringArray(R.array.array_loading);
Random r = new Random();
int rN = r.nextInt(12 - 1) + 1;
progressBar = ProgressDialog.show(Sex_Offenders.this, loading[rN],
"Loading...");
final String urlToLoad ="http://www.nsopw.gov/Core/OffenderSearchCriteria.aspx";
//final String urlToLoad = "http://m.familywatchdog.us/m_v2/msa.asp?es=&l=0&w=0&brtp=html&rstp=xlarge&imgtp=jpg&imgw=310&imgh=320";
wb.setWebViewClient(new HelloWebViewClient() {
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
Context context = getApplicationContext();
if (Repeatables.isNetworkAvailable(context) == true) {
wb.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
wb.loadUrl(urlToLoad);
} else {
wb.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
wb.loadUrl(urlToLoad);
Repeatables.NoConnectionAlert(this);
}
setContentView(wb);
try forcing the user agent string yourself, as it appears that the webview user agent string is not identifying itself as a mobile browser.
use
webview.getSettings.setUserAgentString(...)
You can google for the useragent string, I used
Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
and it worked with the link you provided in your site and loaded the mobile version.

Categories

Resources