I'm doing an Android app and I would know if this was possible:
When the user press the return button, it does the same action as the home button.
Following the question I linked: Override back button to act like home button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Should be the solution.
This overrides the backbutton, and puts the task to the back. Which emulates hiding like the home button. Read the other question for more details, and solutions.
Related
I have attached a physical keyboard to an Android tablet using USB. How can I call a function or perform some action in an Android app on pressing a paticular key, for example when pressing the zero key?
How could I implement the below code (for the zero key)?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
// your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
It was such a silly mistake i didnt import Keyevent ..
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;
}
This is my code when I press the volume up button to next / prev songs :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
prevSongs();
return true;
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
nextSongs();
return true;
} else return super.onKeyDown(keyCode, event);
}
But when the application exits (with the home button to return to the home screen) and I press the volume up button again, it does not work.
How does it work whether it's in or out of the app?
Thank !
You need to create a service to handle your playing media. When you exit the app, you can't listen to volume events, but in service you can do it. Also, listening to volume keys to change songs is a bad idea. You should use a notification with actions (buttons).
I simply want to the options menu to toggle my sliding menu but havent found the standard click listener for the options button. Is OnPrepareOptionsMenu the only method that fires when clicking the options button? I don't really want to use this method because this method also gets fired when the application starts up.
UPDATE: for the menu button (hardware button), you can use the event onKeyUp:
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (event.getAction() == KeyEvent.ACTION_UP)
{
Log.d("onkeyup", "onkeyup");
return true;
}
}
return super.onKeyUp(keyCode, event);
}
NOTE: the solution below is for the ActionBar
Just override the onOptionsItemSelected function.
You can then listen the click event on all the menu items and trigger the actions you want according to the case:
Here is an example from a main activity class:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.menu_calendar:
makeToast("Loading...");
openCalendar();
break;
case R.id.menu_search:
makeToast("Loading...");
openSearch();
break;
case R.id.menu_settings:
openSettings();
break;
case R.id.menu_help:
openHelp();
break;
case R.id.menu_about:
mBackupManager.dataChanged();
openAbout();
break;
}
return true;
}
You need to Override menu button, so that when user clicks Menu your code would be executes instead of opening menu.Like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
//Put the code for an action menu from the top here
return true;
}
return super.onKeyDown(keyCode, event);
}
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().