Loading a WebView URL before splashscreen finishes - javascript

I followed this tutorial https://www.youtube.com/watch?v=QD9nhyWX-gs to make an animated splashscreen which is just my logo doing one 360˚ spin which text and a background below it after the spin it opens a webview which displays google.com.au but it has that white background while it loads the webpage and i need it to load the webpage beforehand in the splashscreen and keep the animation repeating till the website it loaded, the code is fairly simple its just a rotating imageview.
Just tried:
public class WelcomeActivity extends Activity {
WebView web;
ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
web = (WebView) findViewById(R.id.webView);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.voxelservers.net");
imageView = (ImageView) findViewById(R.id.imageView);
}
public class myWebClient extends WebViewClient
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
imageView.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Its just a black screen.

One way we can achieve this is by rather than opening a new activity for webview,
In WelcomeActivity.java itself add webView and imageView to the layout and make webview to invisible or Gone.
On open of WelcomeActivity initialize webView and set custom WebViewClient. Override onPageFinished() in your Custom webViewClient and in this method make webview visible and imageView to Gone.

Related

Site don't want loading at api <19(KitKat)

I need to open the site in my application, but it is loaded only on devices with Android 4.4 and newer. But I need to site opened and with older versions of devices. I tried to connect the HTML5, but it seems made a mistake somewhere. The site contains HTML5 and JS code. Here's an example of my Activiti
WebView web;
ProgressBar progressBar;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
setView();
initToolbar();
web = (WebView) findViewById(R.id.webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setProgress(0);
web.setWebChromeClient(new myWebChromeClient());
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setDisplayZoomControls(true);
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
web.getSettings().setPluginState(WebSettings.PluginState.ON);
enableHTML5AppCache();
web.loadUrl("https://mobile.akbars.ru");
}
private void enableHTML5AppCache() {
web.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
web.getSettings().setAppCacheMaxSize(1024*1024*8);
web.getSettings().setAppCachePath("/data/data/"+ getPackageName() +"/cache");
web.getSettings().setAllowFileAccess(true);
web.getSettings().setAppCacheEnabled(true);
web.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
}
public void setValue(int progress) {
this.progressBar.setProgress(progress);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("ACTIVTTY", "change rotation");
}
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
/*#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}*/
/*#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}*/
}
public class myWebChromeClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
WebViewActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
At Android 5.0 and later this site can open in default browser but in early versions of Android it open only in google chrome.
Is there a way to inherit Google Chrome in my webview?
Or some another ideas to open this site?
SITE: https://mobile.akbars.ru
In my opinion, no there is no way to inherit Google Chrome in my webview (older versions). Only simple webview is the solution. According to the version, HTML5 support also need to be checked.
I think you can use https://crosswalk-project.org/documentation/android.html
Reliably build responsive user interfaces with Flexbox. Your app will render correctly on any Android 4.0+ device when you use the Crosswalk Project.

Remove some part of a webpage in android webview and then display it

I have been able to successfully remove a part of a webpage using the below code, but the only problem is that the webview first display the complete webpage and then it removes the element 'header-text-nav-container' although I am calling it onPageFinished(). I tried many different ways but all in vain. How to display the webpage in the webview only after successfully removing the element 'some-part' in my case 'header-text-nav-container'. Please help
public class myWebClient extends WebViewClient
{
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:(function() { " +
"document.getElementById('header-text-nav-container').style.display='none'; " +
"})()");
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
}
I finally solved the problem by showing a progress dialog and then dismissing the progress dialog when the 'header' is successfully removed. Thus I solved the problem partially.

Webview load a pdf file display a text look like a blur in Android

isssue in Webview
I load pdf files using this url inside a webview " http://staging-argo.ephronsystems.com/content.php?type=view&docType=category&fileId=49&menuID=44"
in this page contains touch jquery plugin.
Thanks for help
You can use original pdf file http://staging-argo.ephronsystems.com/admin/uploads/category_files/ARGO BOND REQUEST FORM.pdf
and for viewing purpose you can use Google Docs Viewer
https://docs.google.com/viewer?url=http://staging-argo.ephronsystems.com/admin/uploads/category_files/ARGO BOND REQUEST FORM.pdf
try this.
web=(WebView)findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setPluginState(WebSettings.PluginState.ON);
web.getSettings().setAllowFileAccess(true);
web.getSettings().setSupportZoom(false);
web.setWebViewClient(new WebViewClient());
loadUrl();
And loadUrl() ->
#JavascriptInterface
public void loadUrl() {
web.post(new Runnable() {
#Override
public void run() {
web.loadUrl(url);
}
});
}
And this.
private class WebViewClient extends android.webkit.WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return true;
}
}

Enabling Javascript in webview?

