I'm trying to create some MenuItems, this way:
main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_add"
android:icon="#drawable/ic_action_add"
android:orderInCategory="1"
android:title="#string/action_add"
android:showAsAction="always" />
<item
android:id="#+id/item_settings"
android:orderInCategory="2"
android:title="#string/action_settings"
android:showAsAction="never" />
<item
android:id="#+id/item_about"
android:orderInCategory="3"
android:title="#string/action_about"
android:showAsAction="never" />
</menu>
MainActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
I cannot get the render of those items on the top menu bar. How do I to make this work, please ?
Thanks in advance!
I figured it out. With android support library v7 appcompat, you have to use a specific namespace to use the attribute showAsAction correctly. Here is what you need to do on your xml file:
Add your custom namespace declaration to the file like this:
xmlns:app="http://schemas.android.com/apk/res-auto"
Then instead of using android:showAsAction, use app:showAsAction.
This will allow the menu items to show.
Here's the result file code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/item_add"
android:icon="#drawable/ic_action_add"
android:orderInCategory="1"
android:title="#string/action_add"
app:showAsAction="always" />
<item
android:id="#+id/item_settings"
android:orderInCategory="2"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/item_about"
android:orderInCategory="3"
android:title="#string/action_about"
app:showAsAction="never" />
</menu>
Hope I have helped you.
Related
I am initializing all of the icons with images but none of them are visible?
< ?xml version="1.0" encoding="utf-8"?>
< menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/nav_item1"
android:icon="#drawable/insta"
app:showAsAction="ifRoom|withText"
android:title="#string/nav1"/>
<item
android:id="#+id/nav_item2"
android:icon="#drawable/artistsample"
app:showAsAction="ifRoom|withText"
android:title="#string/nav2"/>
<item
android:id="#+id/nav_item3"
android:icon="#drawable/artistsample"
app:showAsAction="ifRoom|withText"
android:title="#string/nav3"/>
<item
android:id="#+id/nav_item4"
android:icon="#drawable/artistsample"
app:showAsAction="ifRoom|withText"
android:title="#string/nav4"/>
</menu>
Can you please use the bellow code and see what happens.
<item
android:id="#+id/nav_item2"
android:icon="#drawable/artistsample"
android:title="#string/nav2"/>
and enable app:labelVisibilityMode="labeled" in BottomNavigationView
I have problem to set a menu.
I want to open a menu from an ImageView, because I want to use a specific image on the tool bar.
I can't found any tutorials
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater =getMenuInflater();
menuInflater.inflate(R.menu.days, menu);
return true;
}
Thanks in advance!
Do you want to create menu something like this?
Menu xml code: menu/days.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/item_love"
android:title="Love"
android:icon="#drawable/baseline_favorite_border_white_48"
app:showAsAction="always">
</item>
<item
android:id="#+id/item_more"
android:title="More"
android:icon="#drawable/baseline_add_white_48"
app:showAsAction="always">
<menu>
<item android:id="#+id/sub_item_1"
android:title="Sub Item 1"
app:showAsAction="withText"
/>
<item android:id="#+id/sub_item_2"
android:title="Sub Item 2"
app:showAsAction="withText"
/>
<item android:id="#+id/sub_item_3"
android:title="Sub Item 3"
app:showAsAction="withText"
/>
</menu>
</item>
</menu>
Full source code: https://github.com/hiepxuan2008/basic-menu-android
Hope it will help you. Thanks!
I have problem with actions on Toolbar in my Android app.
I have a group with some actions with app:showAsAction="ifRoom" attribute. Everything works fine if there are fewer than 4 items with ifRoom. But when I add fourth item, then all the items disappear from toolbar.
It works fine:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_paint"
android:orderInCategory="1"
app:showAsAction="ifRoom">
<item android:id="#+id/action_layers"
android:icon="#drawable/ic_layers_white_24dp"
android:title="#string/action_layers"
android:orderInCategory="3"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_tool"
android:icon="#drawable/ic_properties_white_24dp"
android:title="#string/action_tool"
android:orderInCategory="4"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_action1"
android:icon="#drawable/ic_action1_white_24dp"
android:title="#string/action_1"
android:orderInCategory="7"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:title="#string/action_settings"
android:orderInCategory="7"
app:showAsAction="never" />
</group>
</menu>
Unexpected result:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_paint"
android:orderInCategory="1"
app:showAsAction="ifRoom">
<item android:id="#+id/action_layers"
android:icon="#drawable/ic_layers_white_24dp"
android:title="#string/action_layers"
android:orderInCategory="3"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_tool"
android:icon="#drawable/ic_properties_white_24dp"
android:title="#string/action_tool"
android:orderInCategory="4"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_action1"
android:icon="#drawable/ic_action1_white_24dp"
android:title="#string/action_1"
android:orderInCategory="7"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_action2"
android:icon="#drawable/ic_action2_white_24dp"
android:title="#string/action_2"
android:orderInCategory="8"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:title="#string/action_settings"
android:orderInCategory="7"
app:showAsAction="never" />
</group>
</menu>
What I would like to see is: three actions on toolbar and the fourth in the overflow menu.
You are using the <group> element to treat the items as a group. If you do not want to treat the items as a group (e.g., you want them to appear in the action bar individually based upon available space), get rid of the <group> element.
Hello I want to change my app menu option (black text on white Background instead of white text on black Backgroud )
Here is my menu xlm file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.androidsrc.tower.Liste1">
<item android:id="#+id/action_settings"
android:title="#string/action_settings" />
<item android:id="#+id/aide"
android:title="#string/aide" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Change your menu.xml to this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
<item android:id="#+id/action_settings"
android:title="#string/action_settings" />
<item android:id="#+id/aide"
android:title="#string/aide" />
</menu>
In your styles.xml file add the following to your base app theme
<item name="android:popupMenuStyle">#style/CMOptionsMenu</item>
and then add the following styleto the file
<style name="CMOptionsMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">#FFFFFF</item>
<item name="android:textColor">#000000</item>
</style>
In the AndroidManifest.xml remember to add the following to your application
android:theme="#style/AppBaseTheme"
I am trying to an a camera icon to my action bar on android studio, but I can not see any icon. I can see is the "camera" text in my overflow. Below is my me.xml file which is in my menu folder, and also is below is my onCreateOptionsMenu() method
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="#+id/action_logout"
android:title="#string/menu_logout_lable"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="#+id/action_edit_friends"
android:title="#string/menu_edit_friends_label" />
<item
android:id="#+id/action_camera"
android:title="#string/menu_camera_label"
android:icon="#drawable/ic_action_camera"
android:showAsAction="always" />
</menu>
i found out why, in my xml file, on the item for the camera
<item
android:id="#+id/action_camera"
android:title="#string/menu_camera_label"
android:icon="#drawable/ic_action_camera"
android:showAsAction="always"
/>
, i changed
android:showAsAction="always"
to
app:showAsAction="always"
the difference been changing android: to app: .
Thanks