I'm a bit new to Android programming so bear with me if I'm not 100% correct on how i phrase my questions. I'm looking for an a way to stop and reload my Android webView. The webView takes the user to a a simple HTML link, which works. Now I want to implement stop, reload, forward, and back buttons for the webView. What I did for the back and forward functions was very simple-- I simply created an onKeyDown function to facilitate going forward and backward as shown below:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
if((keyCode == KeyEvent.KEYCODE_FORWARD) && mWebView.canGoForward())
{
mWebView.goForward();
return true;
}
return super.onKeyDown(keyCode, event);
}
Now for reloading (refreshing) this webView and stopping this webView from loading is my trouble. I know for reloading a webView you could simply create a new setOnClickListener function and reload the URL. I'm not so sure about stopping a webView from loading. Do I have to create individual buttons for reloading/stopping the webView? Or is there a prebuilt function I can place inside my code that can help me do so? Any help would be appreciated! Thanks!
Reload
mWebview.loadUrl("http://twitter.com"); //calling again `loadUrl()` with `url` gives your refresh type effects.
I think there ain't any method like reloadUrl.
Stop Loading
stopLoading (): Stops the current load.
mWebView.stopLoading();
Reloading the WebView
mWebView.reload();
Stop loading URL
mWebView.stopLoading ()
it will 100% work
Related
I have Actors that I need to move when the keyboard becomes visible (When I press a TextField), or they are stuck behind it. I do this by moving the camera up:
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/3, 0);
stage.getViewport().getCamera().update();
This works fine. It also works fine to move it back when I touch something outside the TextField and call stage.unfocusAll();
My problem is, when I'm in a TextField and press Androids Back button, it hides the keyboard, but does not call the code I have inside the Inputprocessor (THIS CODE CAPTURES BACK-BUTTON ALWAYS EXCEPT WHEN INSIDE A TEXTFIELD AND KEYBOARD IS VISIBLE):
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Input.Keys.ESCAPE) || (keycode == Input.Keys.BACK) )
{
moveBack();
}
return false;
}
};
I looked around and read that it is not possible to catch the back-button when inside a TextField. Which leads me to my questions:
This must be a common scenario (moving UI to work with keyboard), how do other people do it?
If other people do like me (move camera), how do you handle the Android back-button?
EDIT: This answer captures the back-key while inside a TextField, however it has to be done in the Android Launcher, so I can't reach the elements I need to reach. I also overwrites all other calls to the BACK-button from inside the LibGdx project.
Ok so with the help of This I got a solution working, but it's not very pretty.
Basically I rewrite my GDX Launcher class, and use the following code as the layout:
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
game.setMoveBack(true); }
}
return super.dispatchKeyEventPreIme(event);
}
};
Then, in the render-method of my MainScreen (Screen that all other Screens inherit from), inside the render-method I call:
if(game.isMoveBack()){
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/2, 0);
stage.getViewport().getCamera().update();
game.setMoveBack(false);
}
If anyone has an easier way to do this please post and Ill accept that as an answer, but for now, this works if anyone finds themselves where I am :)
i am building a webapp, i am using webview to load webpages. everything works fine till here.
i gave a particular url say "www.google.co.in" in webview to load it. it loads fine, when user clicks links in the particular view it goes to the corresponding page, ok..
now here is what i want i want on backbutton to work only when the webview "www.google.com" is loaed. for all the other web pages the on backpress should return "sorry you cant go back"
say something like this
if(webview=="www.google.co.in")
{
myfunc()
{
}
}
else
{
myanotherfunc()
{
}
}
WebView component has very useful methods. For example you can do it like this.
When your particular web page is loaded call WebView.clearHistory() and then, when you check if back pressed related method should be used, call WebView.canGoBack().
Considering your example code it would be:
if(webview.canGoBack())
{
myfunc()
{
}
}
else
{
myanotherfunc()
{
}
}
If you are also looking for the place where you could call above code, it's Activity.onBackPressed(). You can override this method. Please note that calling super.onBackPressed() will finish your Activity.
You can Override onkeydown method
if (keyCode == keyevent.KeyCode_Back)
// Toast.make
else
// load your web
I've got three activities in my application and I have problems with calling the above mentioned method. First of all, do I have to call it inside every activity's onCreate() method? Right now I've tried it this way, and suddenly it works only in one activity of three. In other two the default volume control bar is not shown and the volume is not adjusted. What's the right way of controling volume stream in a multiple activity application? Thanks in advance.
When you have overriden onKeyDown() etc make sure that you do call super.onKeyDown() as well for keys that you do not handle or at least for the volume up/down keys, e.g. in your view subclass:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// do your stuff here...
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
return super.onKeyUp(keyCode, event);
}
return true;
}
I am trying to handle the back button event on my app but its not working at all. I have inplemented ActivityGroup in my app according to the post Android: TabActivity Nested Activities
I have added the following code according to many posts in this website
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(this.getClass().getName(), "back button pressed: " + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d(this.getClass().getName(), "back button pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
but for some reason i still dont know i am not getting the lines to be logged, it goes back to the home screen. I know that the onBackPressed will not work for me because I need to have this app implemented using api level 4 and it is not available at this level.
My ActivityGroup has only two activities, one list view and a details view. I have put this code on all the three classes to try something different, but still cant get it working. I see "No keyboard for id 0" in the logs, but i dont think it means something that can be related to the problem.
I do appreciate any answer to this.
Many thanks
T
Add a log statement right above the return line and see what KeyEvent is happening.
like this:
Log.w(keyCode, "This is the key code that is returned");
return super.onKeyDown(keyCode, event);
Now look at the returned value and verify/compare it to KeyEvent.KEYCODE_BACK and this may point you in the direction of your problem.
I load HTML data into WebView with loadDataWithBaseURL
Do it one more time
Execute the following code and instead of going back to the 1st
page - whole app exits. What am I doing wrong here?
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && this.browser.canGoBack()) {
this.browser.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Also - is it possible for WebView cache to survive Activity#onStop?
Basically - if I close app and reopen - I want WebView to display last
data that was loaded, currently - I'll get a blank screen and then
have to reload same data again
The problem is that load* does not create a new WebView, nor does it do anything special like create a history record, unfortunately.
You probably want call startActivity() and invoke a second activity for the second set of data.