If i return true on shouldOverrideUrlLoading, web view cannot handle onPageFinish when load new url.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.abc.com")) {
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
Disable progress on page finish.
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
try this code
webview.setWebViewClient(new myWebClient());
webview.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progressInt)
{
if (progressInt < 80 && progress.getVisibility() == ProgressBar.GONE)
{
progress.setVisibility(ProgressBar.VISIBLE);
}
if (progressInt >= 80)
{
progress.setVisibility(ProgressBar.GONE);
}
}
});
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
progress.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progress.setVisibility(View.GONE);
}
}
Related
Few urls are not loading in the webview. Please check the code. If i load google it loads properly. Any Solution? URL LOADS PROPERLY IN THE BROWSER.
Here this url (and few others also) is not loading wv.loadUrl("http://resident.uidai.net.in/get-enroled");
Instead if google is loaded it loads properly. How can i forcefully load this url?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
wv = (WebView) rootView.findViewById(R.id.wvHome);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
wv.setScrollbarFadingEnabled(true);
wv.getSettings().setLoadsImagesAutomatically(true);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient());
try {
wv.loadUrl("http://resident.uidai.net.in/get-enroled");
//wv.loadUrl("http://eaadhaar.uidai.gov.in");
}
catch (Exception e)
{
wv.loadUrl("http://google.com");
}
// Inflate the layout for this fragment
return rootView;
}
private class HelloWebViewClient 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 webView, String url)
{
webView.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
1st. change http to https
2nd. Change wv.setWebViewClient(new HelloWebViewClient());
3rd. override the following
private class HelloWebViewClient 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 webView, String url) {
webView.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(view.GONE);
}
#SuppressLint("NewApi")
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
}
I am making a web view and trying to show loading message using progress Dialog.progress dialog is showing but not dismissed after all the content of URL is loaded.Please Help me.
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
prDialog = ProgressDialog.show(Web.this, null, "loading, please wait...");
super.onPageStarted(mWebview, url, favicon);
Toast.makeText(getApplicationContext(), "started!"+1, Toast.LENGTH_SHORT).show();
num++;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebview, url);
prDialog.dismiss();
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview);
package com.example.webviewtag;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
WebClientClass webViewClient = new WebClientClass();
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient=new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
setContentView(webView);
}
public class WebClientClass extends WebViewClient {
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pd = new ProgressDialog(WebViewDemo.this);
pd.setTitle("Please wait");
pd.setMessage("Page is loading..");
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pd.dismiss();
}
}
public class WebChromeClass extends WebChromeClient{
}
}
Try this code,
private class MYWEBCLIENT extends WebViewClient {
private ProgressDialog prDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(activity, "", "Please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (prDialog != null && prDialog.isShowing())
prDialog.dismiss();
}
}
Load webview code,
webViewInfo.getSettings().setJavaScriptEnabled(true);
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webViewInfo.setWebViewClient(new MYWEBCLIENT());
webViewInfo.loadData("YOUR_URL_OR_HTML_FILE", "text/html", "UTF-8");
You can try this java code:
Just need to replace my entities with yours. Such as class name or other variables or stuff.
public class WebViewDemo extends Activity
{
WebView mWebview;
ProgressDialog prDialog;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
mWebview = (WebView) findViewById(R.id.your_id);
mWebview.getSettings().setJavaScriptEnabled(true);
CustomWebClient webClient =new CustomWebClient(WebViewDemo.this);
mWebview.setWebViewClient(webClient);
mWebview .loadUrl("http://www.google.com");
}
// This is your custom webviewclient
public class CustomWebClient extends WebViewClient
{
public Context context;
public CustomWebClient(Context context)
{
// TODO Auto-generated constructor stub
this.context = context;
prDialog = new ProgressDialog(context);
prDialog.setMessage("loading please wait...");
prDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
prDialog.show();
}
#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);
try
{
if ((prDialog != null) && prDialog.isShowing())
{
prDialog.dismiss();
}
} catch (final IllegalArgumentException ae) {
} catch (final Exception excep) {
} finally {
prDialog = null;
}
}
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm)
{
// TODO Auto-generated method stub
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
You can run my java class to test, is it working or not at your end.
Hope this helps you.
i have more than 100 html files and i want each file to open on row click in listview and each html file should open in the webview , i tried this code but this is not working, only the webview opens on row click , the html file is not showing up.
package com.Example.apk;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebViewClient;
public class WebView extends Activity {
public class WebViewActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
web = (WebView) findViewById(R.id.webview1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/work.html");
}
else if(pos==1)
{
web.loadUrl("file:///android_asset/work1.html");
}
else if(pos==2)
{
web.loadUrl("file:///android_asset/work2.html");
}
else if(pos==3)
{
web.loadUrl("file:///android_asset/work3.html");
}
// similarly for 4 and 5 and so on.
}
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 onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
}
}
Pass the position to next Activity
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(MainActivtiy.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
});
Then in WebViewActivity ie activity_webview)
public class WebViewActivity extends Activity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
web = (WebView) findViewById(R.id.webview1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
web.loadUrl("file:///android_asset/work.html");
}
else if(pos==1)
{
web.loadUrl("file:///android_asset/work1.html");
}
else if(pos==2)
{
web.loadUrl("file:///android_asset/work2.html");
}
else if(pos==3)
{
web.loadUrl("file:///android_asset/work3.html");
}
...// similarly for 4 and 5 and so on.
}
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 onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
}
xml for WebViewActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Make an entry for WebViewActivity in Manifest file.
For your purpose use this
WebView wv = (WebView)rootView.findViewById(R.id.go_web_view);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, description, "text/html", "utf-8", null);
Here "description" is name of string containing data with or without html tags
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String url = m_ArrayList.get(arg2).your_url;
Intent myIntent = new Intent(MainActivtiy.this,AppWebView.class);
myIntent.putExtra("key",url);
startActivity(myIntent);
}
});
If you need to render url then try
public class AppWebView extends Activity{
WebView webView;
ProgressBar pBar;
#SuppressLint("SetJavaScriptEnabled")
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
pBar = (ProgressBar)findViewById(R.id.progressBar1);
String newUrl;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
newUrl = null;
} else {
newUrl = extras.getString("url");
}
} else {
newUrl = (String) savedInstanceState
.getSerializable("myJsonStringS");
}
Log.d("jitendra", newUrl);
//SharedPreferences sp = getSharedPreferences("booking_detail", 0);
//String jsonString = sp.getString("jsonString", "");
webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl(newUrl);
}
public void moveToThanksPage()
{
Intent intent = new Intent(this,ThankYou.class);
startActivity(intent);
}
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
Log.d("sagarWeb", url);
if (url.startsWith("mailto:")) {
String[] blah_email = url.split(":");
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
// emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "what_ever_you_want_the_subject_to)");
Log.d("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + "what_ever_you_want_the_subject_to_be");
startActivity(emailIntent);
}
else if (url.startsWith("tel:")) {
Log.d("Web", "tell");
String uri = url;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
else if (url.endsWith("error.jsp")) {
Log.d("Web", "Error");
}
/*else if (url.contains("thankyou/app")) {
//===================== USE UNDERMENTIONED COMMENT ON FOR SELF THANKS PAGE ==================//
//moveToThanksPage();
}*/
else
{
view.loadUrl(url);
pBar.setVisibility(View.VISIBLE);
}
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
pBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
//progressBar.setVisibility(View.GONE);
}
}
}
I am trying to open this link which is redirected to another URL, in WebView . But it's not showing the page.
Code:
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
web.loadUrl(url);
return true;
}
});
Please help me.
Use this :
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.technotalkative.com");
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);
progressBar.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);
}
Tried this.
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
web.loadUrl(url);
return true;
}
});`
web.loadUrl(url);
Try this...
You need to use "view" (WebView object) which is parameter of that method.
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Use this is working for me...
I have a webView where url is: https://uesr:user#test.move.com:443/test-mobile and when I want to load this page it is unaviable, where I delete authentication from page and delete user:user# page work. How add authentication to webview to work with that url?
this is my webView:
formWebView.getSettings().setJavaScriptEnabled(true);
formWebView.loadUrl(url);
formWebView.getSettings().setBuiltInZoomControls(true);
I resolve my problem by:
formWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler,
String host,
String realm){
handler.proceed(loggedUser.getLogin(), loggedUser.getPass());
}
public void onReceivedSslError(WebView view,
SslErrorHandler handler,
SslError error) {
handler.proceed() ;
}
});
Write below code in your onCreate method.
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient
{
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("UserName", "Password");
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (dialog != null) {
if (!dialog.isShowing()) {
dialog.show();
}
}
}
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
public void onScaleChanged(WebView view, float oldScale, float newScale) {
// TODO Auto-generated method stub
super.onScaleChanged(view, oldScale, newScale);
}
public void onTooManyRedirects(WebView view, Message cancelMsg,
Message continueMsg) {
// TODO Auto-generated method stub
super.onTooManyRedirects(view, cancelMsg, continueMsg);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
public void doUpdateVisitedHistory(WebView view, String url,
boolean isReload) {
// TODO Auto-generated method stub
super.doUpdateVisitedHistory(view, url, isReload);
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
public void onPageFinished(WebView view, String url) {
if (dialog != null) {
dialog.dismiss();
}
}
}