I'm targeting Android >=4 and I have an issue with the ActionBar.
I need to dynamically update the action bar and all the code seems to work fine on a Nexus 7 and Nexus 4. The problem I have is with an Xperia S (Android 4.1.2). The Nexus devices update the ActionBar immediately. The Xperia only updates when the hardware menu button is pressed.
I call invalidateOptionsMenu() which in turn successfully calls the onPrepareOptionsMenu(Menu menu) on all devices. The difference is that the Xperia is simply not updating the display. As soon as I click the hardware menu button, up pops the menu items and buttons.
I've read quite a few posts and tried numerous methods - I simply can't get the Xperia to behave properly.
I've messed around with this and the best I can come up with is to recreate the activity which gives and unacceptable user experience.
Everything works fine on devices with soft menu buttons. Until I can come up with something better I've decided to force all menu items on to the action bar drop down menu for devices with a hardware menu button.
I detect the hardware menu button in the onCreate of the Activity;
this.hardwareMenuButton = ViewConfiguration.get(getApplicationContext()).hasPermanentMenuKey();
Then I have something like this;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem menuItem = menu.add("Refresh");
if(this.hardwareButton) {
menuItem.setShowAsActionFlags(android.view.MenuItem.SHOW_AS_ACTION_NEVER);
} else {
menuItem.setIcon(R.drawable.ic_action_refresh);
menuItem.setShowAsActionFlags(android.view.MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
return super.onPrepareOptionsMenu(menu);
}
Related
In my application i want to add toggle button right side of the Application-name (ie)Right side of BluetoothTextMessaging
Please help me thanks...
May this help you:
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.addSubMenu(0, 1, 1, " Button ").setIcon(R.drawable.file_icon)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
return true;
}
And you can access that button using following code & write logic what to perform on click of that button inside the following code :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1: {
// Your Logic
break;
}
return super.onOptionsItemSelected(item);
}
I believe that what you're searching for is called an options menu. On Android 3.0+ it is supported. This is how Google's own tutorial describes it:
If you've developed your application for Android 2.3.x (API level 10)
or lower, the contents of your options menu appear at the bottom of
the screen when the user presses the Menu button, as shown in figure
When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six
items, Android places the sixth item and the rest into the overflow
menu, which the user can open by selecting More.
If you've developed your application for Android 3.0 (API level 11)
and higher, items from the options menu are available in the action
bar. By default, the system places all items in the action overflow,
which the user can reveal with the action overflow icon on the right
side of the action bar (or by pressing the device Menu button, if
available). To enable quick access to important actions, you can
promote a few items to appear in the action bar by adding
android:showAsAction="ifRoom" to the corresponding elements
Follow this link: (Menu tutorial) to get to the tutorial.
I can provide code snippets, however the ones on the website are much better :-)
So I realised I've wasted a lot of time building a menu system that depends on a device with a physical menu button, which works great on all devices bar Nexus.
Right, so given that I've been using the typical onCreateOptionsMenu(Menu menu), onPrepareOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item) methods. With the usual XML layout for a menu, as you do. How does one transition to a device that has no menu button?
I would recommend ActionBarSherlock. It's backwards-compatible with older versions of Android, and the "action bar" is where you'll want to put your menu or commonly used options.
I have an application which runs full-screen and relies on the menu button. What I didn't realise is that devices like the Galaxy Tab use an actionbar which no longer has a menu button. My app currently loads a fragment displaying a settings menu when one touches the menu button:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
FragmentManager lFM = getSupportFragmentManager();
SettingsDialogFragment lSettingsDialog = new SettingsDialogFragment();
lSettingsDialog.show(lFM, "Settings");
return false;
}
I understand it is possible to add a custom icon to the action bar which when pressed could result in the same behaviour as a menu button. What I am unsure about is how to implement this.
How do I know that a device doesn't have a menu button and I need to add an icon to the action bar? It can't be as easy as checking the SDK version as apparently the actionbar was introduced in honeycomb, but my Galaxy Note runs ice cream sandwich and doesn't have an action bar (it still has a menu button). I don't want to give up any real-estate so adding buttons or menu options to my main layout isn't an option.
I just read on google developer that:
Navigation Bar New for phones in Android 4.0, the navigation bar is
present only on devices that don't have the traditional hardware keys.
It houses the device navigation controls Back, Home, and Recents, and
also displays a menu for apps written for Android 2.3 or earlier.
So I tried setting the target version on my app to 8. Instead of a menu I get a button allowing me to change the screen size of my app - but no menu button.
I have an application which runs full-screen and relies on the menu button.
That has been a bad idea for two years.
What I didn't realise is that devices like the Galaxy Tab use an actionbar which no longer has a menu button.
Such devices have been around for two years.
My app currently loads a fragment displaying a settings menu when one touches the menu button
That was never an appropriate design move. Please allow the MENU button, where it exists, to behave normally, displaying an options menu on Android 1.x/2.x and triggering the action bar overflow on Android 3.0+.
How do I know that a device doesn't have a menu button
http://developer.android.com/reference/android/view/ViewConfiguration.html#hasPermanentMenuKey()
I am using Sherlock actionbar to prevent compatibility issue of action bar but my prolem is does not worked well with android 3.0 or later here is my screen snap..
in android 2.2
now this is fine and i love it..but when i run it on AVD 4.0 it look like this
the pop-up list shown is displayed when i press menu button.
so how to do the same behavior on higher version also?
Here is my oncreateOptionmenu method..(from which menu inflated)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
I think the problem is the configuration of your Emulator. An Emulator with Android 4.0 should not have a Hardware Back Button instead it should have three software buttons (back, home, used apps)on the screen, my guess is that if a device features a menu button the action bar will get rid of the overflow icon and show the overflow menu if the hardware button is pressed.
Try to configure an Emulator without hardware buttons. For that create a new Emulator and click new in the hardware field. Now choose Hardware Back/Home keys and select no as value.
If you don't get the correct behaviour on this emulator post screenshots from that emulator to give us a further clue.
If application with Action Bar run on tablet - there are menu button in right corner. But on smartphone this button don't show, because device has a hardware menu button, I think.
I need impement similar behavior in my code to show my custom menu button only on tablet, and don't show it on smartphone? It is real?
I don't want use action bar
Thanks
You can check if the device is tablet or smarphone, and inflate the option menu only if the device is tablet.
Suppose you have a method isTablet() that returns true if the device is tablet.
Then you need to override the onCreateOptionMenu() and inflate the menu only if isTablet() returns true.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(isTablet()){
menuInflater.inflate(R.menu.menu_tablet, menu);
return super.onCreateOptionsMenu(menu);
}else{
// do nothing
return true
}
}
How to determine if a device is tabled or phone, you'll need to dig this article: http://developer.android.com/guide/practices/screens_support.html