How to open a URL with specific settings? - android

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.

Related

Auto login on web page

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.

Is there a way to hide addressbar in webchromeclient?

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

How do I open another app in my WebView app?

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.

How do i perform an action on click of a particular link available on website?

I have an application in which i have added a menu.
Clicking on this menu opens up a website.
There is a list of links(zip files) available on this website.
Clicking on a particular link should result in that zip file to be downloaded to the assets folder of my application.
I am able to load the website.
Code for this:
String url = "http://almondmendoza.com/android-applications/";
Intent k = new Intent(Intent.ACTION_VIEW);
k.setData(Uri.parse(url));
startActivity(k);
I am referring to the example given on this website
What i am curious to know is that whether it is possible to perform an action on click of a particular link available on website. If it is possible then how can i accomplish this task?
Use WebView to load webpage, you can recognize URL using following code
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
webView.loadUrl(url);
// Here the String url hold 'Clicked URL'
return false;
}
});

Android webview url redirect

I have a webview in my application which upon launch displays an html page. The page has a hyperlink which on click is supposed to display a video.
When i run the application and click on the video hyperlink link , nothing happens. But if i load the same page in android browser, then it launches a default video player and everything works fine.
I debugged it furthers by putting a log statement in shouldOverrideUrlLoading method and noticed that, when the hyperlink is clicked it gets redirected to another link and then to another link (final video streaming url).
My question is : why would the link work perfectly in default android browser and not through a webview.
Thanks
What is happening is when you click the hyperlink, that link probably has some popups inside of it. You need to define the onCreateWindow function in your webview's WebChromeClient. This handles how calls to open new windows or popups are handled.
public boolean onCreateWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
((WebView.WebViewTransport) resultMsg.obj).setWebView(myWebView);
resultMsg.sendToTarget();
return true;
}
After declaring your WebView you should set javascript enabled, then your WebView will work as a browser.
For example:
WebView mwebview = new WebView(this);
setContentView(mwebview);
mwebview.getSettings().setJavaScriptEnabled(true);
or
mwebview.getSettings().setPluginState(PluginState.ON); // this is for newer API's
Basically, do not expect your embedded WebView works the same as Android default Browser. The default Browser is built on the same WebView, but there are lots a customization. (Especially around the no-standard uri, HTML5 stuff)
I followed code from here: WebView and HTML5 <video>, and I put the video link to a video tag, and I got the Video playing in my own version of WebView. The behavior is a little different from the default Browser. Given more time, we could figure that out by looking at its code, but anyways ...

Categories

Resources