Link Does not open in webview? - android

I am developing an android application , in which i have certain Twits with a specific hash tag like #nokia
i have this link https://mobile.twitter.com/search/%23nokia should be open in webview without hiding my Tabs at the bottom.How can i do this?
i have already use the method
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// This line we let me load only pages inside Firstdroid Webpage
if ( url.contains("firstdroid") == true )
/* Load new URL Don't override URL Link */
return false;
// Return true to override url loading (In this case do nothing).
return true;
}
any help

When you return true in this method, you should call:
view.loadUrl(url);

Yes, Olsavage is right. If you want to "override" the URL Loading, you need to return true + call view.loadUrl(url) before you actually return true
Your code should then be:
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// This line we let me load only pages inside Firstdroid Webpage
if ( url.contains("firstdroid") == true )
/* Load new URL Don't override URL Link */
return false;
// Return true to override url loading (In this case do nothing).
view.loadUrl(url);
return true;
}
[edit]:
How about the %23 in your URL ? Is that what you want to achieve? Open the actual #hash in the web browser?

in the webview client, instead of the code.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); // open link in the same web view.
return true;
}
});

Related

How to alter URL loading in a webview?

The basic idea is to alter every url being loaded in a webview (for example adding/removing get parameters).
I have a custom WebViewClient, in which I have the following method :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String modifiedUrl = Util.someMethod(url);
super.shouldOverrideUrlLoading(view, modifiedUrl);
}
Will it work or should I put this logic in another method, for example onPagestarted ?
You rather should do something like :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(conditionForModifyingUrl){
String modifiedUrl = Util.someMethod(url);
view.loadUrl(modifiedUrl);
return true;
}
return false;
}
Calling super.shouldOverrideUrlLoading(view, modifiedUrl) won't work because, as it is its name, this method only checks if the url should be overridden, and does not load the url at all.

How to intercept url loads in WebView (android)?

I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.
My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.
How can I prevent the WebView to load the url?
Here's my code :
WebView banner = ...
banner.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith("app://")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url), getContext(), Main.class);
//startActivity(i);
}
}
}
banner.loadUrl("url_to_the_banner");
Use WebViewClient.shouldOverrideUrlLoading instead.
public boolean shouldOverrideUrlLoading(WebView view, String url){
// handle by yourself
return true;
}
WebViewClient Reference
Updates: Method shouldOverrideUrlLoading(WebView, String) is deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.
But it must return false otherwise, so:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.startsWith(myString){
// handle by yourself
return true;
}
// ...
return false;
}

Android webview calls browser activity

I'm using webview in android
and when I call loadURL it does a call to the browser activity
I need to show the webcontent inside my own activity as I have other stuff to be viewed in it
buttons, text, ,.. etc
Use the WebView.setWebViewClient(WebViewClient) method ( http://developer.android.com/reference/android/webkit/WebView.html) and override the shouldOverrideUrlLoading(WebView, String) method like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ( url.equals( "http://www.example.com" ) ) {
// Do something
return true;
}
// If we couldn't match the URL, the following line
// lets the webview use default behaviour (i.e. open browser)
return super.shouldOverrideUrlLoading(view, url);
}
});

How to make links open within webview or open by default browser depending on domain name?

I have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my application.
I tried to use public boolean shouldOverrideUrlLoading(WebView view, String url) but it does not work properly.
Here is the code that does not work:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
URL urlObj = new URL(url);
if (urlObj.getHost().equals("192.168.1.34")) {
view.loadUrl(url);
return true;
} else {
view.loadUrl(url);
return false;
}
} catch (Exception e) {
}
}
}
In both cases ( return true and return false) the URL is handled by my application.
Once you create and attach a WebViewClient to your WebView, you have overridden the default behavior where Android will allow the ActivityManager to pass the URL to the browser (this only occurs when no client is set on the view), see the docs on the method for more.
Once you have attached a WebViewClient, returning false form shouldOverrideUrlLoading() passes the url to the WebView, while returning true tells the WebView to do nothing...because your application will take care of it. Unfortunately, neither of those paths leads to letting Android pass the URL to the browser. Something like this should solve your issue:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
URL urlObj = new URL(url);
if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
//Allow the WebView in your application to do its thing
return false;
} else {
//Pass it to the system, doesn't match your domain
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
//Tell the WebView you took care of it.
return true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
I know that seems a little counterintuitive as you would expect return false; to completely circumvent the WebView, but this is not the case once you are using a custom WebViewClient.
Hope that helps!
If you can't be bothered to explain what "does not work properly" means, we can't be bothered to give you much specific help.
Use shouldOverrideUrlLoading(). Examine the supplied URL. If it is one you want to keep in the WebView, call loadUrl() on the WebView with the URL and return true. Otherwise, return false and let Android handle it normally.
Add the following to your activity
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("192.168.1.34")) {
view.loadUrl(url);
Log.d("URL => ", url); // load URL in webview
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent); // Pass it to the system, doesn't match your domain
return true;
}

Android WebView, how to handle redirects in app instead of opening a browser

So right now in my app the URL I'm accessing has a redirect, and when this happens the WebView will open a new browser, instead of staying in my app. Is there a way I can change the settings so the View will redirect to the URL like normal, but stay in my app instead of opening a new browser?
Edit:
I want the redirecting URL, I just don't know how to create it, so the only way to get to that URL is through one that will cause a redirect to the one I want.
For example: When you go here: http://www.amazon.com/gp/aw/s/ref=is_box_/k=9780735622777 notice how it will redirect the URL to the actual product. In my app, if I open it in a new browser, it will do that just fine, however if I keep it in my app with a WebView, it will show up as though it's doing a search for k=9780735622777, like this: http://www.amazon.com/gp/aw/s/ref=is_s_?k=k%3D9780735622777&x=0&y=0 . OR, it will open the view in the browser and show what is appropriate. However, I want to keep everything in my app.
Create a WebViewClient, and override the shouldOverrideUrlLoading method.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
According to the official documentation, a click on any link in WebView launches an application that handles URLs, which by default is a browser. You need to override the default behavior like this
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Just adding a default custom WebViewClient will do. This makes the WebView handle any loaded urls itself.
mWebView.setWebViewClient(new WebViewClient());
You will have to set your custom WebviewClient overriding shouldOverrideUrlLoading method for your webview before loading the url.
mWebView.setWebViewClient(new WebViewClient()
{
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
return shouldOverrideUrlLoading(url);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
{
Uri uri = request.getUrl();
return shouldOverrideUrlLoading(uri.toString());
}
private boolean shouldOverrideUrlLoading(final String url)
{
Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
// Here put your code
return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.
}
});
Checkout the example code for handling redirect urls and open PDF without download, in webview.
https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500
Create a class that implements webviewclient and add the following code that allows ovveriding the url string as shown below.
You can see these [example][1]
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On your constructor, create a webview object as shown below.
web = new WebView(this); web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Then add the following code to perform loading of urls inside your app
WebSettings settings=web.getSettings();
settings.setJavaScriptEnabled(true);
web.loadUrl("http://www.facebook.com");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient() {
//
//
}
Please use the below kotlin code
webview.setWebViewClient(object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
return false
}
})
For more info click here
In Kotlin, to navigate within same webView we needed to override the shouldOverrideUrlLoading for webview
If return type is true then navigation will be blocked If return
type is false then navigation will happen
object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
return true
}
}.also { webView.webViewClient = it }
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (url.equals("your url")) {
Intent intent = new Intent(view.getContext(), TransferAllDoneActivity.class);
startActivity(intent);
}
}

Categories

Resources