Webview navigation returning wrong url - android

I have a webview that looks like this
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("https://myurl.com/");
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(LoginActivityWebView.this, url, Toast.LENGTH_SHORT).show();
return false;
}
}
When i change my URL to google everything works great, i can navigate to different sites and everything works.
When I put in my URl it is supposed to link to myurl2, but when I click it the toast prints myurl instead and the page juts reloads (not helpful!).
Any idea as to how this could happen and where shouldOverrideUrlLoading gets its url from?
If i load myurl in a browser and the user clicks a button then it works correctly.
If i load myurl in a webview, but open a browser on links pressed, then when the link is pressed it opens myurl on in a broswer and if you click the button again it correctly opens myurl2 (super weird).
Any ideas?

If the URL is opening in Chrome properly, you should consider adding, webView.getSettings().setJavaScriptEnabled(true) to your code and try again.

You can get the loaded URL from
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});

Related

In my android webview, URL shown but I want to remove this and it always open in browser I don't want that

When I create android webview then after URL shown in the AVD. When click on any button then then it open in browser but I don't want that type of problem.
I want simple type of app ex- "ashley madison". Please help me.
...
WebView webview = (WebView) findViewById(R.id.webView);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl(URL);
...
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}

How to enable ZoomControls in android WebView when open specific URL?

I'm using WebView in android; I want to show ZoomControls not for all pages but only for my specific webpage. What should I do? Please Help!
Assuming that it's an activity and you are receiving the url from the intent
String url = getIntent().getStringExtra("url");
WebView webview = (WebView) findViewById(R.id.webview_id_here)
webView.getSettings().setBuiltInZoomControls(url.equals("your_url_here"));
If you want this to happen every time a url changes, use a custom webview client.
webView.setWebViewClient(new CustomWebViewClient());
wherever the webView is initialized. CustomWebViewClient :
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.getSettings().setBuiltInZoomControls(url.equals("your_url_here"));
return false;
}
}

Android - Bug in Webview? Some URL's prompt webview to start external browser

I have a "NewsActivity" with a webview. When I start it from my main activity, links are opened in the webview correctly but, for some reason, some url's cause the webview to open but then to immediately launch the external browser. Checking the debug console I have not found any exception or other message thrown by webview as not being able to handle the url.
Please note that I am not talking about a link clicked after the webview has loaded the url/page.
I have also tried to activate javascript in the webview but to no avail.
Also, this happens for just some urls from the same domain (specifically a news website; also, I have no block url or override in place).
Here is one of the urls that fail to open in the webview: url_not_loaded
Here is the code that calls the "NewsActivity"
Intent intent = new Intent(this, NewsActivity.class);
intent.putExtra(MainActivity.EXTRA_MESSAGE, url);
startActivity(intent);
And here is the code in "NewsActivity"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
mWebview = (WebView) findViewById(R.id.newsv);
Log.d(MainActivity.LOG_TAG, "URL: " + url);
mWebview.loadUrl(url);
}
If someone has a clue as to what may be happening or can suggest any idea, I'll be grateful.
Thanks!
Your URL's might have redirects. I encountered a similar problem.
Add this to your activity with the webview.
webView.setWebViewClient(new Callback());
Add this outside of onCreate.
private class Callback extends WebViewClient{ //Helps to open in webview instead of browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
It's not a bug.
You need to use WebViewClient.
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
System.out.println("hello");
return false;
}
});
//Toast.makeText(this, "", Toast.LENGTH_SHORT);
mWebView.loadUrl(url);
You can set a newWebViewClient like this:
webView.setWebViewClient(new WebViewClient());
But to be more precisely you need to override the shouldOverrideUrlLoading method like this:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}});

Blank empty page in android webview after facebook login using javascript

i am developing a new android application,and using a webview to view my website in one the website pages there is a link for login with facebook
after logging directly, it returns a blank and embty webview
i have seen couple of topics here regarding the same issue but it is not exactly like mine
please note that if i am using normal desktop browser the issue is not there
I assume that there is a property of the webview should be adjusted , below my webview code
webView1=(WebView)findViewById(R.id.webView1);
webView1.setWebViewClient(new MyWebViewClient());
WebView webView1 = (WebView) findViewById(R.id.webView1);
webView1.setWebChromeClient(new WebChromeClient());
webView1.getSettings().setAppCacheEnabled(true);
webView1.getSettings().setDatabaseEnabled(true);
webView1.getSettings().setDomStorageEnabled(true);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView1.getSettings().setGeolocationEnabled(true);
webView1.loadUrl("http://www.myactivepoint.com/mobile/maplp.asp");
i really approciate your support
I had a similar experience with you. I solved the problem by the following methods.
public class CustomWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
if(url.startsWith("https://m.facebook.com/dialog/oauth")){
String redirectUrl = "http://www.mydomain.com/myReturnUrl";
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}}
and just add it to your WebView
webview.setWebViewClient(new CustomWebViewClient());
This is my solution. After successfull login you have to redirect the page. You can get the address from refsrc parameter in the Facebook URL, but sometimes (e.g. next try to log in) refsrc is missing so I use domain parameter. You can override the URL address if you need. See the code:
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(final WebView view, final String url)
{
if(getActivity()!=null)
{
if(url.contains("facebook.com") && url.contains("dialog/oauth") &&
(url.contains("&refsrc") || url.contains("&domain")))
{
Uri uri = Uri.parse(url);
String callbackUrl = uri.getQueryParameter("refsrc");
if(callbackUrl==null)
callbackUrl = "http://" + uri.getQueryParameter("domain");
view.loadUrl(callbackUrl); // callback URL
}
}
}
}

How to use a webview in a fixed size?

I need a page which show web contents in a fixed width and height.
I put a webview inside a xml file with a height of 200px,but while loading that page,it directly redirect to default browser view.
While press the back button in the phone,it redirect to the original page,then the webview shows nothing.
in my xml file
<WebView android:layout_width="fill_parent" android:layout_height="200px" android:id="#+id/wvbrowser" />
in code
WebView wvbrowser;
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.loadUrl("http://www.orkut.com");
So tell me where I am Wrong? and how to show WebView in FIXED size?
Try this...
create a separate class.
public class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Then in your activity use this.
WebView wvbrowser;
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.setWebViewClient(new HelloWebViewClient());
wvbrowser.getSettings().setJavaScriptEnabled(true);
wvbrowser.loadUrl("http://www.orkut.com");
When the user clicks a link from a web page in your WebView,the default Android Browser handles the Intent to view a web page, because your Activity isn't technically enabled to do so. You need to override the WebViewClient class and enable your webview Activity to handle its own URL requests.
setWebViewClient(new HelloWebViewClient());
The above line creates a WebViewClient that will load any URL selected from this WebView into the same WebView.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
The shouldOverrideUrlLoading(WebView, String) method is passed the current WebView and the URL requested, so all it needs to do is load the URL in the given view. Returning true says that the method has handled the URL and the event should not propagate.

Categories

Resources