How to show web page in android in android app only - android

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

Related

Android - Disable auto-forwarding on website

I am trying to display a website in a WebView.
The problem is that it contains the following HTML-tag:
<meta http-equiv="refresh" content="4; URL=subst_001.htm">
This causes the website do disappear (and the subst_001.htm, which contains an error message, appears)
Is there an easy way to solve this problem?
Clarification: I don't have (editing) access to the website I want to open.
I found a solution:
Using a "custom" WebView does the trick.
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new CustomWebViewClient());
public class CustomWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#TargetApi(24)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return true;
}
}

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

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

Clicking URLs opens default browser

I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?
I have enabled JavaScript. But still it's not working. Have I forgotten something?
If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:
#Override
public void onLoadResource(WebView view, String url)
{
if (url.equals("http://redirectexample.com"))
{
//do your own thing here
}
else
{
super.onLoadResource(view, url);
}
}
Official documentation says, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
or if there is no conditional logic in the method simply do this
myWebView.setWebViewClient(new WebViewClient());
Add this 2 lines in your code -
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
The method boolean shouldOverrideUrlLoading(WebView view, String url) was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).
You can use both by doing something like this:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
} else {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
Arulx Z's answer was exactly what I was looking for.
I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
exactly under your WebView statement.
Here's a example of my implemented WebView code:
public class WebView1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
wv.setWebViewClient(new WebViewClient()); //the lines of code added
wv.setWebChromeClient(new WebChromeClient()); //same as above
wv.loadUrl("http://www.google.com");
}}
this way, every link clicked in the website will load inside your WebView.
(Using Android Studio 1.2.2 with all SDK's updated)

Categories

Resources