How can I add a custom icon to my android menu the code below is what I currently have in my xml file.
<item android:id="#+id/item1" android:icon="#android:drawable/ic_menu_add" android:title="Blog"></item>
the icon is already within my drawable folder
you have to replace this in the xml:
android:icon="#android:drawable/ic_menu_add"
with
android:icon="#drawable/your_menu_icon_name"
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//Set icon for the menu button
Drawable icon = getResources().getDrawable(R.drawable.icon);
menu.getItem(0).setIcon(icon);
return true;
} //End onCreateOptionsMenu()
More reading: MenuItem
NavigationView navView = findViewById(R.id.navigationView);
navView.setItemIconTintList(null);
This works for me to set the original icon with the original colors.
Related
I am working with the navigation drawer and i want to display different icon on different fragment when user click on any item of listview on navigation drawer.
i have did a code in xml of activity which contain navigation drawer.
<item
android:id="#+id/action_settings"
android:icon="#drawable/icon_setting"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
When i click on any item on list the icon should change. i tried to get reference like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
mItem = menu.getItem(0);
return true;
}
But it gives null pointer exception.thank you in advance.
Try findItem:
mItem = menu.findItem(R.id.action_settings);
I recently switched from ActionBarSherlock to the Android Support Library ActionBar, and now I get a null on the action view of a spinner in the action bar.
Here's the code as suggested by the docs:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
MenuItem spinnerItem = menu.findItem(R.id.menuNavigateType);
View view = MenuItemCompat.getActionView(spinnerItem);// !! view is NULL !!
...
}
Here's R.menu.home_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto">
...
<item
android:id="#+id/menuNavigateType"
myapp:showAsAction="always"
myapp:actionViewClass="android.support.v7.widget.Spinner" />
...
</menu>
How do I get my action view?
Thanks.
Not sure, but try the following, instead of MenuItemCompat.getActionView(), use:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
MenuItem spinnerItem = menu.findItem(R.id.menuNavigateType);
View view = spinnerItem.getActionView();
// ...
if(view instanceof Spinner) {
final Spinner spinner = (Spinner) view;
// create your adapter
// ...
// set your adapter
spinner.setAdapter(adapter);
}
}
Also for your item, instead of android.support.v7.widget.Spinner:
<item
android:id="#+id/menuNavigateType"
myapp:showAsAction="always"
myapp:actionViewClass="android.widget.Spinner" >
I saw this solution here: Android ActionBar (ActionBarCompat) Spinner Dropdown list ?.
Let me know if it helps you.
The error is due most probably to the lack of including the support.v7 library.
Just dont use the myapp. Instead:
<item android:id="#+id/menuNavigateType"
android:showAsAction="always"
android:actionViewClass="android.widget.Spinner"/>
I am using ActionBarSharlock in my Application for Action Bar. In actionBar I take a button and If I click the button I see a menu with three items. Here is the code-
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu1 = menu.addSubMenu("Action Item");
subMenu1.add("Sample");
subMenu1.add("Menu");
subMenu1.add("Items");
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.sub_menu_icon);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
But I don't know how to make this three submenu1 item clickable and move one activity to another. What kind of function I need to write. Let me know It will be great help
First: why don't you add your menu items in the XML menu file in res/menu/your_file.xml:
<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"/>
<item android:id="#+id/action_compose"
android:icon="#drawable/ic_action_compose"
android:title="#string/action_compose" />
then inflate the menu in your activity:
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);
}
and then override the onOptionsItemSelected method to handle the menu item clicks.
Also Google developed their own library for ActionBar on API level < 11 so you should try using that one as well instead of Sherlock.
Hope this helps. Best of luck.
I have started my android application with the standard holo theme. Then I added the tabs functionality and now I would like to add a small settings button in the action bar.
How can I do that. I already set this method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
Try this:
menu.add("some title").setIcon(R.drawable.[your image]).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
I'm developing an app which supports APIS 9 to 17.
It also has a Navigation Drawer and for the action bar I use ActionBarSherlock. The problem comes when I press the menu hardware button (API <= 10). The actions displayed in the actionbar are duplicated. How to fix this issue?
Here's my code to inflate the menu
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_content_action_menu, menu);
return true;
}
And my menu_content_action_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/send_order_action"
android:icon="#drawable/ic_navigation_accept"
android:showAsAction="ifRoom|withText"
android:title="#string/send_order_button_text">
</item>
</menu>
Hope you can help me.
Use your code in the onCreateOptionsMenu() implementation instead.
public boolean onCreateOptionsMenu(final Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu_content_action_menu, menu);
return true;
}