I am currently working on an android project and I am having a bit of a strange issue. Its probably something really simple I'm missing but can't see what I've done wrong.
I've added a new menu item to my XML file and then I am trying to reference the new menu item to either show it or hide it. The menu item is being shown on the screen when I run the app so I know I have the XML correct and have the correct file but when I try and do findViewById it returns null.
Below is the code.
MenuItem mnuUpgrade;
mnuUpgrade = (MenuItem)findViewById(R.id.mnu_upgrade);
if (common.checkForProVersion())
{
//mnuUpgrade.setVisible(false);
}
else
{
//mnuUpgrade.setVisible(true);
}
and below is my XML file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/mnu_addLogin"
android:title="New Login"
android:icon="#android:drawable/ic_menu_add"
android:showAsAction="always">
</item>
<item android:id="#+id/mnu_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always">
</item>
<item android:id="#+id/mnu_settings"
android:title="Settings"
android:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_manage">
</item>
<item android:id="#+id/mnu_upgrade"
android:title="Upgrade"
android:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_upload">
</item>
<item android:id="#+id/mnu_logout"
android:title="Log out"
android:showAsAction="ifRoom"
android:icon="#android:drawable/ic_menu_close_clear_cancel">
</item>
</menu>
Thanks for any help you can provide.
The findViewById can't find a MenuItem. You can use the onPrepareOptionsMenu in your activity. It's called when user tries to open the menu. You can try this:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
MenuItem item = menu.getItem(3);
// the number is the position of your "upgrade" item in the menu, starting from 0
// then check for the pro version
if(common.checkForProVersion()) {
item.setVisible(false);
} else {
item.setVisible(true);
}
}
Related
Basically I am inflating a menu
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
globalMenu = menu;
getSupportMenuInflater().inflate(R.layout.menu_refresh, menu);
return super.onPrepareOptionsMenu(menu);
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_refresh"
android:showAsAction="never"
android:title="#string/refresh"/>
<item
android:id="#+id/menu_24hours"
android:showAsAction="never"
android:title="#string/twentyfour_hours"/>
<item
android:id="#+id/menu_1week"
android:showAsAction="never"
android:title="#string/one_week"/>
<item
android:id="#+id/menu_1month"
android:showAsAction="never"
android:title="#string/one_month"/>
<item
android:id="#+id/menu_3month"
android:showAsAction="never"
android:title="#string/three_month"/>
<item
android:id="#+id/menu_6month"
android:showAsAction="never"
android:title="#string/six_month"/>
</menu>
whats happening, is that none of them are showing in 3 dots format.In ice cream sandwich you have to click the button menu on hardware, in nexus it shows 3 dots..
i need everywhere to have it as 3 dots, no device uniqueness.
However if i do this in my menu...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh_default" />
</menu>
this shows up in actionbardsherlock on top, on all devices. no menu hardware key.
Try creating dummy actionButton which have 3 dots as the icon. This is what I do if I need 3 dots button to be shown on all devices (Especially Samsung devices which have hardware menu keys).
Note that this is actually a hack
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_dummy_overflow"
android:icon="#drawable/ic_action_ic_overflow"
android:showAsAction="always"
android:title="#string/more">
<menu>
<item
android:id="#+id/menu_open_browser"
android:icon="#drawable/ic_action_ic_browser"
android:showAsAction="never"
android:title="#string/opeinInBrowser">
</item>
</menu>
</item>
</menu>
This is something that's decided by the Android OS build that is running. The bad news is that it means you have little control over it. The good news is that it means the user will see the same behaviour on every app on their phone, so they will be expecting it anyway.
You could mimic the behaviour by manually adding an overflow item to your menu and putting your overflow items into a sub-menu of that item. The downside of this is that the number of items you show is fixed. You won't get the benefit of a tablet in landscape mode showing more items in the ActionBar
I have a Navigation drawer in my app. It has a list of items. I use a switch statement and replace a current Fragment with a completely new Fragment which has its own views and logic.
My problem is that each Fragment has its own menu item in the action bar. I don't know how to change the actionBar menu item according to the selected drawer list item. I have tried lots of tutorials and ended with no result. Some sample code would really help.
Thanks in advance.
I think this tutorial is exactly what you're looking for:
http://www.grokkingandroid.com/adding-action-items-from-within-fragments/
create menuitemlist.xml in your menu folder. You can define your menu and sub menu here. EX:
<item
android:id="#+id/filter"
android:showAsAction="ifRoom"
android:icon="#drawable/filter_data"
android:title="projectlist"
android:titleCondensed="FILTER">
<menu>
<item
android:id="#+id/search"
android:title="Search"/>
</menu>
</item>
<item
android:id="#+id/profilename"
android:showAsAction="ifRoom"
android:title="details"
android:titleCondensed="fullmode">
</item>
<item
android:id="#+id/login"
android:showAsAction="always"
android:title="here"
android:titleCondensed="about">
<menu>
<item
android:id="#+id/it1"
android:title="list1"/>
<item
android:id="#+id/it2"
android:title="list2"/>
<item
android:id="#+id/it3"
android:title="list3"/>
</menu>
</item>
<item
android:id="#+id/admin"
android:icon="#drawable/admin"
android:showAsAction="always"
android:title="welcome"
android:titleCondensed="welcome">
</item>
In activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menuitemlist, menu);
return true;
}
For More Check
http://www.grokkingandroid.com/adding-actionbarsherlock-to-your-project and enjoy with action bar .`
Why the icons didn't appear when i used the menu.xml below !!. Please help me.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="#+id/group1">
<item android:id="#+id/item1" android:title="Embelance" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher" >
<menu>
<item android:id="#+id/E_H1" android:title="To home" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/E_W1" android:title="To work" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/E_O1" android:title="To oppional" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
</menu>
</item>
<item android:id="#+id/item2" android:title="Fier" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher">
<menu>
<item android:id="#+id/F_H1" android:title="To Home" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/F_W1" android:title="To work" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
<item android:id="#+id/F_O1" android:title="To Optional" android:showAsAction="always|withText"
android:icon="#drawable/ic_launcher"/>
</menu>
</item>
</group>
and when used the next attribute it shows that there is error in the app. : the window
says that the project contains error(s),fix it before the its running or something like that.
android:icon="#android:drawable/ic_launcher"
and the code is:
#Override
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.main_context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// .......
switch(item.getItemId()){
case R.id.E_H1: // ...........
break;
case...case....case...... // other cases
return super.onContextItemSelected(item);
}
I'll try to answer this, but so far the question is not 100% clear to me. Based on the comments, it seems you wish to know how to combine icons with text in menu item.
For that you have provided the answer yourself in your question: the 3-rd item contains what you need. If you wish to group items in a menu item block, then you can use groups as you did, but specifically for your example, the group is point-less. Something like below will suffice (running on a Motorola Defy 2.3.6 and on Nexus 4):
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/context_setting_item1"
android:icon="#drawable/ic_launcher"
android:title="#string/action_settings">
</item>
</menu>
In your Activity you will have (suppose menu is in /res/menu/entry.xml):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.entry, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_setting_item1:
Toast.makeText(this, "context_setting_item1 selected", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Now, to go back to the menu item general discussion: these visual components have been replaced by ActionBar - it is more friendly, more straight-forward when it comes to expose the content/app-hierarchy and it is sexy! :)
To dive into development, you can check this Android developers blogs, "Goodbye to MenuItem". For more development details check the developer section. From programming perspective it's not that much of big deal as the action bar components are developed with menu items in xml. You just need to provide more XML attributes.
Ex.: Only the above xml changes to:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/context_setting_item1"
android:icon="#drawable/ic_launcher"
android:showAsAction="always|withText"
android:title="#string/action_settings">
</item>
</menu>
And the Activity code remains as it is.
Note that I have added only android:showAsAction="always|withText".
The fun part is that nothing changes on Motorola 2.3.6 (since ActionBar was added in API level 11), but on Nexus you'll notice the top action bar has the icon on top. Previously, it had a 3-dots and the user had to tap on them in order to view the action.
If you wish to have the action bar in api-level below 11, I strongly recommend using Sherlock ActionBar.
I have an ActionBar that should display the action buttons in a custom way. For this I created a custom view and attached it to the ActionBar.
One thing to mention is that I am using a menu.xml resoure file to load the options menu and display them on a smartphone, but do not display them on tablet, instead use a custom view. For this I market every menu item in the xml as: android:showAsAction="never"
Everything looks fine, except one little thing that still remains on the right of the ActionBar - the "More" button.
How can I remove it?
I tried this:
ActionBar bar = activity.getActionBar();
bar.removeAllTabs();
but the "more" button still remains there.
EDIT:
This is my menu.xml file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_username"
android:icon="#drawable/menu_username"
android:orderInCategory="0"
android:showAsAction="never"
android:title="#string/menu_username">
<menu>
<item
android:id="#+id/menu_logout"
android:title="#string/menu_logout"/>
</menu>
</item>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/menu_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/menu_search"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/menu_search"/>
</menu>
Please note I still want to inflate this menu on a smartphone, but don't want to use it on a tablet.
Setting showAsAction="never" will force an menu item into the overflow. Why not check in onCreateOptionsMenu(...) that the device is a tablet, and if it is just not inflate the menu? Something like this:
public boolean onCreateOptionsMenu(Menu menu) {
if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
//It's a tablet, don't inflate, only create the manual view
manualMenuCreation();
} else {
getMenuInflater().inflate(R.menu.menu, menu);
}
return true;
}
Don't forget smallestScreenWidthDp is only available in 3.2 or above, so you'll have to take that into consideration.
I have an action bar in my app with 3 items.
Only 2 can be displayed due to space issues, so I'd expect the first to be displayed and the rest to be displayed in the overflow.
However in practice only the first 2 items are shown and there is no overflow detectable.
Here is the relevant code:
list_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_insert"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_insert"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_call"
android:icon="#android:drawable/ic_menu_call"
android:title="#string/menu_call"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_agenda"
android:icon="#android:drawable/ic_menu_agenda"
android:title="#string/menu_agenda"
android:showAsAction="ifRoom|withText"/>
</menu>
Activity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
If you want to show the three dots, irrespective of device menu button! then you can call this method in your application class' onCreate method-
private void makeActionOverflowMenuShown() {
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
Output
res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Location Found -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<!-- More -->
<item
android:id="#+id/a_More"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="More">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Download the Action Bar Icon Set
I realize this is not an overflow menu, but it is similar.
Okay, this is simple but was hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
On devices with hardware menu buttons (Galaxy S3, stubborn as Samsung is...) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button.
When you say "overflow" menu, do you mean the three dots that show up in the end to indicate that there are more items.... or do you mean the split actionbar that shows up in the bottom for overflow items?
If you mean the split action bar, you should add this to your activity's manifest file
android:uiOptions="splitActionBarWhenNarrow"
By default, the three dots overflow menu should happen automatically, so it's hard to tell what the problem is from the information you provided above.
To alway show action overflow (three dot) on actionbarcompat:
in menu file, example:
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_about"
android:title="#string/action_about"
app:showAsAction="never"/>
</menu>
and in activity file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
It's worked fine for me.
Tested on Google Nexus S, Samsung S3.
It appears that on devices with a menu button the overflow menu does not show in the ActionBar. Also we have no way to know if a device has a hardware menu button before API level 14 (4.0+).
This solution adds a overflow button to a android.support.v7.app.ActionBar that opens the standard options menu on pre-Honeycomb (<3.0) only, otherwise leaving it to the system. In any case you can choose between always or ifRoom for all actions.
Get the overflow icon from the Action Bar Icon Pack.
Add a menu (I'omitting some non-relevant parts as titles, ordering etc.)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_overflow"
android:orderInCategory="100"
android:icon="#drawable/ic_action_overflow"
app:showAsAction="always" />
<item
android:id="#+id/action_first"
app:showAsAction="always" />
<item
android:id="#+id/action_second"
app:showAsAction="ifRoom" />
</menu>
Remove our overflow action on 3.0+
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
menu.removeItem(R.id.action_overflow);
}
return super.onPrepareOptionsMenu(menu);
}
Finally call Activity.openOptionsMenu() from the overflow action.
Er... no. You cannot not call openOptionsMenu() from a menu button... but if you are using AndroidAnnotations you're done easily:
#OptionsItem
void action_overflow() {
openOptionsMenuDeferred();
}
#UiThread
void openOptionsMenuDeferred() {
openOptionsMenu();
}
If not you should do something like this, I suppose
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == id.action_overflow) {
openOptionsMenuDeferred();
return true;
}
}
private Handler handler = new Handler(Looper.getMainLooper());
public void openOptionsMenuDeferred() {
handler.post(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
}
);
}
Try changing the theme of the app from
Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light
Put xmlns:your_app_name="http://schemas.android.com/apk/res-auto" inside menu tag
and instead android:showAsAction="always" use your_app_name:showAsAction="always"
for eg
<item
android:id="#+id/action_search"
android:icon="#drawable/icon_search"
android:title="#string/search"
your_app_name:showAsAction="ifRoom"/>