I want to paging a web's content in an android application. For example, i use Flipview to display each page of web content, and i don't know how to using the same webview which i created, loaded url to display on each flipview page.
Anyone as any suggest for me !
Thanks for all suggestions !
use webview.cangoback() property.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
for more details please visit. http://developer.android.com/guide/webapps/webview.html
hope this will help you
Related
When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?
I guess what I am trying to do is to understand the correct behavior I should be employing.
Let's look at some popular browser. currently, in my device, there are two browsers
chrome
default internet browser
In both browsers, it goes to the previous page. So it is a common behavior. to do so
you can overwrite the onBackPressed to go to your previous page.
#Override
public void onBackPressed(){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}
else{
finish();
}
}
Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.
And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().
In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.
You can use this override the onkeydown event and check the key pressed (in this case is back key) and check if is there any previous page
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When you are clicking back button, you are clicking it for activity that is opening the webview better thing would be to give a back button in you layout..and hence saving the last url opened by user
Something like this in activity with WebView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or override onBackPressed method:
#Override
public void onBackPressed() {
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
} else {
finish();
}
}
My application is set up using ViewFlipper so that I have one main View which links to three other Views, each through their own button. As I've set it up this way, when I'm on one of the three other Views if I press the back button on my android device it backs all the way out of the app. I Just wanted to know if it was possible to map it to just go back to the main View?
Override onKeyDown() :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Place your code here!
}
return true;
}
Id like my full screen mobclix ads to be non destroyable for 2 seconds. Right now users can press the back button on the android phone before the ad even shows up. How can I accomplish this?
All you need to do is intercept the back button press which you can do by overloading:
onBackPressed()
On older devices this won't quite work and you'll need to do something like:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Right now when you hold down the menu button on my android device it pulls up a soft keyboard.Is there a way to override this? I would rather choose what happens on a long touch of this button.
When using onKeyLongPress it only detects when the "Back" button is held down. How can I make this work for the menu button?
For this, you can use the onKeyLongPress()-method, offered by the KeyEvent.Callback-class (can be used in Activity's too, since they are a subclass of the KeyEvent.Callback-class).
There is also a little trick to make this work: You'll have to tell Android to track a long-press click on the "Menu"-button as the onKeyLongPress()-method will not be triggered otherwise. This is done in the normal onKeyDown()-method.
So your code might look like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_MENU){
// Do what you want...
Toast.makeText(this, "I'm down!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyLongPress(keyCode,event);
}
A great article with further informations can be found on the Android Developer Blog.
In my app when user clicks on a gallery widget i open the image in a webview as it supports zoom functionality.
Now when user clicks back button on webview i want the user to see my application. But it goes directly to home screen.
How should i handle the onBackKeyPressed() ?
Override the Activity's onKeyDown(..) event
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// close image gallery
return false; // this avoids passing to super
}
return super.onKeyDown(keyCode, event);
}
*edit: code
#Override
public onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();