Android WebView NOT launching new browser - android

Previously when using a webview in android, clicking any link would force the "proper" browser to open and then web browsing would continue in that instance.
However this seems to no longer be the case. The following code results in all links staying inside the webview, but I actually want the links to launch a new browser instance. Did this change in 2.3 ?
Note I am asking the opposite of what most people ask (they ask how to keep all links inside the webview, I want them to launch outside)
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView view = new WebView(this);
setContentView(view);
view.loadUrl("http://news.bbc.co.uk");
}
}
EDIT: Clarification - the first URL will load in the WebView and all subsequent clicks will open in a new browser. This is simplification from what I really want, but good enough.
Bascially the problem is, previously clicking a link would open a new browser session. Most people don't want that (hence the questions on here about it) but I do. However for some reason now it seems to load in the webview all the time (2.3 perhaps?)

Let me understand. Do you want to launch the first webpage in the same webview, while the other hyperlinks to go to the default browser, or you want the default browser itself to open for the first link clicked?
If it's the first case, I don't exactly know, unless you know some way to gather the link URL from the webview.
For the second instance, i.e. launching the default browser for any URL click, just skip (remove) this line: view.setWebViewClient(new WebViewClient() { });, which will open all the links in the default browser, and not the webview itself.

Houps, wrong answer, I misunderstood your question.
Need to delete...

Related

Android: is it possible to go to a different activity once you log into website via WebView

I want to create a native Android application based on my existing website.
Due to extreme time constraints and the fact that we don't have an API server set up, I think it'd be fastest to just embed the website as a WebView in my mobile app.
So, here's what I had in mind for the mobile app:
The website has a login page.
I want that login page to be the the first screen that comes up when you enter the mobile app.
Once you hit the "sign in" button on the WebView login page and the login credentials are valid, I want it to take you to a new activity (I have no clue if this is possible since you have to detect the button click on the webview and then determine whether the credentials are valid).
Apologies, in case it's not painfully obvious, I'm not really a mobile developer. How would you guys go about this?
Does that next activity still contains WebView inside and just want display different buttons through another activity?
If that is the case, you can use this code
webview.setWebViewClient(new WebViewClient()
{
// Override URL
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equals("http://Url displayed after successfull log-in"))
{
Intent i = new Intent(getApplicationContext(), APImages.class);
startActivity(i);
return true;
}
return false;
}
});
But if the case is after you logged - in through web, user will be redirected to Activity and will display data from WebView, it is impossible considering that you have no API to communicate with your Web App.

closing webview in android

I am using Twitter4j but I need to authenticate a user with oAuth. The user needs to get a pin and input it into the app. When I load a webview with the url and the user accept's to give the app privileges, they receive a pin. How would they close the webview in order to input the pin. I cant close the webview in the emulator. Below is the method in the Async class where the webview is shown after the url is retrieved.
protected void onPostExecute(String url) {
WebView webview = new WebView(MainActivity.this);
setContentView(webview);
webview.loadUrl(url);
}
You can't "close" a WebView. What you could do is hide it. Maybe this solution will shed some light - Hide WebView until JavaScript is done
Well, you could
instead use another dedicated activity with mainly a webview [potentially also with 'done' button]. You could start that activity from your onPostExecute with intent, and then you will go back when user is done with it, or you could start that webview activity for result
Another option is to replace your webview when you are done with another view, for example by calling setContentView with another layout. I never did this myself, and not sure it will work easily, but I found that some people recommend using ViewFlipper for similar effect.
Hope that helps.
You can call finish() from anywhere inside the Activity class
This works for me:
webview.destroy();
Add a close button and on its click set:
webview.setVisibility(View.INVISIBLE);
webview.loadUrl("");
You can then reduce the height width of the web view to minimum.

webview : how to handle javascript code from multiple web pages

In a WebView, how do you access the JavaScriptInterface from multiple web
pages, and not just from the page that you load via mWebView.loadUrl("http://10.241.139.45:9081/amexco/login.html")
e.g.
In my project's assets folder, I have:
login.html
page1.html
page2.html
... and here is the relevant WebView code:
webView.addJavascriptInterface(new JavaScriptInterface(), "android");
webView.loadUrl("file:///android_asset/index.html");
So at different points in time, a user may look at index.html, page1.html, etc. From what I can tell, only the JavaScript in index.html has access to the JavaScriptInterface via "window.android.someMethodName()". When I try to make a call to "window.android.someMethodName()" from page1.html, it says that "window.android" is not defined.
so the user starts the webview from
login.html page and navigates through different pages..
my requirement is in the last page of my webpage i have a button saying return to android activity. so when user clicks on the last page of my navigation the user has to come back to the android activity.
window.android.someMethodName()" can be called in html page which works fine. but this method will work if i call this method only from the first page which i loaded ie from the login.html page only this method will work.. if i call this method from the last page of the webview naviagation this is not working.
Can any one help me out in this.
Get rid of the window.. If you are injecting a virtual global named android, you call methods on it as android.someMethodName().
Also, make sure that you are viewing your subsequent Web pages in the WebView. By default, links bring up the user's default Web browser, which will not have your injected virtual global.

Android: webview

I am trying to wrap a web application say www.xyz.com within a webview using shouldOverrideUrlLoading. I have two activity one that launches the application, this checks all the activity running and determines whether to launch new instance or not and other activity which wraps my application www.xyz.com in the webview.
I'm facing two problems:
When I press the sleep button and then again press it to open the device, or when the application is idle and went to sleep and I press the button to open the device, the application restarts (the webview restarts).
Also when link in the webapplication which opens someother site say for example www.abc.com doesn't work fine, it works for the first time and opens the site in external browser as desired, however on further click tries to open the site within the webview which is not desired.
I have googled this and found suggestion like the code below:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("abc")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
However, in my case for the first time the site www.abc.com is opened in external browser but for the second time its open within the webview, i've debugged it in eclipse and found the url value comes the same in subsequent cliks .
1) For the WebView restart problem, you need to add the following lines of code to your activity that contains the WebView
android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"
Also see tutorials on how to save state of WebView in Bundle.
2) For your external browser problem, try
url.equals("www.abc.com")
instead of
url.contains("abc")
Hope that solves your problem :)

Android: open URL in a new window WITHOUT leaving app

Is there anyway I can open web when user clicks on TextView, in a new window without leaving the app, it's like in iPhone when you click on an URL it shows you a new browser window, but there is a BACK button you can click to go back to previous screen?
Thanks!
You can create your own Browser Activity with a WebView in the layout. Inherently it is not going to have any of the functionality of the actual stock browser app though, other than displaying the web page. You'll have to add in the forward / back buttons if you want them and anything else you need. If you don't need any of that stuff then you should be fine with just a plain WebView. When you press the back button on the device it should close that activity and take you back to the one you started in.
Edit:
To get callbacks from a WebView when the user clicks a link you can use the WebViewClient shouldOverrideUrlLoading() method, like this:
wv.setWebViewClient(new WebViewClient() {
/* On Android 1.1 shouldOverrideUrlLoading() will be called every time the user clicks a link,
* but on Android 1.5 it will be called for every page load, even if it was caused by calling loadUrl()! */
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.i(myTag, url);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) ;
startActivity(i);
return true;
}
});
For a TextView its a little bit more work, You'll have to make your own copy of the Linkyfy class and use a TransformFilter to make the links behave however you want them to. Check out this question for an example Android Linkify both web and #mentions all in the same TextView
Maybe a WebView is what you want.

Categories

Resources