I have a webview in my app. I can see the title of the website on the title bar (I have custom title bar). However, i dont see anything in the view - the website is not viewable :(.. any suggestions ? Here's the code:
public class WebViewer extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
webView = (WebView) findViewById(R.id.webview);
String url = "http://www.google.com";
final TextView title=(TextView) findViewById(R.id.title_text_view_success3);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
title.setText("Loading...");
WebViewer.this.setProgress(progress * 100);
if(progress == 100)
title.setText(webView.getTitle());
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
This isn't needed. shouldOverrideUrlLoading is called before the url is loaded to give you a chance to handle loading yourself. What you're doing is loading the url over and over.
http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView,%20java.lang.String%29
Related
I am using android in that I am calling a link via webview. Now, the problem is that whenever I click any button or call any page of that link at that time after some second progress bar coming i.e. progress bar performance is delay while load anything.
From this problem user can't know that button/link has been clicked or not, without getting any progress bar sign, and hence from that user clicked many times that button/link.
Here is my android code of MainActivity.java,
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar)findViewById(R.id.progressBar2);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://florro-001-site2.etempurl.com/");
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I searched many things but related topics I didn't get.
use bar.setVisibility(View.VISIBLE); in the shouldOverrideUrlLoading not in onPageStarted try to use like that
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
view.loadUrl(url);
bar.setVisibility(View.VISIBLE);
return true;
}
}
I am doing webview in android in which I am calling a website link in that website I am calling web.whatsapp.com link. Now this link is working good in desktop as well as mobile view while the same link if I open in webview then showing error i.e. web page is not available for more clear let see the error image
Here is the code of webview android
public class MainActivity extends AppCompatActivity {
WebView webView;
ProgressBar bar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView2);
bar =(ProgressBar) findViewById(R.id.progressBar2);
webView.setWebViewClient(new myWebClient());
webView.loadUrl("http://.../");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebClient());
}
public class myWebClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(webView.VISIBLE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
bar.setVisibility(View.VISIBLE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
bar.setVisibility(View.GONE);
return super.shouldOverrideUrlLoading(view, url);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Try to override shouldOverrideUrlLoading in your web client and use code given below .
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean overrideUrlLoading = false;
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
overrideUrlLoading = true;
} else {
view.loadUrl(url);
}
return overrideUrlLoading;
}
I want to open glauniversity.in in my webview. I have implemented following code and searched a lot, but everything seems futile. Any Help will be Appreciated. The code goes as follows:
public class WebViews extends Activity {
String link;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
link="glauniversity.in";
webview = findViewById(R.id.wv);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed();
}
});
webview.setWebChromeClient(new WebChromeClient() { #Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } });
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(link);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webview.canGoBack()) {
webview.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
glauniversity.in, This is not a valid url
replace this to
http://glauniversity.in/
Bonus::
Check valid url by
Patterns.WEB_URL.matcher(url).matches()
or
URLUtil.isValidUrl(url)
Android webview display white screen instead of loading the website on Android 4.1.2 .The website perfectly opens when I open it from web browser, I am using following code
public class MainActivity extends AppCompatActivity {
WebView web;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("www.Example.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;
}
}
// 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);
}
}
Remember to set the protocol "http:"
web.loadUrl("http://www.Example.com");
and very important, be sure that url has a value at this point:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
i want to disable phone browser and i want to use a webview .
but this code didn't work for me . please guide me :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Wop=(WebView)findViewById(R.id.webi);
String mturl="http://www.google.com";
Wop.getSettings().setJavaScriptEnabled(true);
Wop.loadUrl(mturl);
}
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.loadUrl("http://www.stackoverflow.com");
To enable the previous web-page to be loaded,
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
} }
Definiton of the HelloWebViewClient class :
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
//what you want to do when the page finished loading, eg. give some message, show progress bar, etc
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
//what you want to do when the page starts loading, eg. give some message
}
}
WebView w = new WebView(this);
//add this to your code
w.setWebViewClient(new Callback());
w.loadUrl("http://www.facebook.com");
//and this class also
private class Callback extends WebViewClient{ //HERE IS THE MAIN CHANGE.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}