When playing twitter video in android web view it shows the message 'by playing this video you agree to the Twitter use of cookies' and video is not played after that.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
} else
CookieManager.getInstance().setAcceptCookie(true);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(Color.parseColor("#ffffff"));
webView.loadDataWithBaseURL("file:///android_asset/", htmlTemplate, "text/html", "UTF-8", "");
JavaScript is disabled in a WebView by default.
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
UPDATED:
Use TweetView for Native Videos
Launching native video support for Twitter Kit
Call loadDataWithBaseURL instead of loadData, like this:
loadDataWithBaseURL("https://mywebsite.com", mContent, "text/html", "UTF-8", null);
I have the code for playing YouTube in WebView on Android.
It works well on new phones (Android 4.4, 5.1, 6, 7 OS) but when I tried it on Android 4.0.3 it opens the YouTube frame with controls and after clicking on button play in the midle the gray background is shown all the time.
The code:
private void initializeWebView() {
WebView webView = (WebView) findViewById(R.id.webView);
//webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.setBackgroundColor(getResources().getColor(R.color.semi_transparent_black_20percentage));
webView.getSettings().setBuiltInZoomControls(true);
//webView.getSettings().setDisplayZoomControls(false);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(false);
//webSetting.setLoadWithOverviewMode(true);
//webSetting.setUseWideViewPort(true);
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
webView.setLayoutParams(new RelativeLayout.LayoutParams( screenWidth / 2, LayoutParams.MATCH_PARENT));
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webSetting.setJavaScriptEnabled(true);
webSetting.setBuiltInZoomControls(true);
webSetting.setAllowContentAccess(true);
webSetting.setEnableSmoothTransition(true);
webSetting.setLoadsImagesAutomatically(true);
webSetting.setLoadWithOverviewMode(true);
webSetting.setSupportZoom(false);
webSetting.setUseWideViewPort(true);
webSetting.setAppCacheEnabled(true);
webSetting.setSupportMultipleWindows(true);
webView.setVisibility(View.VISIBLE);
}
public void loadPageIntoWebView(String htmlFilename){
AssetManager mgr = getBaseContext().getAssets();
try {
String htmlContentInStringFormat = "";
WebView webView = (WebView) findViewById(R.id.webView);
String frameVideo = "<body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/2a7f29Jvihc?wmode=transparent\" frameborder=\"0\" allowtransparency></iframe></body>";
htmlContentInStringFormat = frameVideo;
webView.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);
} catch (Exception e) {
e.printStackTrace();
}
}
This code is working well and playing YouTube videos on newer phones (Android OS4.4-7).
What I should change to get it working on Android 4.0.3 as well ?
this is the photo showing the gray background without playing youtubevideo after clicking on the button play inthe middle of the screen:
Solved!
After I added the following line it works on Android 4.0.3 aswell:
webView.setWebChromeClient(new WebChromeClient() {});
It is not mandatory but I added the line before this line:
webView.setWebViewClient(new WebViewClient()
Hope that this solution will help other collegues working with Epson Moverio BT-200 smart glasses etc.
;-)
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);
I've been having issues trying to get webview from stop zooming in when a user clicks a input field on webview. How can I keep webview from stop zooming in?
Heres my code:
wv = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wv.getSettings();
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setDisplayZoomControls(false);
wv.getSettings().setBuiltInZoomControls(false);
wv.getSettings().setSupportZoom (true);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDefaultZoom(ZoomDensity.FAR);
String form = "<form action='demo_form.asp'><input type='text' name='email' style='font-size: 200%;width:78%;float:left'><input type='submit' value='Submit' style='float:right;width:20%;font-size: 200%'></form>";
String summary = "<html><body>"+form+"</body></html>";
wv.loadData(summary, "text/html", null);
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