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

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);

Related

streaming in webview (android)

I am trying to play livestream TV in a WebView. WebView shows a black screen livestream area in page, but it is not loading and playing the stream.
wv.setWebChromeClient(new WebChromeClient()); (mainactivity)
and
android:hardwareAccelerated="true" (manifest.xml) doesn't work
wv = (WebView) findViewById(R.id.wv);
wv.setWebChromeClient(new WebChromeClient());
wv.getSettings().setPluginState(WebSettings.PluginState.ON);
wv.getSettings().setJavaScriptEnabled(true);
wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setAppCacheEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl("http://www.fox.com.tr/canli-yayin");
wv.setWebViewClient(new WebViewClient());
All Live streaming is not supported in Mobile.So first check whether your URL work in chrome browser. if it is working then you can use the custom tab
compile 'com.android.support:customtabs:23.3.0'
String url = ¨http://www.fox.com.tr/canli-yayin¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
You have to set this webview settings for the video stream
mWebview.Settings.JavaScriptEnabled = true;
mWebview.Settings.SupportZoom();
mWebview.Settings.SetAppCacheEnabled(true);
mWebview.Settings.DomStorageEnabled = true;
mWebview.ZoomOut();
mWebview.ZoomIn();
mWebview.Settings.BuiltInZoomControls = true;
mWebview.Settings.LoadWithOverviewMode = true;
mWebview.Settings.UseWideViewPort = true;
if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)
mWebview.Settings.MixedContentMode = MixedContentHandling.CompatibilityMode;
mWebview.Settings.SetPluginState(WebSettings.PluginState.On);
mWebview.Settings.GetPluginState();
mWebview.Settings.AllowFileAccess = true;
And if you want to open the stream with in the application then setwebviewclient of webview.
mWebview.SetWebViewClient(new WebViewClient());
Event if it does not work then please set user agent string of webview
webview.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/_BuildID_) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36");

Force webview to display desktop sites

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");

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