Change visibility of ActionBar item on change tab - android

I have Activity with 3 tabs as Fragments. I also have 3 ActionBar items. On each tab only one item should be displayed and others should be in overflow menu. My code looks like this
private void updateMenuItemsVisibility()
{
MenuItem itemAddGate = menu.findItem(R.id.action_add_gate);
MenuItem itemAddLinking = menu.findItem(R.id.action_new_linking);
MenuItem itemNewConversation = menu.findItem(R.id.action_new_conversation);
MenuItemCompat.setShowAsAction(itemNewConversation,
MenuItemCompat.SHOW_AS_ACTION_NEVER);
MenuItemCompat.setShowAsAction(itemAddLinking, MenuItemCompat.SHOW_AS_ACTION_NEVER);
MenuItemCompat.setShowAsAction(itemAddGate, MenuItemCompat.SHOW_AS_ACTION_NEVER);
if (viewPager.getCurrentItem() == 0)
{
MenuItemCompat.setShowAsAction(itemNewConversation,
MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
if (viewPager.getCurrentItem() == 1)
{
MenuItemCompat.setShowAsAction(itemAddLinking, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
if (viewPager.getCurrentItem() == 2)
{
MenuItemCompat.setShowAsAction(itemAddGate, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
}
}
and it works... on Android 4.x. On 2.1 line for hiding seems not to work. When I change tab to second tab 2 items appear, on third 3 items. Switching to previous tabs does not hide items. I'm using ActionBarCompat. Is it a bug?
edit: Actually it also behaves like this on Samsung Galaxy S (first one) running CyanogenMod on Android 4.1. So it might be connected to situation when we have physical button instead of software keys.

I'm not quite sure but the problem may be that API < 11 do not know the SHOW_AS_ACTION_ALWAYS as the the actionbar wasn't implemented yet.
That's why you have to add 2 additional lines of code in your menus like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/new_sms"
android:title="#string/create_sms"
android:orderInCategory="1"
android:showAsAction="always"
yourapp:showAsAction="always"
android:icon="#drawable/arrow_right" />
</menu>
The lines xmlns:yourapp="http://schemas.android.com/apk/res-auto" and yourapp:showAsAction="always" make it possible that the icons are always seen. Even for API 7 - 10.
Else the menu items will appear in the overflow menu.
I'm not sure how to implement this programmatically but you could write 3 menu ressources like that and call onSupportInvalidateOptionsMenu() to let the fragments show their own menus.
If you do so don't forget to call setHasOptionsMenu(true) in the fragments' onCreate().

Related

the setting options in another activity

in my app on the top right of the action bar, I have the setting as three dots (as the usual setting). but when I click on it, it shows a white rectangle that has the word "setting" on it! But I don't want that!! I want just the three dots and when I click it I want it to take me to another activity right away(without showing the option setting on a rectangle).
this my code for the setting xml:
<item
android:id="#+id/action_settings"
android:orderInCategory="0"
android:title="#string/action_settings"
android:textColor="#color/colorBackground"
app:showAsAction="never" />
</menu>
The three dots you're talking about are usually there if you don't have enough room in your action bar and you want some of your menu items to show in a menu instead of directly displaying it to the action bar. The best thing for you is to create another item and set app:showAsAction to always and call a method using onOptionsItemSelected event:
<item
android:id="#+id/your_id"
android:title="your title"
app:showAsAction="always" />
Call it like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.your_id:
//code here...
}
return super.onOptionsItemSelected(item);
}
Currently your app has a default menu that has a settings item added. You can create your own menu options.
Create a new menu.xml file that contains 1 item whose image is the three dots.
In your class, inside the onOptionsItemSelected method, call the activity that you want to open.
Hope this helps :)

How can I remove dropdown actionbutton overflow?

I'm trying to remove the dropdown menu ActionButton Overflow, from Action Bar. In the case, it would be the one that has 3 points and default option "Settings".I would like to remove this item "Settings", so that, by clicking on the menu icon, he has to take action automatically. Any idea?
go to folder menu->main then remove item settings then you can add another item (for example)
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"
android:icon="#drawable/refresh">
</item>
and in your activity:
onOptionsItemSelected(MenuItem item) // write your switch case to handle the clicked item.
the line that allows the option to be directly on the action bar is:
android:showAsAction="ifRoom"
You cannot get rid of the overflow. You can ask for the action bar to consider making one (or more) of your action bar items be toolbar-style buttons in the action bar. To do that:
if you are using the native action bar, add android:showAsAction="ifRoom" to the <item> in your menu resource
if you are using the appcompat-v7 action bar backport, add app:showAsAction="ifRoom" to the <item> in your menu resource (assuming that you have the appcompat-v7 namespace set to be app)
However, the decision on whether or not to show the item as a toolbar button or to put it in the overflow is up to the action bar, not you. For example, if you ask for 8 items to all be toolbar buttons, while that may be possible on a 10" tablet in landscape, there will not be enough room for all of them on a phone-sized screen in portrait. Some of your items will be toolbar buttons; the rest will go into the overflow.
The Menu is inflated in the onCreateOptionsMenu method, and uses a menu XML in the menu folder (default) to retrieve items. If you would like to perform an action on the keycode menu, you can use the following
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
Toast.makeText(getApplicationContext(), "Menu Pressed", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keycode, e);
}
if you have no use for menu items, you can also go ahead and get rid of the onCreateOptionsMenu and onOptionsItemSelected methods, as well as the menu XML, that is up to you though
You have to remove all methods that create the menu (onCreateOptionsMenu etc).
Then in the XML file for the Toolbar, you have to add in the toolbar a custom icon. For example:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<----YOUR CUSTOM ICON/BUTTON HERE---->
</android.support.v7.widget.Toolbar>
Then programmatically you can add an onClickListener on the custom icon/button and you can do whatever you want with it then.

menu not visible on tablet

After hours of browsing the internet, I am still having a problem showing a menu on my app.
I have a viewflipper with in it several scrollviews and they have several layouts as well.
The whole app works great, I can swipe from scrollview to scrollview without any problems.
Before I connected a Samsung Galaxy Tab to play with, I was running the app in the emulator. There if I pressed the menu button, the submenu was showing and I was able to run functions on menu item touch.
On the tablet, the menu is not showing anywhere.
Here is my menu file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/KPNsites"
android:icon="#drawable/ic_menu_name"
android:title="KPN Sites"/>
<item
android:id="#+id/VDFsites"
android:icon="#drawable/ic_menu_name"
android:title="Vodafone sites"/>
</menu>
In my activity I have this code:
#Override
public boolean onCreateOptionsMenu(Menu my_menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, my_menu);
return super.onCreateOptionsMenu(my_menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.KPNsites:
getKPNsiteInfo();
return true;
case R.id.VDFsites:
getVodafoneSiteInfo();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When I run the app on the tablet, I only have a small bar at the top with only te app-name in it. I suspect the menu should be visible on that bar?
What else should I do / have done (differently)??
rg,
Eric
If your tablet is running Android 3.0 or greater you should change targetSdkVersion to <= 10 in manifest file.
You may want to consider using ActionBarSherlock this will give your app uniform and modern look on any Android version.
Try this example:
Menu Example
well gave it up for now.
every try messes up what I already got.
To get by, I've put buttons on the layout.
May be with the next project I get it working.
(but then by starting with it before I have all my layouts finished)
Thanks for the help!
rg,
Eric

Android ActionbarSherlock show menu button at top

I am trying to implement ActionBarSherlock in my application and I need to show the menu at the top of my application, integrated in action bar. The problem is that it's not working properly. I am using it like this :
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Settings")
.setIcon(R.drawable.ic_action_settings)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
So the thing which I want to achieve is to show at the right side of my action bar a refresh icon and a menu icon, which should open the default menus when clicked. I checked ForcedOverflowItem example in ActionbarSherlock Demos, but it's not working as I want. I need to look the same as in Android 2.+ and in Android 4.+.
Any advices / helps / suggestions how can I get this to work?
You can do an xml file line this.
I saw this in some other post but I cannot track it rigth now.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/more"
android:showAsAction="always"
android:icon="#drawable/abs__ic_menu_moreoverflow_holo_dark">
<menu>
<item
android:id="#+id/remove"
android:showAsAction="withText|never"
android:title="#string/remove">
</item>
</menu>
</item>
</menu>
Then just inflate it like a normal menu.
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.overlow, menu);
return true;
}
From what I read in ActionbarSherlock's documentation you cannot force the menu icon to appear in Android 4.+. When the device has a menu button the menu icon does not appear. I guess the guy who wrote ActionbarSherlock knows the subject matter well ;-)
Please read the dorjeduck's answer. If you want have same experience on all devices you have to add custom menu with his submenus. Here this the code sumple:
SubMenu sub = menu.addSubMenu("More");
sub.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
sub.add(0, MENU_SETTINGS, 0, "Settings");

