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);
Related
I have toolbar with a view pager and two tabs which are fragments in my application. When the user swipes between the two, a different menu item displays in the toolbar. I am able to do this with no problem. However, I want to get a fade animation between the two menu items when they are switching.
How I achieve the switch between the menu items follows.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
this.menu = menu; //get reference to menu
return super.onCreateOptionsMenu(menu);
}
I get a reference to the menu onCreateOptionsMenu() method.
Then using an update method I pass a value when the tabs are switched.
private void updateMenuTitles(int value)
{
if(value == 0)
{
MenuItem sort = menu.findItem(R.id.sort_menu);
sort.setVisible(true);
MenuItem dateRange = menu.findItem(R.id.date_range);
dateRange.setVisible(false);
}
else if(value == 1)
{
MenuItem dateRange = menu.findItem(R.id.date_range);
dateRange.setVisible(true);
MenuItem sort = menu.findItem(R.id.sort_menu);
sort.setVisible(false);
}
}
XML for the menu.
<item
android:id="#+id/date_range"
android:title="#string/sort_options"
android:icon="#drawable/ic_date_range_white_24dp"
android:orderInCategory="2"
android:visible="false"
app:showAsAction="ifRoom">
</item>
<item
android:id="#+id/sort_menu"
android:title="#string/sort_options"
android:icon="#drawable/ic_sort_white_24dp"
android:orderInCategory="2"
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/sortName"
android:title="#string/sort_name"
app:showAsAction="never"
/>
<item
android:id="#+id/sortSize"
android:title="#string/sort_size"
app:showAsAction="never"
/>
<item
android:id="#+id/sortDate"
android:title="#string/sort_date"
app:showAsAction="never"
/>
<item
android:id="#+id/sortLastUsed"
android:title="#string/sort_usage"
app:showAsAction="never"
/>
</menu>
</item>
The above works fine. The problem arises when I want to add an animation between
sort menu item and the dateRange menu item, when switching tabs.
I have search and tried to use the following stackoverflow posts with no
success.
Changing view on Menu Item in Action Bar
getActionView() of my MenuItem return null
Animated Icon for ActionItem
Add ActionBar Items with a fade animation
Using the above information I tried setting an android:actionViewClass which
returns null when I try to fetch it. Then I tried app:actionViewClass which makes my menu item disappear.
I tried to setActionView in the code instead of XML, then try to animate
that with no luck.
I also tried to use android(app):actionLayout. Make an ImageView layout, but that makes my menu item unclickable and I was still unable to animate the fade.
action bar (Toolbar) on android has 3 options menu. Its working fine. But the problem is when ever i am uninstalling and reinstalling this duplication of the menu item is showing. Also the Title is also remains same, not changing. after sometimes, say if we close the app and restarts its working fine. I am totally confused and struck.
Here is the Code, MainActivity (OnCreateOptionsMenu):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
MenuItem item = menu.findItem(R.id.action_wishlist);
wishlistCount = (RelativeLayout) MenuItemCompat.getActionView(item);
wishlistCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
setWishlistCount();
MenuItem cartitem = menu.findItem(R.id.action_cart);
cartCount = (RelativeLayout) MenuItemCompat.getActionView(cartitem);
cartCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
restoreToolbar();
return super.onCreateOptionsMenu(menu);
}
Here is my menu_main.xml:
<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">
<item
android:id="#+id/action_search"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<item
android:id="#+id/action_wishlist"
android:orderInCategory="300"
android:title="#string/wishlist"
app:actionLayout="#layout/actionbar_badge_layout"
android:icon="#drawable/ic_wishlist_dark"
app:showAsAction="always"></item>
<item
android:id="#+id/action_cart"
android:orderInCategory="300"
android:title="#string/cart"
app:actionLayout="#layout/actionbar_badge_cart_layout"
android:icon="#drawable/ic_cart_dark"
app:showAsAction="always"></item>
</menu>
Here is the Screenshot:
1: first time:
enter image description here
2: normal:
enter image description here
Note: The problem occurs when the application opens with opening navigation bar, else its working fine. On createoptionsmenu is called at all times, but still its not working. Anyone can help on this?
More Precisely this should do the trick:
menu.clear() and return super.onCreateOptionsMenu(menu); does the trick! :)
#Override
public boolean onPrepareOptionsMenu(final Menu menu)
{
menu.clear();
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
CHEERS! :)
I have cleared it by myself, by changing the code from "onCreateOptionsMenu" to "OnPrepareOptionsMenu". Also it is recommended to call Super class before clearing or inflating the menu.
Thanks for all.
I want to animate a ic_action_refresh in the ActionBar. I'm using AppCompat, and so far I have done this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main,menu);
MenuItem item = menu.findItem(R.id.action_refresh);
if(item != null)
refreshView = MenuItemCompat.getActionView(item);
super.onCreateOptionsMenu(menu, inflater);
}
The results are: item is ok, but refreshView is null.
Any idea of what can I do, or what am I missing?
Edit
<item android:id="#+id/action_refresh"
android:title="#string/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
To display an animation (I'll assume a spinning ProgressBar) in a menu item use the following:
item.setActionView(new ProgressBar(YOUR APP CONTEXT));
and when you want to get rid of the progress bar simply do item.setActionView(null);
You have to use ..
item.getActionView() // to get the current action view set for the item
but make sure you have already set action view for this item using..
item.setActionView(view)
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.
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.