Menu button´s activity programming - android

I want to do a button that when I click it does the same what the menu button of the telephone would do.
I know for example that if I use onBackPressed(); it does the same that the back button of the telephone would do.
Anybody knows how to do that for the menu button?
Thank you

I found a link suggesting that you do this:
#Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
if(keycode == KeyEvent.KEYCODE_MENU){
//WHATEVER YOU'D LIKE TO DO
}
return super.onKeyDown(keycode,event);
}
Is this what you are looking for?
Source of the code

If you are trying to open the app's option menu, as the Menu button does, take a look here
Android Option Menu on Button click

Related

Android onKeyDown() not execute on pressing delete button

I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.
From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29

Android: handle home menu item

Context: Very familiar with iOS development and Java. New to Android.
Problem: I added a handler to navigate 'back' when the home button is pressed. It works fine, until I add another handler when the user scan button:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.home) //The ID here is no longer R.id.home
{
navigateBack();
return true;
}
else if (item.getItemId() == R.id.scan_button)
{
presentScanner();
return true;
}
return super.onOptionsItemSelected(item);
}
As soon as I add the code for the Scan the id is no longer R.id.home. It works if I do this:
if (item.getItemId() == 16908332)
{ . . etc. . .
What has happened? How can I correctly get the ID of the home button in this current activity?
Update: Changed from R.id.home to 'android.R.id.home' and this works. Why?
As pre #Romain Guy the man behind android this cannot be done see here. But for older devices you might want to try the keyevent method.. This post explores additional ways to achieve what you are looking for.
UPDATE
android.R.id.home is used in the actionbar to know that your actionappIcon is pressed(left side). While R.id.home is the reference to physical home button(or touch as in nexus 5). Have a look at this tutorial for further explanation and use cases.

Use a Dialog instead an option menu on ICS

I want to use a dialog as option menu in my application, the problem is I've understood that smartphones without buttons only show the menu button if the activity have an action menu implemented.
How can I show a Dialog instead a Menu without lost the Menu button on ICS?
Thanks a lot! Regards from Spain!
You can find your answer here. In short, you just decrease the target sdk version, and then the menu button will appear on all ICS devices. And then you just use this to detect the click:
#Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
if(keycode == KeyEvent.KEYCODE_MENU){
//do you thing here
return true;
}
return super.onKeyDown(keycode,event);
}

Gallery problem in pressing back button android

Hie team gud eve, I had a set of images like 1 2 3 4 5 6. on clicking on 1 (i.e first image) the one should show in another activity. When we swipe with the finger it should move to the next image. If i stop swiping near the image position 4. while pressing the back button on the device. The main Activity should show near the position of 4.
I already done with the swipe with the help of onFling(). The problem is when i press the back button the image is moving to the first position.I need in the same position......
Thanks in advance
manually you can override that key
i think this code will help u...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
try {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
//start the what activity you want
}
}
If you're targeting Android 2.0 or newer, you can use onBackPressed() to decide what happens when you hit the back button. This is slighlty easier to do.
#Override
public void onBackPressed() {
// Do something
}
To select a item in the Gallery:
gallery.setSelection(int selection);

can't handle keyboard events android

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.

Categories

Resources