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)
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.
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.
This is my xml file for the ActionBar menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/fav_button"
android:title="Favourite"
android:icon="#drawable/unstar"
android:showAsAction="always|withText" />
</menu>
In my onCreate function, after calling setContentView. I do favButton = (MenuItem) this.findViewById(R.id.fav_button); But this returns null.
But returns the proper object on the onOptionsItemSelected function.
I'm using ActionBarSherlock, if that would make a difference.
I have tried various options suggested by other findViewById returns null questions, but they haven't solved my issue.
Instead of
favButton = (MenuItem) this.findViewById(R.id.fav_button);
in onCreateOptionsMenu after getMenuInflater().inflate(R.menu.activity_main, menu);
favButton = menu.findItem(R.id.fav_button);
Use menu.findItem() to get the menu. But this needs to be done after the menu is inflated.
Also, to answer your q in comment, you could use onPrepareOptionsMenu to set the state of your menu. If this menu is a one time updating, you could use onCreateOptionsMenu too, which is called only once.
but if someone really needs View and not MenuItem (for different manipulations, for example to start animation) you can still get it the next way:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu_xml_file, menu);
...
new Handler().post(new Runnable() {
#Override
public void run() {
view = findViewById(R.id.menu_refresh_button);
// view.startAnimation(animation);
}
});
return true;
}
Try
final Toolbar toolbar = findViewById(R.id.toolbar);
Menu menu=toolbar.getMenu();
MenuItem item = menu.findItem(R.id.'name id item');
for me works.
I don't know why, but in my case, I use findViewById(R.id.menu_id) return null. But I find that I use findViewById(item.getItemId) in onOptionsItemSelected, it return the view what we want.