How can I do this? I have an HTML button and I'd like to open an intent when I click on that button.
You can only do this if you control the Webview that you are using to display the web page. If you're talking about making this work in the Browser app then you're out of luck.
If you do have control over the Webview you'll want to use [addJavascriptInterface][1] to create a link between your webpage and your Java code
[1]: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
Related
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.
Hi i am having a activity with a button, on click of the button it has to load a custom browser in a new activity not the default browser of android. and i need a way to exclude the history of the browser such that on back press it comes back to the previous activity without navigating to previous website. I am new to android and any ways to do this
You are probably just talking about the WebView
Link to the official tutorial
Note :
This onKeyDown(int, KeyEvent) callback method will be called anytime a
button is pressed while in the Activity. The condition inside uses the
KeyEvent to check whether the key pressed is the BACK button and
whether the WebView is actually capable of navigating back (if it has
a history). If both are true, then the goBack() method is called,
which will navigate back one step in the WebView history.Returning
true indicates that the event has been handled. If this condition is
not met, then the event is sent back to the system.
You can start by putting a WebView in an activity.
If you want to have a view which can browse the web in your app then make use of the WebView view.
Else if you want to create your own custom browser, then you have to make use Web engine WebKit present in the library layer of the 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...
my android app is simple client for web site. It opens site's url, using JavaScript and DOM enters username and password (predefined) and clicks "Log in" button. It uses WebView class.
Question: how during that process I could show some "ajax" loading image instead of showing webweiv on the same activity?
THank you!
You can set a WebViewClient to your webview and use the callbacks onPagestart and onPageFinished to show the progress.
I have a webview with a banner and when I click on the banner there should open a second webview that follows the link. How can I do that? I have created the first WebView and it shows my banner but when I click on it, it opens the link in the same WebView.
How can I catch any events in the WebView when I click on a link that it should do something (with that link)? Just like shouldStartLoadWithRequest in iPhone.
Thank you,
Wouter
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.
shouldOverrideUrlLoading good method, but if you click on current link webview client not calling to shouldOverrideUrlLoading method.