on OptionsMenu click listener - java

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);
}

Related

How to handle different keyboard action in Anrdoid app?

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 ..

Custom hardware button events/intents

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;
}

Disable sound when volume keys clicked

In my application I override volume up and volume down keys. The problem is that when user click one of these two keys, sound is played. I want somehow to disable/mute this sound.
Here is fragment of my code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN: {
// do something when user click volume down key
return true;
}
case KeyEvent.KEYCODE_VOLUME_UP: {
// do something when user click volume up key
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Using onKeyDown or onKeyUp still gives the default sound of changing the volume. Use dispatchKeyEvent instead.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
return true;
default:
return super.dispatchKeyEvent(event);
}
}
I figured it out for myself.
There is a way to stop the sound when volume key is pressed. You need to play an empty sound infinitely, so when user click a volume key it is "trying" to change the media volume, so there isn't click sound played.
You can download an empty sound by searching for empty sound and then put it in your raw folder in the project.
The last step is just to override the volume keys.
Here is the code you need:
Define MediaPlayer variable.
private MediaPlayer infinite_sound;
In onResume() you need to play the empty sound infinitely.
#Override
protected void onResume() {
super.onResume();
infinite_sound = MediaPlayer.create(this, R.raw.empty);
infinite_sound.setLooping(true);
infinite_sound.start();
}
In onPause() you need to stop the sound.
#Override
public void onPause() {
super.onPause();
infinite_sound.release();
}
And finally overriding the actions of the buttons.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN: {
// Do something when volume down button is clicked
return true;
}
case KeyEvent.KEYCODE_VOLUME_UP: {
// Do something when volume up button is clicked
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Override back button event of top menu issue in Android

I want to back to previous activity from the current activity.
So I added this code to the current activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//super.onBackPressed();
//NavUtils.navigateUpFromSameTask(this);
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
But it doesn't fires at all.
What do I am missing?
Override the onOptionsItemSelected() method in your Activity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // the default resource ID of the actionBar's back button
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
break;
}
return true;
}
You get the clicked menu item id using item.getItemId(),
then you check if it's equal to android.R.id.home, the default resource ID of the actionBar's back button.
The FLAG_ACTIVITY_CLEAR_TASK flag finishes all the old activities.

Button return = action button home

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.

Categories