I am trying to get javascript to be enable, i thought i had it right but when i run it on my phone it seems to not load up the javascript. I also am wondering how i can load external links outside the webview, because right now i have the override to only load in my webview, how do i go about flipping it to where anything that isnt from my site to load externally?
Do i have it in the wrong place?
package com.webapp.area956;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebViewClient yourWebClient = new WebViewClient()
{
// Override page so it's load on my view only
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// This line we let me load only pages inside Firstdroid Webpage
if ( url.contains("area956") == true )
// Load new URL Don't override URL Link
return false;
// Return true to override url loading (In this case do nothing).
return true;
}
};
String url = "http://www.area956.com";
WebView view = (WebView) this.findViewById(R.id.webView1);
view.setWebViewClient(yourWebClient);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
WebView view = (WebView) findViewById(R.id.webview1);
view = (WebView) findViewById(R.id.webview);
view .setWebChromeClient(new InredisChromeClient(this));
view .setWebViewClient(new InredisWebViewClient(this));
view .clearCache(true);
view .clearHistory();
view .getSettings().setJavaScriptEnabled(true);
view .getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
set these properties

webview don't display javascript windows.open()

I have a WebView in which i display web content which i have no control over. The content displays fine, but have links which spawn a popup window. The javascript function that does that looks like this:
function getEntry(id) {
var win = window.open('', 'Booking',
'resizable=yes,scrollbars=yes,status=no,width=500,height=400');
win.document.location = '/some/url/1-' + id ;
}
I can't easily change this, and if the people responsible for the page i download would change it, i guess my app would fail miserably...
My WebView setup in the activity looks like this:
_webview = new WebView(this);
setContentView(_webview);
final Activity activity = this;
_chromeClient = new MyChromeClient();
_webview.setWebChromeClient(_chromeClient);
//I experimented with changing user agent, in case that would have any effect, it didn't...
_webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
_webview.setWebViewClient(new MyWebViewClient());
_webview.getSettings().setJavaScriptEnabled(true);
_webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
_webview.getSettings().setSupportZoom(true);
_webview.getSettings().setBuiltInZoomControls(true);
_webview.getSettings().setDomStorageEnabled(true);
//Cache settings...
_webview.getSettings().setAppCacheMaxSize(1024*1024*8);
_webview.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache");
_webview.getSettings().setAllowFileAccess(true);
_webview.getSettings().setAppCacheEnabled(true);
MyWebClient:
private class MyWebViewClient extends WebViewClient {
#Override
public void onLoadResource(WebView view, String url) {
Log.d("MyWebViewClient",url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
showProgressDiag();
Log.d("MyWebViewClient","shouldOverride... : " + url);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
hideProgressDiag();
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if(failingUrl.equals("file:///android_asset/html/error.html")){
hideProgressDiag();
Toast.makeText(_context, "Error! Check internet connection, or try again later...", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(_context, failingUrl, Toast.LENGTH_SHORT).show();
view.loadUrl("file:///android_asset/html/error.html");
}
}
}
MyChromeClient:
private class MyChromeClient extends WebChromeClient{
#Override
public void onProgressChanged(WebView view, int progress) {
Pdiag.setProgress(progress * 100);
}
}
When clicking one of the links that points to the javascript function all that happens is that the WebView turns grey, without going through shouldOverrideUrlLoading(). When i hit the back key the app exits, meaning that nothing was placed in the nav history of the WebView. Sometimes nothing happens, but then the shouldOverrideUrlLoading() do run and from a Log.d() i can see that the correct URL for the popup has been given to the WebView.
The thing is, on very rare occasions it shows up fine, but i have no clue how to reproduce it, and wether it actually shows in a popup.
I'm lost... And quite frustrated... Thinking of watching a bad sitcom instead :(
EDIT:
Actually, maybe the URL wasn't all that correct after all... In Firefox the URL ends with "X<<<<" but in my Log.d() output it ends with "X%3C%3C%3C%3C"... I'll investigate if i could change that...
EDIT 2:
Nope, didn't do anything... The URL is identical to the one in Firefox...
First of all, you need to set the following settings on your WebView:
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
Then you need to attach a WebChromeClient that overrides onCreateWindow. Your implementation of this method can create a new web view, and display it inside a dialog:
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(MyActivity.this);
WebSettings webSettings = newWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Other configuration comes here, such as setting the WebViewClient
final Dialog dialog = new Dialog(MyActivity.this);
dialog.setContentView(newWebView);
dialog.show();
newWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onCloseWindow(WebView window) {
dialog.dismiss();
}
});
((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
});
Don't forget to set the new web view to the resultMsg, send it to its target and return true, as mentioned in the API documentation.
Please check with adding this -
getSettings().setSupportMultipleWindows(true);

Categories

Resources