I've implemented a HybridWebView as described here: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/
For now, I have the url hardcoded in the renderer, like this:
Control.LoadUrl("http://www.google.com");
The result is the url opens, but always in a new browser, not embedded in my ContentPage as I would expect.
Is there a Settings property on the Android.Webkit.WebView I can use to determine how it displays?
The result is the url opens, but always in a new browser, not embedded in my ContentPage as I would expect.
For android platform, it uses Android.Webkit.WebView as native control as you can see it from code:
if (Control == null) {
var webView = new Android.Webkit.WebView (Forms.Context);
webView.Settings.JavaScriptEnabled = true;
SetNativeControl (webView);
}
Then for Android.Webkit.WebView, if you don't enable the activity to handle the intent to view a web page, it is a simple web page viewer, not quite a browser yet because as soon as you click a link, the default Android Browser handles it. To enable it, this code can go anywhere following the initialization of WebView: webView.SetWebViewClient(new WebViewClient()). So you may add this code for example:
if (Control == null) {
var webView = new Android.Webkit.WebView (Forms.Context);
webView.SetWebViewClient(new WebViewClient());
webView.Settings.JavaScriptEnabled = true;
SetNativeControl (webView);
}
Usually we can also subclass WebViewClient and override ShouldOverrideUrlLoading, if it returns true, means that method has handled the URL and the event should not propagate.
Related
In my app I am trying to open web application which needs flash player but as I am new to android I only know opening web view with chrome client. is there any method to open webview with any other browser instead of chrome.
I am planing to open that web activity in puffin browser as it supports flash player
Write this to open URL in your own webview :
web.setWebViewClient(new myWebClient());
To enable Javascript :
web.getSettings().setJavaScriptEnabled(true);
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView.
EDIT
Add this to your code and see if its work:
Make sure shouldOverrideUrlLoading(...) should return false to prevent the default browser from being opened.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
I am trying to write an app I want it to open a web page and auto login I am not sure how to go about sending the info to the browser from the app code.
So basically you are going to need to load in the webpage within a WebView (You can find instructions for that here and then probably push javascript into the WebView that will fill in the fields and load the page.
In your activity's onCreate:
WebView webview = new WebView(this);
setContentView(webview);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean onPageFinished(WebView view, String url) {
// Check here if url is equal to your site URL.
}
});
webview.loadUrl("http://yourwebsite.com/");
This line enables javascript in your WebView:
webView.getSettings().setJavaScriptEnabled(true);
Then you can use the WebViewClient to detect when the page you want has fully loaded. When that happens, you can use:
webView.loadUrl("javascript:document.getElementsByName('username').value = 'username'");
webView.loadUrl("javascript:document.getElementsByName('password').value = 'password'");
webView.loadUrl("javascript:document.forms['login'].submit()");
And it should automatically log you in. It's worth noting that this generally isn't easy to do on a lot of sites since they will randomize the login control ids and it also doesn't generally sit well with users if an application is logging into a website automatically for them.
I am developing an android application in which I want to show a website, which have links in it. When any link is clicked, then it plays an online stream.
Till now I have developed an app which work alright. In this I have used webview to display first screen. The live stream is not supported in webview, so I used webchromeclient.
Now the problem is when any link is clicked, a new browser opens and plays the stream and also shows the address bar and address of page loaded page.
I want to hide the address of new loaded page.
and if possible, also I wnt to keep webchromeclient in existing screen, not a new browser.
in this case you have to customize the WebViewClient like this
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
return true;
}
}
}
Then create an instance of this new WebViewClient for the WebView:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
for further have a look on this, and read carefully
Hi, everybody.
This is my code. I've created a menu in my app. One of the items opens a URL correctly. But I would like to open the URL in a WebView with this WebSettings. But it doesn't work.
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case WEBSITE:
Toast.makeText(Activity.this, "About", Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("http://www.english.com/about");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
WebView web = new WebView(this);
WebSettings webSettings = web.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
startActivity(it);
return true;
Those web settings don't work.
Starting the Browser to view a webpage means you're subjecting yourself to however the browser will present the url, and you have no control over the defined settings of another Application. If you're trying to use your own WebView, you'll load the URL like this:
webview.loadUrl("http://www.english.com/about");
There are 2 things you are doing wrong: 1) you create a new WebView but never attach it to your view tree and 2) you ask an outside app to handle the viewing of the url you want to display. You need to either add the WebView to your xml layout file or attach to the view tree by using view.addChild(web); on your root view. Once the WebView is part of your view tree just call web.loadUrl(...); to load the web page. Since you don't want to open the web page on an external browser, you should just get rid of your Intent it =... and startActivity(it);.
If you want to open the web page on a different activity, just create a new activity with a webview in its layout. You can find an example on how to do it here.
I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.