Not able to open url in webview - android

When I load url it shows a window to select browser. For ex. - If I want to open "http://www.facebook.com/" then it show me window to choose chrome or default browser. I am not able to understand why this is happening.
Actually in below code if i successfully got url from server then I hide a sorry image and show webview. Otherwise I show webview and hide sorry image.
webView = (WebView) findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
ImageView img = (ImageView) findViewById(R.id.image);
img.setVisibility(View.GONE);
webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.loadUrl("http://www.facebook.com/");

Try to add this line
webView.setWebViewClient(new WebViewClient());

Add a WebViewClient like this
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;
}
}
You should override your shouldOverrideUrlLoading() method.
In your UI activity add this line
web.setWebViewClient(new myWebClient());
It will solve your problem. Hope it helps!

Firstly, replace http with https and then in a order to do this, you need to first create a new java class that extends the WebViewClient class and overrides the onPageFinished method like this:
public class CustomWebViewClient extends WebViewClient
{
#Override
public void onPageFinished(WebView view, String url) {
//https://www.facebook.com/dialog/permissions.request
//actually works for me, but I put the URL you say is coming up
//blank in there instead, whatever works for you:
if(url.startsWith("https://www.facebook.com/dialog/oauth")){
String redirectUrl = "http://www.yourdomain.com/MyApp/";
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
Second, just add it to your WebView:
webview.setWebViewClient(new CustomWebViewClient());

Related

WebView Links not opening

I am trying to open a link using Webview in Android. There are some links embedded into WebView. My problem is webview is not opening any link that does not starts with www. For ex, www.google.com is working but maps.google.com is not.I have also tried to override WebViewClient but it didn't work.
One thing I noticed is by putting Toast to see what url is being called in WebViewClient. It showed perfect for www.google.com but returned nothing for other links. I thing WebViewClient is not getting overriden in that case. What could be the reason. Do I have to call any ethod or some property of webview.
Any help will be appreciated.
menuView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
Toast.makeText(getApplicationContext(), "url:--" + url, Toast.LENGTH_LONG).show();
view.loadUrl(url);
return false;
}
});
Properties that I have already set are :
menuView.setVerticalScrollBarEnabled(false);
menuView.setHorizontalScrollBarEnabled(false);
final WebSettings webSettings = menuView.getSettings();
menuView.getSettings().setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
menuView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
menuView.getSettings().setBuiltInZoomControls(true);
// Below required for geolocation
menuView.getSettings().setJavaScriptEnabled(true);
menuView.getSettings().setGeolocationEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// in oncreate
webview.setWebChromeClient(new wecrome());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadsImagesAutomatically(true);
webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview.setWebViewClient(new MyBrowser());
webview.getSettings().setPluginState(PluginState.ON);
webview.loadUrl("http://www.example.net/locations/");
//inner class
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object() {
#JavascriptInterface
public void performClick() throws Exception {
Log.d("LOGIN::", "Clicked");
Toast.makeText(googleplus.this, "Login clicked",
Toast.LENGTH_LONG).show();
}
}, "login");
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
System.out.println("started");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
pd.dismiss();
System.out.println("ends");
super.onPageFinished(view, url);
}
}

Android - Open URL in a WebView [duplicate]

I created an Activity that has a title and a web view in a LinearLayout. In the onResume() method it calls webView.loadUrl(url). The problem is that the activity first shows the title with the rest of the screen blank, then the device browser is launched with the page for the URL. What I want to see is the page being shown in the WebView below the title. What could be the problem?
Edit:
Ok, did some further search and found this one:
Clicking URLs opens default browser
It points to the WebView tutorial here.
Just implement the web client and set it.
Answering my question based on the suggestions from Maudicus and Hit.
Check the WebView tutorial here.
Just implement the web client and set it before loadUrl. The simplest way is:
myWebView.setWebViewClient(new WebViewClient());
For more advanced processing for the web content, consider the ChromeClient.
Use this:
lWebView.setWebViewClient(new WebViewClient());
use like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dedline);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
}
Make your Activity like this.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.wvwMain);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// Set WebView client
browser.setWebChromeClient(new WebChromeClient());
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
// Load the webpage
browser.loadUrl("http://google.com/");
}
}
I was facing the same problem and I found the solution
Android's official Documentation about WebView
Here is my onCreateView() method and here i used two methods to open the urls
Method 1 is opening url in Browser and
Method 2 is opening url in your desired WebView.
And I am using Method 2 for my Application and this is my code:
public class MainActivity extends Activity {
private WebView myWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
/* Method : 1
This following line is working fine BUT when we click the menu item then it opens the URL in BROWSER not in WebView */
//((WebView) rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);
// Method : 2
myWebView = (WebView) rootView.findViewById(R.id.detail_area); // get your WebView form your xml file
myWebView.setWebViewClient(new WebViewClient()); // set the WebViewClient
myWebView.loadUrl(mItem.url); // Load your desired url
}
return rootView;
} }
Try this code...
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (final WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(view.getContext());
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
If you see an empty page, enable JavaScript.
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl(url);
Simply Answer you can use like this
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
}
}
If you're using webChromeClient I'll suggest you to use webChromeClient and webViewClient together. because webChromeClient does not provides shouldOverrideUrlLoading. It is okay to use both.
webview.webViewClient = WebViewClient()
webview.webChromeClient = Callback()
private inner class Callback : WebChromeClient() {
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (newProgress == 0) {
progressBar.visibility = View.VISIBLE
} else if (newProgress == 100) {
progressBar.visibility = View.GONE
}
}
}
I just found out that it depends on the formatting of the URL:
https://example.com/example gets opened in the browser
https://example.com/example/ (with / at the end) gets opened in the webView
My code just uses
webview.loadUrl(url)
no need to set
webView.setWebViewClient(new WebViewClient())
at least in my case.
Maybe that's useful for some of you.
My problem ended up being that I needed to do a clearHistory before I could switch between sites without opening an external browser.
#Override
public void onPageFinished(WebView view, String url) {
webView_.clearHistory();
super.onPageFinished(webView_, url);
}

