I am working on the following code:
private class HandleBackButton implements OnKeyListener
{
#Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg1==KeyEvent.KEYCODE_BACK)
{
showResults(0);
}
return true;
}
}
I am somewhat new to android and my purpose is to operate the above code when the back button is clicked. User can click the back button any time. But, how can I set this listener to the Activity? I can't find something like this.setOnKeyListener().
I am using Android 2.3.3.
For the Activity you should override onBackPressed which is invoked when you press the back button. OnKeyListener dispatches key events to the view. You find setOnKeyListener defined in the View class
Interface definition for a callback to be invoked when a hardware key
event is dispatched to this view. The callback will be invoked before
the key event is given to the view. This is only useful for hardware
keyboards; a software input method has no obligation to trigger this
listener.
Just override the onKeyDown() method of Activity.
You don't have to set a listener then.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showResults(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
Optionally you can also override onBackPressed() if your api level is >= 5.
You can use onBackPressed():
#Override
public void onBackPressed() {
showResults(0);
}
Related
I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
I have:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// Do your thing
TheTimer.cancel();
TheTimer.start();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
Right now I have a countDownTimer and I want to call onKeyDown() within it. I know that I want
onKeyDown(KEYCODE_VOLUME_DOWN, ?????)
What is it I put into the second argument?
Move your code in onKeyDown() into a another fuction. And it's not ideal to manually call fuctions like onKeyDown as they called automatically by the system when that events happens.
void keyDown() {
TheTimer.cancel();
TheTimer.start();
}
Then in the onKeyDown() and inside the countDownTimer call the keyDown() method.
Can I modify data which is sent to OnTouchListener? My situation looks like this:
class A extends View{
public A(Context context){
super(context);
}
...
}
And in some activity I have:
A a = new A(this);
a.setOntouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
// event should be modified
....
}
}
If a touch event occurs, I want to modify it, inside class A (for example change event's coordinates) so that inside onTouch method I can use modified event.
There is no way to modify what gets sent to the onTouch, I would go this route instead. Note that Coordinates is just a made up name, I am sure something similar exists but you could just create this class if not. Any parameters that can be accessed from within the touch listener could be sent to the method as well, if not you may have to set a class variable to get the value you need to check inside of the getModifiedCoordinates method
A a = new A(this);
a.setOntouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Coordinates coords = getModifiedCoordinates(event);
//do something with the coordinates
}
}
...
private Coordinates getModifiedCoordinates(MotionEvent event) {
boolean shouldBeModified = <the conditions you are checking for>;
if(shouldBeModified)
return new Coordinates(modified_x,modified_y);
else
return new Coordinates(event.getX(), event.getY());
}
I would like to start a new activity for a result, with startActvityForResult(), but I would like to have the back button working as normal in the new activity.
Currently when I invoke a new Activity for result, nothing happens when I press the back button in the new Activity.
I tried something like this:
#Override
public void onBackPressed() {
setResult(0);
super.onBackPressed();
finish();
}
in the new Activity, but it didn't work. Still nothing happens when the back button is pressed.
Is there a way around this?
EDIT : I could of course load the last Activity in the onBackPressed() (can I?), but it seems like a rather crappy hack.
Alex Ady's answer solves my problem, but I still don't understand why onBackPressed() doesn't work. The working code now is something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
setResult(1);
finish();
}
return super.onKeyDown(keyCode, event);
}
I could use an explanation.
You could try
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
You shouldn't have to override the Back button behavior at all. By default, if the user presses the back button, the result will be Activity.RESULT_CANCELED.
Try getting rid of the line that contains the finish().
I'm trying to override the onBackPressed() method of the ActivityGroup class:
public class MyClass extends ActivityGroup {
#Override
public void onBackPressed() {
// do something
return;
}
but I'm getting the error The method onBackPressed() of type MyClass must override a superclass method.
I'm relatively new to Java, I've seen here that people do it and it works for them
Why I'm getting this error?
I'm writing an app for android 1.5, could the problem be here?
Edit
You need to override the back button like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}
Yes u r correct onBackPressed() method has been introduced only in API Level 5 which means you can use it only from 2.0 SDK