I'm busy with changing my existing app to material design, but i'm having trouble with the menu items inside the ActionBar.
First of all in some activities i'm using a searchview, But i have read that you need to use a custom prefix inside the searchview menu xml. So i did:
<?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/action_search"
android:icon="#drawable/ic_action_search_light"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
But everytime I'm getting the error:
"Should use android:showAsAction when not using the appcompat library"
if i change app:showAsAction="ifRoom|collapseAction" to android:showAsAction="ifRoom|collapseActionView"
I'm not getting a searchview that take the whole lenght of the actionbar, just some strange square in the right corner where the items are placed. Here is my code of the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchview, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView search = (SearchView) MenuItemCompat.getActionView(searchItem);
if(search != null)
{
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
try {
new LoadResultsTask().execute(URLEncoder.encode(query, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Toast.makeText(IndexerOverviewActivity.this, "Could not parse Query", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return true;
}
#Override
public boolean onQueryTextChange(String search) {
return true;
}
});
}
search.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
I noticed aswell that all my actionbar menu items that has android:showsAsAction="always" are shown as android:showsAsAction="never" when i start my application on my device. So my first guess is it has everything to do with the prefix to use in the menu xml files.
Pffff, after just a single "clean" it works perfectly now...
It took me freaking 2 hours to fix this.
I am having trouble getting the menu icon to show up on the action bar. I tried setting
android:showAsAction="always"
but even that doesn't work. In an earlier project I just used
android:showAsAction="ifRoom|withText"
and that worked. The only thing different with this project is that I am building with gradle instead of ANT. Could that make a difference?
Here is my menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/new_transaction"
android:icon="#drawable/ic_menu_add"
android:title="#string/new_transaction"
android:titleCondensed="#string/new_transaction_condensed"
android:showAsAction="always" />
</menu>
I copied each version of ic_menu_add.png from the android library to each respective drawable folder.
And here is my onCreateOptionsMenu in my ActionBarActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.account_pager, menu);
return true;
}
No matter what I try, the icon does not show up. I've tried changing the max and min API level, but to no avail. In order to click the menu item, I have to go to the overflow menu and then click the dropdown menu item there, which is definitely less than ideal. How can I make the icon show?
My min API level is 11, and my max is 20. I am developing on an HTC One running Android 4.1.2, if any of that matters.
Thanks in advance.
Try this:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (NoSuchMethodException e) {
Log.e("", "onMenuOpened", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
I saw many posts in SO but couldn't figure out why Overflow icon is not displayed. I am testing on real device Android 4.4 . I get the items displayed on bottom of the screen when options button is pressed . But I need it to appear as 3 dot menu on Top Right like in Gmail App. Pl help. Need something like attached pic of Tubemate app when options button clicked.
Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--xmlns:com.app.MainActivity="http://schemas.android.com/apk/res-auto"-->
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/icon_search"
android:orderInCategory="1"
android:title="search"
android:showAsAction="never"/>
<item
android:id="#+id/action_end"
android:icon="#drawable/end"
android:menuCategory="container"
android:orderInCategory="2"
android:title="#string/end"
android:showAsAction="never"/>
</menu>
MainActivity:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{ menu.clear();
this.menu = menu;
getMenuInflater().inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_end:
System.exit(0);
break;
case R.id.action_search:
break;
default:
break;
}
return false;
}
Overflow menu will only appear on devices that don't have physical menu button. In devices with physical menu button it will appear from bottom.
To force overflow menu, put this code inside onCreate:
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) {
e.printStackTrace();
}
}
Source
I want to add menu handler to my project. I read http://developer.android.com/guide/topics/ui/menus.html too, its very simple but the icon is not shown. I am very confused. I even added a menu item programmatically.
My code is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Quit").setIcon(R.drawable.ic_launcher);
getMenuInflater().inflate(R.layout.menu, menu);
return true;
}
and in xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_bookmark"
android:icon="#drawable/update"
android:title="#string/Update" />
</menu>
After Long try i found below solution which might help others to save there time. Basically, the solution provided by "lbarbosa", i like to thanks to him sincerely.
Tried this based on the previous answers and it works fine, at least with more recent versions of the support library (25.1):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}
return true;
}
If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google.
You can read more about it in this on Android developers blog.
No matter what design choices where made by the system, you can circumvent this with the solution provided in the top upvoted answer to this question
Code below for completeness. Tested working on android.support.v7.app.ActionBarActivity
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
Old question but hope it will help someone.
use the following code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_item_share"
android:title="Share"
app:showAsAction="always"
android:icon="#drawable/share" /></menu>
note i used app:showAsAction instead of android:showAsAction
You can add to your XML file the attribute android:showAsAction="always" inside your item element. It then will show the relevant menu option as an icon inside your action bar.
Note that it will be instead of the text in the menu.
For further read, look here under android:showAsAction.
/* menu 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/one" android:title="one"
android:icon="#mipmap/ic_launcher" app:showAsAction="always" />
<item android:id="#+id/two" android:title="two"
android:icon="#mipmap/ic_launcher" app:showAsAction="always" />
<item android:id="#+id/three" android:title="three"
android:icon="#mipmap/ic_launcher" />
</menu>
/* Java code */
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu,menu);
if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}
return true;
}
Forget all those, do this step.
add app:showsAsAction="always" to your item. If you use android:showsAsAction="always" you won't get the solution.
Try with adding app attribute to your item.
I put explicit icons in onCreateOptionsMenu method using below code
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.print) {
item.setIcon(getDrawable(R.drawable.print));
}
}
Working with android.support.v7.app.AppCompatActivity, is making the input for the icons a very difficult task. You need to implement onMenuOpen, super method from AppComactActivity class. After this check, if the menu is not null. If the menu is not null pass Method class like this and setOptionalIconsVisible to true with boolean.
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e("MAIN", "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
Just add this attr inside item tag in your menu.xml
app:showAsAction="always
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "androidDemo").setIcon(R.drawable.ic_launcher);
getMenuInflater().inflate(R.layout.menu, menu);
return true;
}
and in xml:
<item android:id="#+id/menuUpdate"
android:icon="#drawable/update_icon"
android:title="#string/Update"
android:showAsAction="always"/>
I have an ActionBar using ActionBar Sherlock where I need it to display overflow because I have more actions than room. But, it doesn't show the overflow icon. Here is my configuration:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/action_search"
android:title="#string/menu_search"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_library"
android:icon="#drawable/hardware_headphones"
android:title="#string/my_music"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_downloads"
android:icon="#drawable/av_download"
android:title="#string/downloads"
android:showAsAction="ifRoom|withText"/>
</menu>
And here is code to set it up:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.shopping_menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
searchMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(ShopActivity.this, SearchDialog.class));
return false;
}
});
MenuItem downloadMenuItem = menu.findItem(R.id.menu_downloads);
downloadMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity( new Intent(ShopActivity.this, DownloadQueueActivity.class) );
return false;
}
});
MenuItem myMusicItem = menu.findItem(R.id.menu_library);
myMusicItem.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(ShopActivity.this, MyMusicActivity.class));
return false;
}
});
return true;
}
I've looked over the demos in ActionBar Sherlock, but I can't tell what they do differently to get the overflow than what I'm doing. So what's happening here why its not showing?
If you have a physical menu key, the overflow indicator does not show. That is a behaviour by design. See here for more details on another question asked.
Hmm I think there are two issues here. First, as t0mm13b states, if the device has a physical menu key, the overflow indicator does NOT show. This is by design. Although in my experience, it doesn't apply to every device (unfortunately...)
The second issue is that, if you want to force an item to the overflow, you need to set the showAsAction to "never". Otherwise, the only elements that appear in the overflow are ones that simply "don't fit" in the action bar. And given that you have 3 items that you want to display with text.. you're pretty much guranteed to have at least one overflow item, and therefore the overflow icon (with the caveat of the first paragraph)
Try by changing the android:showAsAction tag to app:showAsAction according to the Android guide in the menu_main.xml file as shown below
Add this line if not present
menu xmlns:app="http://schemas.android.com/apk/res-auto"
<item android:id="#+id/action_search"
android:icon="#drawable/search_icon"
android:title="#string/action_search"
app:showAsAction="always"/> <!--change here android: to app:-->
<item android:id="#+id/action_location"
android:icon="#drawable/location_icon"
android:title="#string/action_locate"
app:showAsAction="always"/>
</menu>