Progress Till Web View Loads Web Page - android

In my application web view takes time to load few webpages.. Is it possible to show progress till web view loads web page. I am using below code to load web page
WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new MyBrowser());
String url = "http://somesites.com";
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl(url);

For showing progress bar use this method in oncreateview in Activity :
private WebView web;
private KProgressHUD pDialog;
private void setPages() {
web = (WebView) findViewById(R.id.web);
// you can use progressbar or progressdialog or any other
// progressview here
pDialog = KProgressHUD.create(activity)
.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
.setDimAmount(0.5f);
pDialog.setCancellable(true);
pDialog.show();
web.getSettings().setSupportZoom(true);
web.getSettings().setBuiltInZoomControls(true);
web.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pDialog.dismiss();
}
});
web.loadUrl(YOUR_URL_HERE);
}

Related

Webview: how to open link in html loaded via "loadDataWithBaseURL"

I'm opening a WebView with static html in my Android app. The html contains a link as well which currently gets opened in the system browser, i.e. the app is put in the background. How can I achieve opening that link in the WebView itself? I tried it with
webView.setWebViewClient(new WebViewclient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// gets never called
}
}
but that method is never called.
Try this it works for you.
private void loadWebView() {
webView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Loading...", true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://yourWeb.com/");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
try {
pd.dismiss();
} catch (Exception e) {
}
}
});
}

Android webview setWebViewClient() show nothing

guys! I have problem to load html page with android-webview. I need to load the url with my webview but not with the mobile system broswer or other broswer, so I have to apply the method setWebViewClient() to my webview but not WebChromeClient(). However, there's load nothing but blank page when applied the setWebViewClient(), and works fine with the later method. I don't know where's problem, here is the code:
.xml :
<WebView android:id="#+id/webview"
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
.java :
WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= 19) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
private String loadUrl = "http://www.baidu.com";
webView.loadUrl(loadUrl);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
view.loadUrl(url);
super.onLoadResource(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.loadUrl(url);
super.onPageStarted(view, url, favicon);
}
});
/*
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v(Log_Tag, String.valueOf(newProgress));
}
});
*/
Just put this code in your activity
private String loadUrl = "https://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.webView);
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
WebViewClient mWebViewClient = new WebViewClient();
mWebView.setWebViewClient(mWebViewClient);
mWebView.loadUrl(loadUrl);
}
You shouldn't override methods in WebViewClient class if you don't want to change their behavior or add some functionality. And don't forget to add permission Internet to your manifest file. WebView will not work without it.
Thanks #Mike M. again. The method shouldOverrideUrlLoading() should return false if you want to loading the url with your webView but not with the mobile system default browser or other Third-Party browsers. And, if you want to deal some javascript actions of the webpage with your webView, you're suggested to apply the WebChromClient to your webView.
Here is the good example:
WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load the page with cache
if (Build.VERSION.SDK_INT >= 19) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return true load with system-default-browser or other browsers, false with your webView
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v(Log_Tag, String.valueOf(newProgress));
//put your code here if your want to show the progress with progressbar
}
});
private String loadUrl = "http://www.baidu.com";
webView.loadUrl(loadUrl);
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.w3schools.com/");
emphasized text

Android WebView Loads all the urls but not only mine

I am trying to load my site in android web view, whenever I set url to google.com or any other it loads without any problem but when I try to load my site darpankulkarni.in then it just shows blank screen.
WebViewActivity:
public class WebViewActivity extends Activity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webView = (WebView) findViewById(R.id.webView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.darpankulkarni.in");
//webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new MyWebViewClient());
}
}
MyWebViewClient:
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//webProg.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
fixed it by adding code below settings to the web view inside of onCreate
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
websettings.setAllowFileAccess(true);
websettings.setAllowContentAccess(true);
websettings.setAllowFileAccessFromFileURLs(true);
websettings.setAllowUniversalAccessFromFileURLs(true);
}
Apply the basic settings to your web view for loading the web view in view port
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
So Here is the complete working code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView=(WebView)findViewById(R.id.webView1);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAllowContentAccess(true);
mWebView.getSettings().setAllowFileAccessFromFileURLs(true);
mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
progressDialog.setMessage("Loading the Web View");
progressDialog.show();
}
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.darpankulkarni.in");
// mWebView.loadUrl("http://www.google.com");
// mWebView.setWebViewClient(new MyWebViewClient());
}
Don't forget to give the internet permission in your manifest
<uses-permission android:name="android.permission.INTERNET" />
I created a progress dialog for loading the web view as it taking some time to load.
Hope this vll help !!

Android WebView failed to load a particular URL

I am working on a android project. In that project i have to load a URL in WebView.
But I am unable to Load the particular URL (Any other URL is loading perfectly). I have also added INTERNET permission. Though the URL open from Android browser .
public class MainActivity extends Activity {
private WebView webView;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webview1);
progDailog = ProgressDialog.show(this, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
// webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
webView.loadUrl("https://XXXXX.com/");
}
}
This site might contain insecure content.Check your proxy settings for the WIFI network connection and make sure they are correct.

android webview not displaying video using WebViewClient

i want to play a video from youtube on webview.. it displays the video,But i want to play it on the same page i mean i've to use WebViewClient.. but using that it doesn't play the video.. (on pressing play button it doesn't play the video) what should i do? my code is
setContentView(R.layout.main);
wvSpecials = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wvSpecials.getSettings();
webSettings.setJavaScriptEnabled(true);
wvSpecials.loadUrl("http://here.com/is link/");
wvSpecials.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(
specialsActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.setMessage("Please wait...");
progressDialog.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
}
You may use the below listed code:
public class YouTube extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView;
myWebView = (WebView) findViewById( R.id.web);
myWebView.setWebViewClient(new MyWebViewClient());
String pre="<iframe class=youtube-player type=text/html width=";
String height=" height=";
String suffix=" src=http://www.youtube.com/embed/**xxxxxxxxxxx**?autoplay=1 frameborder=0>"; // replace xxxxxxxxxxx with the specific embed id of your video
String playVideo=pre+260+height+150+suffix;
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadData(playVideo,"text/html","UTF-8");
}
// override default behaviour of the browser
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
} }

Categories

Resources