AdvancedWebView loading html from phone storage issue - android

Hi i am using compile 'com.github.delight-im:Android-AdvancedWebView:v3.0.0' for display html5, my target file is stored in my phone storage path is "file:///storage/sdcard0/DcLms/Conjunction/index.html". when I am working with normal android WebView no issues. But trying with above mentioned library i got Webpage not available issue.
String targetPath = "file:///storage/sdcard0/DcLms/Conjunction/index.html";
AdvancedWebView webView = (AdvancedWebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new HelloWebViewClient());
webView.loadUrl(targetPath);
WebView basicView = (WebView) findViewById(R.id.basicView);
webView.setWebViewClient(new HelloWebViewClient());
basicView.getSettings().setJavaScriptEnabled(true);
basicView.setWebChromeClient(new WebChromeClient());
basicView.getSettings().setDomStorageEnabled(true);
basicView.loadUrl(targetPath);
basicView.setWebViewClient(new HelloWebViewClient());

First of all AdvancedWebView configures all settings automatically on it's instantiating. Take a look on the sources here.
And the problem is that by default AdvancedWebView dissallow access using file:// scheme:
...
final WebSettings webSettings = getSettings();
webSettings.setAllowFileAccess(false);
setAllowAccessFromFileUrls(webSettings, false);
webSettings.setBuiltInZoomControls(false);
...
So after creation of AdvancedWebView you need to add following lines:
AdvancedWebView webView = (AdvancedWebView) findViewById(R.id.webView);
webView.getSettings().setAllowFileAccess(true);
if (Build.VERSION.SDK_INT >= 16) {
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
NOTE: AdvancedWebView just subclassing WebView so it will not make any difference - the only difference that it configurating settings for you and handle some of the events.

Related

Android WebView white page after restart activity

I am using WebView to load a webpage in Android activity.
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.addJavascriptInterface(new Unlock(), "AndroidUnlock");
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/demo.html?ver=" + System.currentTimeMillis());
When the webview loaded, I pressed the back button to finish the activity.
And I launch the activity again, the webview load the webpage incorrectly. It shows a blank page to me.
How can I to fix the issue please?
If I use System.exit(0) to exit the activity, it works very well, the webview load the webpage correct again.
Regards
You are loading the URL in onCreate(). Put the code (except webView = (WebView) findViewById(R.id.webview);) in onResume() :
#Overrride
protected void onResume(){
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.addJavascriptInterface(new Unlock(), "AndroidUnlock");
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/demo.html?ver=" + System.currentTimeMillis());
}
Hope this helps.

android webview unset default browser

I use the following to show a webpage in a webview
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView)findViewById(R.id.webView);
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webView.loadUrl("http://www.bbc.co.uk");
}
It works perfect and even has pinch and zoom however When i click to go to another webpage via a link it opens it in the default browser instead of the webview
How to i achieve this. I have read many articles on this but cant understand where i need to insert the commands
Any help appreciated
Mark
thanks very much for the link above the following works perfect
myWebView.loadUrl("http://someurl.com");
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
viewx.loadUrl(urlx);
return false;
}
});

Android - can't seem to enable JavaScript on a WebView?

Is there a way to enable JavaScript in a WebView?
I am currently trying to enable JavaScript by doing this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
WebSettings webSettings = webview.getSettings();
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webview.setWebChromeClient(new WebChromeClient(){});
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
webview.loadUrl("http://www.youtube.com/user/Okudjavavich");
}
But JS seems to not be enabled because the YouTube videos do not play YouTube ads. Would anyone know what may be the problem?
Is there a way to enable JavaScript in a WebView?
Yes, via the code that you have:
webview.getSettings().setJavaScriptEnabled(true);
In your case, the problem was that your test (playing YouTube videos and looking for ads) was complicated. A simpler test helped determine that JavaScript indeed was enabled, but that something else was influencing Google's YouTube page-serving. In this case, it appears to be based on user agent.

Android WebView - code that can display and play a podcast can't play a YouTube video

I have this code which works to display a WebView and use it:
WebView webview = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
//setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
//setContentView(R.layout.podcasts);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//webSettings.setBuiltInZoomControls(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("url_to_go_to");
But when I try to use this code to point to a YouTube channel, it just shows a blank screen.
Any idea why that would happen?
Thanks!
In order to get HTML5 videos to show up you need to enable the following things in the WebView:
WebView view;
... //initialize WebView
WebSettings webViewSettings = view.getSettings();
view.setWebChromeClient(new WebChromeClient(){}); //just added this
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setAppCacheEnabled(true);
webViewSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webViewSettings.setDatabaseEnabled(true);
webViewSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
You also need to put android:hardwareAccelerated="true" in your AndroidManifest. Otherwise, HTML5 stuff like YouTube videos won't work.

WebView user agent

I have WebView which loads one mobile site, I need send to user agent to the server how it to realize?
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://searchmp3.mobi/");
mWebView.setWebViewClient(new HelloWebViewClient());
Checkout the setUserAgentString() method in the WebSettings, e.g.
mWebView.getSettings().setUserAgentString("My user agent string, here");
try this one
WebView wv = (WebView) findViewById(R.id.webview1);
WebSettings webSettings = wv.getSettings();
webSettings.setBuiltInZoomControls(true);
wv.loadUrl("http://www.google.com");

Categories

Resources