I'm developing an app in which i have to display an ad,have set it's position and ad is displying but when we click on ad image this open in same webview.But i wanna when we click another webbview should open for display.Can any please help me out.Thanks
Code:
WebView ad = (WebView) findViewById(R.id.ad);
ad.loadUrl(url);
ad.setWebViewClient(new 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;
}
}
Try to switch second and third line, I think the problem is you are loading url prior to setting your webviewclient:
WebView ad = (WebView) findViewById(R.id.ad);
ad.setWebViewClient(new MyWebViewClient());
ad.loadUrl(url);
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
getApplicationContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
Finally done this using.......following code
private class MyClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
I recognize that, when you comment out your ad.setWebViewClient(new MyWebViewClient());, the banner ad in the webview does start a new view.
I use only wrap the img tag with an anchor tag, that totally works in my development with Android 4.4
Related
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);
}
}
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;
}
}
I created an android app with webview.
Within the web page I have amazon banners.
If a user clicks on that banner I would like it to be open in the native android browser and not in webview.
I've tried several methods after searching and trying with no success.
When the banner is clicked - the app still shows the amazon site in webview mode.
Can anyone help me fix this issue ?
Your help is greatly appreciated...
Below is the code I use :
final WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
view.loadUrl(url);
loadAd();
return true;
}
});
webView.loadUrl("http://starter_url");
loadAd();
The function shouldOverrideUrlLoading() should:
Returns True if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
So you have to change your code to something like:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
if(url.contains("amazon-adsystem.com")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
}
return false;
}
});
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;
}});
I am using a webview of height 60dp and i am passing a local html file to it, by default when i click a link in the webview it has to open the browser . But strangely its opens the links with in the webview, i also tried the webview client and trying to pass the response url to default browser through intent, but in vain..
my code snippet:
WebViewClient yourWebClient = new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.out.println("Inside WebViewClient the URL is....."+url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
}
};
WebView ad = (WebView) findViewById(R.id.webview1);
ad.getSettings().setJavaScriptEnabled(true);
ad.loadUrl(feed.getItem(position).getLink());
ad.getSettings().setLoadWithOverviewMode(true);
ad.getSettings().setUseWideViewPort(true);
ad.setInitialScale(100);
ad.setWebViewClient(yourWebClient);
ad.loadUrl("file:///android_asset/advertisement.htm");
Stop the current webview loading. As ad is the object of WebView, Try doing it this way:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.out.println("Inside WebViewClient the URL is....."+url);
ad.stopLoading();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
Also add this while adding settings for your WebView:
ad.getSettings().setSupportMultipleWindows(false);
Edit:
Try associating a chrome client with your webview and then check:
ad.setWebChromeClient(new MyWebChromeClient());
Hope this works for you.