Why the pictures/icon don't show up in the menu?

I followed this tutorial to create a menu
but my menu looks differently:
How can I create a menu with images?
This is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/preferences"
android:icon="#drawable/preferences"
android:title="Preferences" />
<item android:id="#+id/help"
android:title="Help"
android:icon="#drawable/ic_action_search" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/*menu.add(Menu.NONE, PREF_ID, Menu.NONE, "Preferences")
.setIcon(R.drawable.preferences).setAlphabeticShortcut('e');
return (super.onCreateOptionsMenu(menu));*/
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_ygo_main, menu);
return true;
}
First of all I want to say : Say Goodbye to the Menu Button
Your code have no problem, and it should be showing the icons if the drawables are there in correct folder,Working fine on Android 2.2.
The Menu features says :
1. Context menus: Do not support item shortcuts and item icons.
2. Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged.
3. Sub menus: Do not support item icons, or nested sub menus.
No problem with your code, Problem may be with the API level you are using, but still want to suggest that don't use Menu anymore.
Android no longer requires a dedicated Menu button, some devices don’t have one, and you should migrate away from using it.
If you use some following attribute in manifest file then it's will be show your icon....
<activity android:name=".ui.CategoryActivity"
android:label="#string/app_name"
**android:theme="#android:style/Theme.NoTitleBar"**></activity>
It's work fine for me...:)
**must be enter.

Categories

Resources