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

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;
}
}

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 show web page in android in android app only

I want to show any webpage in my app only, not in browser.
I have used the below code, but it opens the browser.
web = (WebView)findViewById(R.id.web_vieww);
web.loadUrl("http://www.google.co.in/");
web = (WebView)findViewById(R.id.web_vieww);
web.loadUrl("http://www.google.co.in/");
web.setWebViewClient(new MyWebViewClient());
Define MyWebViewClient();
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
}

Android webview navigate in app not open browser [duplicate]

I have a webview in my android app, but when someone navigates around the site, it opens in a new window, i want it to stay inside the webview.. is there a way to do this easily? Here is the code in my activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.co.uk");
You'll have to create a WebViewClient:
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
And then set it to your WebView like this:
webview.setWebViewClient(new myWebViewClient());
Or you can just do that, without creating a new class:
myWebView.setWebViewClient(new WebViewClient());
It works for me.
webview.setWebViewClient(new WebViewClient());
If you want to customize then you should override shouldOverrideUrlLoading (WebView view, String url). but it's deprecated in API 24. You can use public boolean shouldOverrideUrlLoading (WebView view,WebResourceRequest request). actually both of them needs to return false.
Android WebView Example

surfing all website and all links opens in same webview

when I create webview application to view any website on same webview not browser it works fine but when I click on any link in that website it opens in browser ? how to load all internal links/pages/sections when user click on them at same webview ?
Java version:
You only need to set a WebViewClient for your WebView and then all links will be opened it the same WebView:
webView.setWebViewClient(new WebViewClient());
private class myCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//here load the url in your webview
webview.loadUrl(url);
return true;
}
And this for set in your webview
webView.setWebViewClient(new myCustomWebViewClient());
Kotlin version.
webView.webViewClient = WebViewClient()
You should add a webViewClient of your own.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
in the webViewClient you can override the shouldOverrideUrlLoading() method to be like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
I hope you find it helpful.

open another webview on clicking on ad image

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

Categories

Resources