webview.setVisibility(View.VISIBLE);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String URL, Bitmap favicon)
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description,failingUrl);
}
#Override
public void onPageFinished(WebView webview, String url) {
super.onPageFinished(webview, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});webview.loadUrl(“my_url”);
Related
i am trying load a .php file in WebView. it was working properly before API level 28 after i updated to API level 28 its not working its showing white screen. nothing is showing i tried all the options.
Here is the code
I am added the safe browsing false in the manifest file also.
I tried searching in the google nothing is helped me if any one done please help in this case to resolve.
String url="https://xxx/xxx/abc.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleSSLHandshake();
WebView mWebView = (WebView) findViewById(R.id.webView);
// PackageInfo webViewPackageInfo = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// webViewPackageInfo = WebView.getCurrentWebViewPackage();
mWebView.getSettings().setSafeBrowsingEnabled(false);
// Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);
}
WebViewClientImpl webViewClient = new WebViewClientImpl(this);
mWebView.setWebViewClient(webViewClient);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
}
public class WebViewClientImpl extends WebViewClient {
private Activity activity = null;
public WebViewClientImpl(Activity activity) {
this.activity = activity;
}
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
// Log.i(TAG,url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
}
}
I solved my problem. it was the certificates issues, i just added the following code, its working fine now
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
I'm getting the URL and finishing the activity when the URL is stackoverflow.com. How do I pass this URL to Parent Activity?
wvPayment.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "shouldOverrideUrlLoading: "+String.valueOf(request.getUrl()));
if(String.valueOf(request.getUrl()).equals("http://stackoverflow.com/")){
finish();
Toast.makeText(PaymentActivity.this ,"Successful",Toast.LENGTH_LONG).show();
}
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
wvPayment.setVisibility(View.GONE);
ProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
wvPayment.setVisibility(View.VISIBLE);
ProgressBar.setVisibility(View.GONE);
}
});
The following code is the MainActivity of the app. I tried to add a Custom Error page by using:
mywebView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mywebView.loadUrl("file:///android_asset/error.html");
} });
When I use the above code in the main activity either the Error page or the Loading Icon are working at a time. (overriding each other).
I'm not understanding where I'm mistaking. Can anyone please help me fix this problem? Thanks in advance.
public class MainActivity extends AppCompatActivity {
public WebView mywebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("http://google.com/");
mywebView.setWebViewClient(new WebViewClient());
mywebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
mywebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress).setVisibility(View.GONE);
}
});
}
public void onBackPressed() {
if(mywebView.canGoBack()){
mywebView.goBack();
} else {
super.onBackPressed();
}
}}
mywebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
findViewById(R.id.progress).setVisibility(View.VISIBLE);
}
public void onPageFinished(WebView view, String url) {
findViewById(R.id.progress).setVisibility(View.GONE);
}
public void onReceivedError(WebView webview, int i, String s, String s1)
{
webview.loadUrl("file:///android_asset/error.html");
}
});
Finally I found the solution.
In iPhone we have functions like webViewDidStartLoad and webViewDidFinishLoad to check the Start of loading and finish of particular url.
Do we have anythng like this on Android ?
You could try something like this :
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Do something here
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Do something here
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// Do something here
}
});
Yes you can use WebViewClient :
private class CustomWebClient extends WebViewClient{
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
public void onPageFinished(WebView view, String url)
{
}
}
Usage:
webView.setWebViewClient(new CustomWebClient());
Every method name is self explanatory and you can also check this : http://developer.android.com/reference/android/webkit/WebViewClient.html
I'm looking to develop an application to track how long a site takes to load on the default android browser. Does the browser have any method of tracking when the site is fully loaded?
on page started and on page finished are the methods to
get the the start time and end time of loading of the webview
WebView search_webview;
search_webview = (WebView) findViewById(R.id.show_webview);
search_webview.setWebViewClient(new WebViewClient(){
#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);
}
#Override
public void onPageFinished(WebView view, String url) {
_dialog.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
_dialog.dismiss();
Toast.makeText(ShowWebView.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
// TODO: handle exception
}
}
});