I've been trying to follow the official Android development tutorial for a couple of days but I've noticed there's a few discrepancies between the two available IDEs, Eclipse with the ADT plugin and Android Studio. I heard nice things about the latter so that's the one I'm using, but there seems to be differences in the way the files are laid out when you first create a new project. I've mostly been able to solve stuff here and there so it works just like on Eclipse (the IDE used on the tutorial) but I can't figure out why my Action Buttons aren't showing up in the ActionBar. Here's the tutorial for reference.
So, this is what my res/menu/main.xml file looks like:
<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="com.example.myapplicationn.app.MainActivity" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
All I did was replace action_settings and copy action_search directly from the tutorial's sample code. There's a subtle (maybe not that subtle) difference here between AS and the tutorial: the tutorial's file is supposed to be res/menu/main_activity_actions.xml but its AS equivalent seems to be the default file res/menu/main.xml, so I went with that. Also, the tutorial sample only has the first attribute (xmlns:android="http://schemas.android.com/apk/res/android"), but the file created by AS had those additional ones so I left them untouched. Then, I modified the onCreateOptionsMenu method with this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Once again, the original code used R.menu.main_activity_actions, so I changed it to R.menu.main. The project compiles nicely and MainActivity shows up in my phone after a few seconds, but action_search is still hidden under the action overflow (isn't that what it's called?) button. Am I doing something wrong? Any help is appreciated. English isn't my first language and my writing style is kind of lousy, so sorry for that.
I had the same issue recently.
The showAsAction property is in the app namespace. It is explained in the tutorial that it is used when using the compatibility library. But if you are targeting an API level high enough that shouldn't be needed.
I was able to use the android namespace and it works fine for me. So change app:showAsAction to android: showAsAction and it will show up where you want it.
try this menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom"
android:title="#string/action_search"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
Make sure the activity which is creating the menu inherits from ActionBarActivity, which is the base activity class when using the support library.
Otherwise, whatever you set in app:showAsAction will not work.
I've scratched my head wondering why this didn't work for me in the past, dunno why they don't mention that in that tutorial you linked.
Hopefully this is your issue.
Bit late to the party. For those who still are having problem with this,
Try Removing this line
xmlns:app="http://schemas.android.com/apk/res-auto"
and change
app:showAsAction="always"
to
android:showAsAction="always"
IMPORTANT:
Make sure that you PNG Image is not larger than 27x27
I had the exact same problem while using Android Studio and following the official tutorial. Here is what worked for me, no idea why though.
1) I used the ic_launcher instead of the ic_action_search just to check if the search icon file itself had some issues.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_launcher"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
Now I could see that the launcher icon was visible.
2) Then I downloaded again the icons from the "Action Bar Icon pack" and changed the android:icon back to ic_action_search.
And now the search button is visible.
Like I said not sure why this worked.
Me too, have been following the tutorials, the link you have poosted. Same thing, the buttons just not show in the ToolBar, tried different solutions. Finally, one from Udacity is working.
Solution:
on your MainActivity, add the follwing code:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu,menu);
return true;
}
The rest is the same as the code in the official link, here, https://developer.android.com/training/appbar/actions.html#add-actions
Related
I am trying to add an item in a bottom menu navigation bar according to a var got from an API.
I am able to delete an item from this navigation bar, like this :
if (restaurant.acceptsBookings == false) {
bottom_navigation_view.menu.removeItem(R.id.bottom_menu_book)
}
The problem is, when I am launching my app, we can see the icon during like, half of a second, then it disappears.
This is not that bad, but I was hoping there is a better and neater way to do this ; for example by adding the elements in an empty navigation bar, instead of removing them.
Here is my navigation bar xml code :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/bottom_menu_home"
android:enabled="true"
android:title="#string/bottom_menu_home"
android:icon="#drawable/ic_home"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_menu"
android:enabled="true"
android:icon="#drawable/ic_menu"
android:title="#string/bottom_menu_menu"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_profile"
android:enabled="true"
android:title="#string/bottom_menu_profile"
android:icon="#drawable/ic_user"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_book"
android:enabled="true"
android:icon="#android:drawable/ic_menu_my_calendar"
android:title="#string/bottom_menu_bookings"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_fidelity"
android:enabled="true"
android:icon="#drawable/giftgrey2"
android:title="#string/bottom_menu_fidelity"
app:showAsAction="ifRoom" />
</menu>
Do someone have a solution for this ?
Thanks in advance.
First, thanks everyone for helping me.
I tried both methods and they work perfectly : Hide the navigation bar with
bottomNavigationMenu?.visibility = View.GONE
just after bottomNavigationMenu declaration, and then set
bottomNavigationMenu?.visibility = View.VISIBLE
just after API response.
The method which create dynamically an item works too, here is how
bottomNavigationMenu?.menu?.add(Menu.NONE, 1, Menu.NONE, "TEST")?.setIcon(R.drawable.ic_home)
Here is what the man tell about the add fun (from https://developer.android.com/reference/android/view/Menu.html ; ctrl + f "add" 43) :
groupId int: The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId int: Unique item ID. Use NONE if you do not need a unique ID.
order int: The order for the item. Use NONE if you do not care about the
order. See getOrder().
title CharSequence: The text to display for the item.
Thanks everyone for helping !
This sounds like a race condition.
What you can do is block the drawing of the navigation menu or the entire screen until after you get a response from the API. This just means your app will take half a second longer to launch.
Instead of using a static menu.xml file, what you can do is dynamically add menus that suits your condition.
Perhaps, this link may help: Inflate Bottom Navigation View menu programmatically
I am trying to show icons with overflow menu with below codes
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu,menu);
if(menu instanceof MenuBuilder){
MenuBuilder menuBuilder = (MenuBuilder) menu;
menuBuilder.setOptionalIconsVisible(true);
}
It gives me this error
menuBuilder.setOptionalIconsVisible can only be called from within the same library group
on line
menuBuilder.setOptionalIconsVisible(true);
I know I can suppress it for ignore but I want know why its coming and there any another way to fix it ?
Thanks
Edit
Ok, after investigating more on the subject this seems to be a bug as stated in comment and answers to this question and should be safe to suppress it. It may be fixed in one of the next releases of support libraries.
OLD ANSWER
Why not make menu items visible in XML? Use attribute android:showAsAction. There are several values available: ifroom | always | collapseActionView | never | withText - read more.
For instance if you want to always show first item and show second item if there is room for it:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/item_id1"
android:icon="#drawable/ic_icon1"
app:iconTint="#color/white"
app:showAsAction="always"
android:title="First item"/>
<item android:id="#+id/item_id2"
android:icon="#drawable/ic_icon2"
app:iconTint="#color/white"
app:showAsAction="ifRoom"
android:title="Second item"/>
</menu>
Note to all: This may be a duplicate question but I could not find a question with an answer that actually helped me out so I was forced to make this.
I am folowing the tutorial on how to make an Android application and I am stuck on this section: https://developer.android.com/training/basics/actionbar/adding-buttons.html
I am stuck on "Respond to Action Buttons" because action_search cannot be resolved or is not a field in this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openSearch(){
}
public void openSettings(){
}
This line
case R.id.action_search:
This is hindering the progress of my learning and I would like to know how to fix it. To me it just seems like poor tutorial teaching on Android's part but it could be my fault too. Thanks!
EDIT: My XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Just had this exact problem. The tutorial fails to mention these two steps.
If you haven't yet, do the following:
Add an image named ic_action_search.png to the 4 different res/drawable folders
Create a string named action_search in strings.xml
<string name="action_search">Search</string>
The main_activity_actions.xml fails to find those two things and doesn't add action_search to the R.java file, but doesn't give a useful error to point you in that direction.
Good luck!
EDIT: Also make sure you're using the app:showAsAction. As explained here.
What IDE you are using, if it's NetBeans, remove the line contains R.id.action_search and build your project, then add this line, if u are using Android Studion, i think it needs to work, but dont import R from android, import it from your project main package, I think the probleme is in the package where u defined your activity
The above answer (dmarsi) is 100% correct.
The Android Developers tutorial mentions the following:
http://developer.android.com/training/basics/actionbar/adding-buttons.html
The icon attribute requires a resource ID for an image. The name that follows #drawable/ must be the name of a bitmap image you've saved in your project's res/drawable/ directory. For example, "#drawable/ic_action_search" refers to ic_action_search.png. Likewise, the title attribute uses a string resource that's defined by an XML file in your project's res/values/ directory, as discussed in Building a Simple User Interface.
I struggled with the same problem until I created the string and added a random .png image for ic_action_search.png
Good luck with the rest of the tutorial!
I am following the tutorial on the developer website to implement an action menu bar, and I cant seem to get the icon to be drawn. http://developer.android.com/training/basics/actionbar/adding-buttons.html
Here is what I have so far...
res/menu/main_activity_actions
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Then I added that menu to the main activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
I am pretty sure the icon can be found because I added it in all res/drawable-* folders
Then when I run the virtual device this is what I get
As you can see, the 2 menu items are there, but search has no icon like it does in the tutorial. Heres what it's supposedly supposed to look like
I have been going through the tutorials as well, and ran into the same difficulty. Here is what I found.
I spent a few hours trying the various technical solutions given for the problem. None of them made sense given my context. I tried a few things that seemed to suggest themselves to no avail. Then I found another possible solution, and although quite a bit simpler, perhaps harder to debug.
Make sure that the icons you provide are from the action bar icons in the path: holo_dark -> 01_core_search.
The holo_light icons were the same color as the background of my device and so appeared as if not present. Press on the screen in that area, and the icon does appear because the background color changes with selection of the menu item.
Hope this helps.
I tried to create a menu with search and refresh button, but since refresh button will only be available at several activities, I want to set the visibility to false, and then add this:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
MenuItem item = menu.findItem(R.id.action_refresh);
item.setVisible(true);
return true;
}
to the activity java
Using the below code, the menu is displayed properly, with refresh on the left and search on the right
<item
android:id="#+id/action_search"
android:title="Search"
android:menuCategory="system"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="#drawable/ic_action_search"/>
<item
android:id="#+id/action_refresh"
android:title="Refresh"
android:menuCategory="system"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:visible="false"/>
But by reversing the item order,
<item
android:id="#+id/action_refresh"
android:title="Refresh"
android:menuCategory="system"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#drawable/ic_action_refresh"
android:visible="false"/>
<item
android:id="#+id/action_search"
android:title="Search"
android:menuCategory="system"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="#drawable/ic_action_search"/>
the refresh button/item will not appear.
While I have found the workaround and could proceed, this still puzzles me very much, and I haven't got any good explanation.
Why would that happen?
Are you using Eclipse?
I have seen a bug in Eclipse where simply changing the order of elements in an XML resource file would not recompile the file causing all kinds of weird things to happen.
I suggest you do a "Project > Clean" to make sure the resource identifiers get recreated from scratch.