Android - Detecting WebView URL not working

I am developing an app, in that I want to detect each time when a new url gets loaded in webview and then display that url in edittext.
I tried many solution and googled. But, not found significant solution which solves my purpose..
My Code is:
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url_new = wv.getUrl();
Log.v("","Webview URL: "+url_new);
edittext.setText("http://"+url_new);
return true;
}
});
Not even printing the Log Statement :(
Please Help..!!
Thanks in advance..!! :)
First off the code you're posting is returning true from shouldOverrideUrlLoading which means you've just prevented the webview from navigating to any location.
Second off, shouldOverrideUrlLoading will not get called for any urls you pass to the webview.loadUrl API. As suggested in one of the other answers, onPageStarted is the simplest API to use for this.
You can use the code below to play around with the APIs:
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.v("","Webview shouldOverrideUrlLoading URL: " + url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
Log.v("","Webview onPageStarted URL: " + url);
}
});
This is the code I use in my applications.
WebView view = (WebView) findViewById(R.id.myWebView);
EditText edit = (EditText) findViewById(R.id.myEditText);
view.setWebViewClient(new WebViewClient(){
public void onPageStarted (WebView view, String url, Bitmap favicon){
edit.setText(url);
}
});
Set WebViewClient to your webview and geturl on when page finshed laoding
class MyWebView extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
editText.setText(view.getUrl());
}
}
set this WebViewClient to your webview ..
webView.setWebViewClient(new MyWebView());
You can override onLoadResource() method of WebViewClient :
wv.setWebViewClient(new WebViewClient() {
public void onLoadResource(WebView view, String url) {
edittext.setText("http://"+url_new);
}
}

webview links not working

l am using webview in my xml, loading html file from asset directory. But clicking on links sometimes launching browser on first click and sometimes not responding even after 5 clicks.
Any help is appreciated.
Thanks
For, this you've to use WebViewClient() to your WebView
WebView web = (WebView)findViewById(R.id.webView1);
.....
..... // Your stuff
.....
web.setWebViewClient(new HelloWebViewClient());
public class HelloWebViewClient extends WebViewClient
{
public HelloWebViewClient()
{
// do nothing
}
#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);
}
}
just add these lines
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Android webview launches browser when calling loadurl

I created an Activity that has a title and a web view in a LinearLayout. In the onResume() method it calls webView.loadUrl(url). The problem is that the activity first shows the title with the rest of the screen blank, then the device browser is launched with the page for the URL. What I want to see is the page being shown in the WebView below the title. What could be the problem?
Edit:
Ok, did some further search and found this one:
Clicking URLs opens default browser
It points to the WebView tutorial here.
Just implement the web client and set it.
Answering my question based on the suggestions from Maudicus and Hit.
Check the WebView tutorial here.
Just implement the web client and set it before loadUrl. The simplest way is:
myWebView.setWebViewClient(new WebViewClient());
For more advanced processing for the web content, consider the ChromeClient.
Use this:
lWebView.setWebViewClient(new WebViewClient());
use like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dedline);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
}
Make your Activity like this.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// find the WebView by name in the main.xml of step 2
browser=(WebView)findViewById(R.id.wvwMain);
// Enable javascript
browser.getSettings().setJavaScriptEnabled(true);
// Set WebView client
browser.setWebChromeClient(new WebChromeClient());
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
// Load the webpage
browser.loadUrl("http://google.com/");
}
}
I was facing the same problem and I found the solution
Android's official Documentation about WebView
Here is my onCreateView() method and here i used two methods to open the urls
Method 1 is opening url in Browser and
Method 2 is opening url in your desired WebView.
And I am using Method 2 for my Application and this is my code:
public class MainActivity extends Activity {
private WebView myWebView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
/* Method : 1
This following line is working fine BUT when we click the menu item then it opens the URL in BROWSER not in WebView */
//((WebView) rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);
// Method : 2
myWebView = (WebView) rootView.findViewById(R.id.detail_area); // get your WebView form your xml file
myWebView.setWebViewClient(new WebViewClient()); // set the WebViewClient
myWebView.loadUrl(mItem.url); // Load your desired url
}
return rootView;
} }
Try this code...
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
//Show loader on url load
public void onLoadResource (final WebView view, String url) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(view.getContext());
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}catch(Exception exception){
exception.printStackTrace();
}
}
});
// Javascript inabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
If you see an empty page, enable JavaScript.
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl(url);
Simply Answer you can use like this
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
}
}
If you're using webChromeClient I'll suggest you to use webChromeClient and webViewClient together. because webChromeClient does not provides shouldOverrideUrlLoading. It is okay to use both.
webview.webViewClient = WebViewClient()
webview.webChromeClient = Callback()
private inner class Callback : WebChromeClient() {
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (newProgress == 0) {
progressBar.visibility = View.VISIBLE
} else if (newProgress == 100) {
progressBar.visibility = View.GONE
}
}
}
I just found out that it depends on the formatting of the URL:
https://example.com/example gets opened in the browser
https://example.com/example/ (with / at the end) gets opened in the webView
My code just uses
webview.loadUrl(url)
no need to set
webView.setWebViewClient(new WebViewClient())
at least in my case.
Maybe that's useful for some of you.
My problem ended up being that I needed to do a clearHistory before I could switch between sites without opening an external browser.
#Override
public void onPageFinished(WebView view, String url) {
webView_.clearHistory();
super.onPageFinished(webView_, url);
}

Categories

Resources