Use a Dialog instead an option menu on ICS - android

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

Related

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.

Explicit context menu invocation works from time to time in Android

I want to show a context menu when a user presses Menu button. I found and use the following code snippet:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if((keyCode == KeyEvent.KEYCODE_MENU) && event.getRepeatCount() == 0)
{
ImageView v = (ImageView)findViewById(IMAGE_ID);
Log.v("me", "menu " + v.toString());
openContextMenu(v);
}
return true; // return false; doesn't work either
}
The view is, of course, registered for context menu, and it works ok by long touches. The problem is that the abovementioned code works only from time to time. That is the log messages are always fired, when I press Menu button, but the context menu shows up in very rare cases. The only case when it shows ok, is just after the application start. In other words, only first attempt is successfull, and all the others work occasionally, but mostly don't. The view is not changed and always found, otherwise it could produce such problem and exception in logging line.
It turned out that there was a bug in onCreateContextMenu which prevented the context menu from showing up time to time. In fact it did not populate the menu with items, and an empty menu is skipped by design in Android's core. Now it works as expected.

Menu button´s activity programming

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

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.

Android - keep options menu open

I have an Options menu up and running in my Android application and I've overridden the onCreateOptionsMenu, onOptionsItemSelected and onPrepareOptionsMenu methods to customize the menu a little.
My question is related to keeping the Options menu open after the user clicks on a menu item. Basically, I'd like to be able to hide the menu until the user clicks on the device menu key. Once the user clicks on this key, I'd like to be able to hold the menu in place regardless of how many times the user clicks on menu items. If the user wants to hide the Options menu, they'd just need to click on the device menu key again.
Is this type of interaction supported (or even advisable). If this interaction is not supported, any alternative suggestions are more than welcome.
Cheers!
Sean
This will not be possible with onCreateOptionsMenu and the other methods. They always act that way.
But you can do it another way. But there you have to program the whole menu yourself. Basically add the Menu in your layout.xml and let it be hidden (visibility = gone). Then you overwrite the methods onKeyDown. There you check if it is the Menu key. if the menu is not yet open yes, then you show the menu. If it is open already, you hide it.
should not be too difficult. Good thing as well is, that you can make the menu look exactly the way you want and as well let it react the way you want.
For anybody like me, who found this question in google:
To keep menu open after selecting item, you need this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
item.setChecked(!item.isChecked());
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(this));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
Important to return false in onOptionsItemSelected and methods of OnActionExpandListener
This solution from #MayurGajra. More details here: How to hold the overflow menu after I click it?

Categories

Resources