I know how to show icons in the overflow menu of the ActionBar. This is what I use,
#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);
}
But when I long-press on any item of my ListView the CAB starts. Now, when I open the overflow menu of the CAB, the menu doesn't have any icons. How can I do that?
Thanks in advance.
I waited for 2 days but didn't get any answer on this. So solved it myself.
The idea is quite simple here. You need to create your own overflow item in the and the create a nested menu to show both the icon and the text.
See the example code below,
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="#string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="#+id/cab_menu_select_all"
android:icon="#drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="#string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
The trick here is to create nested menus. You can add as many items as you want.
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've developed an application which contains a menu icon in my actionbar, I create the menu as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
and here's the code for the onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
startActivity(new Intent(this, MainActivity.class));
return true;
case R.id.menu_adv_search:
startActivity(new Intent(this, AdvSearchActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
using my smart phone the menu icon is there (using LG phone), but when I test it on my tablet (Galaxy Tab 8) the menu icon is gone, but the functionality is still there. pressing the menu soft button at the bottom, the popup appears, but the icon is missing from the top action bar. How to fix it? any ideas?
On devices with hardware menu buttons ( for example Galaxy series of samsung) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button.
please use following attribute for each of your menu item
android:showAsAction="always"
if you are using actionbarcompat pack , then instead of above attribute use following
app:showAsAction="always"
don't forget to add the following namespace aswell
xmlns:app="http://schemas.android.com/apk/res-auto"
As I found out, on devices with menu button hardware provided, the menu icon will be hidden and making the menu item showAsAction="always" will not bring the menu icon back it in my question I was intending to, so I've found the solution Here
and here's the code I've used from the above link to overcome this issue(if it can be called an issue):
private void getOverflowMenu() {
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();
}
}
you can just put this function in your activity or base activity and call it in your onCreate function and it will do all the magic for ya.
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 try to create OptionMenu which application's target is 15 but minSDK is 8. I Have one Menu folder and mymenu.xml in it. I wanna use the default icon menu and wanna make it support since sdk 8. How should it do that? The problem when i test is, the option menu icon show only on sdkVersion 8 but, not show on sdkVersion 15
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/my_menu" android:title="Send Mail" android:icon="#android:drawable/ic_menu_send"></item>
</menu>
and in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.my_menu:
//Do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The action overflow -- what you are thinking of as the options menu on API Level 11+ -- does not show icons.
#ohh: android:showAsAction="ifRoom", you can add it to your menu.xml