I want to have an Menu item in the ActionBar that visualizes a boolean value. If this boolean value is true, the icon should start animating a series of 2 images in a loop. I did the following:
I added an item in the menu.xml file:
<item
android:id='#+id/myAnimation'
android:icon='#drawable/pic1'
android:showAsAction='ifRoom'>
</item>
I created an animation-list:
<?xml version='1.0' encoding='utf-8'?>
<animation-list xmlns:android='http://schemas.android.com/apk/res/android'
android:oneshot='false' >
<item
android:drawable='drawable/pic1'
android:duration='100' />
<item
android:drawable='drawable/pic2'
android:duration='100' />
</animation-list>
In the method onCreateOptionsMenu I assign this menu item to a variable
myMenuItem = menu.findItem(R.id.myAnimation);
Now, I was hoping to add an animation which would get executed whenever I tell the menu item to animate. But I wasn't able to come even close.
is this even possible?
How can I do that?
Thanks! Any help is appreciated!
Edit:
The App should support API from 8
I did ((AnimationDrawable) item.getIcon()).start(); in onCreateOptionsMenu(Menu menu)
This did not work. Then I thought, maybe it should be done on the UI thread, and then this worked:
someView.postDelayed(new Runnable() {
#Override
public void run() {
((AnimationDrawable) item.getIcon()).start();
}
}, 1000);
I was looking for the same thing and just found your question unanswered, so here is the solution, from the Android dev website:
((AnimationDrawable)myMenuItem).start();
Related
EDIT: Problem solved after upgrading Build Tools Version from 23 to 27.
I have the following code snippet:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);
}
Recently I have got some errors on Crashlytics with line menu.findItem(...):
Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.View android.view.MenuItem.getActionView()' on a null object reference
at pl.application.ProductViewFragment.onCreateOptionsMenu(SourceFile:434)
at android.support.v4.app.Fragment.performCreateOptionsMenu(SourceFile:2186)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(SourceFile:2250)
at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(SourceFile:328)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(SourceFile:363)
The problem is not deterministic and shows only on Android Oreo (by Fabric - 100% of crashes were on Android 8.0, different devices). I have never had problems with this line before.
Were there any important changes in Android 8.0 able to cause NPE there? I've tried reproduce it on my Xiaomi Mi A1, but with no effects. Or maybe there is a workaround?
Thanks!
// Edit: added xml menu file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/breadcrumbsMenuItem"
android:icon="#drawable/ic_breadcrumbs"
android:title="#string/breadcrumbs"
res:showAsAction="always" />
<item
android:id="#+id/shareMenuItem"
android:icon="#drawable/ic_menu_share"
android:title="#string/share"
res:actionLayout="#layout/layout_share_button"
res:showAsAction="always" />
</menu>
Just try to add something like this, to see if it will be still null or not
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
menu.findItem(R.id.shareMenuItem).getActionView().setOnClickListener(onShareMenuItemClickedListener);
}
}, 2000);
If this fixes the problem, then the reason for him to be null is that is still hasn't been created and you area all rdy trying to access to it
I did have a problem when I was trying to change the color of an icon of my Menu acording to some info that it get's when the program starts, and that "work arround" fix the problem to me
I'm not sure where you get res:... but i think it should be app:...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/breadcrumbsMenuItem"
android:icon="#drawable/ic_breadcrumbs"
android:title="#string/breadcrumbs"
app:showAsAction="always" />
<item
android:id="#+id/shareMenuItem"
android:icon="#drawable/ic_menu_share"
android:title="#string/share"
app:actionLayout="#layout/layout_share_button"
app:showAsAction="always" />
</menu>
I use the new NavigationView in one of my recent projects. However I have a problem for the update data.
Previously, I used a ListView in my DrawerLayout and when I needed to change my data I called notifyDataSetChanged() method of my Adapter.
Currently NavigationView does not notifyDataSetChanged() method and when I want to update an item on my menu nothing is happening, for example:
Menu menuAccount = navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu();
menuAccount.findItem(R.id.drawer_item_login).setVisible(!isLoggedIn);
Do you have a solution ? Thanks you for your help.
UPDATE: This was fixed in v23.0.0 so you don't need to do anything by youself, just update your dependency. Got from this answer: https://stackoverflow.com/a/32181083/2489474
Old solution (just to see how stupid it could be):
But I've found working solution in this answer https://stackoverflow.com/a/30604299/2489474. It uses reflection to call updateMenuView(boolean) of NavigationView's presenter.
I've modified code from the answer for my purposes. Check also a method from the answer and choose which one is better for you.
//HACK
private void updateNavigationView() {
try {
Field presenterField = NavigationView.class.getDeclaredField("mPresenter");
presenterField.setAccessible(true);
((NavigationMenuPresenter) presenterField.get(navigationView_)).updateMenuView(false);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
P.S. it is interesting that updateMenuView ignores a value was given to it.
#Moinkhan Thanks you for your helper but doesn't work for me. Here my menu_drawer.xml
<group
android:checkableBehavior="single"
android:id="#+id/group1">
<item
android:id="#+id/drawer_item_publications_list"
android:icon="#drawable/ic_drawer_publications_24dp"
android:title="#string/drawer_menu_item_publications" />
</group>
<group android:id="#+id/drawer_group_account">
<item
android:title="#string/drawer_menu_sub_item_account"
android:id="#+id/drawer_item_account">
<menu>
<item
android:icon="#drawable/ic_drawer_login_24dp"
android:id="#+id/drawer_item_login"
android:title="#string/drawer_menu_item_login" />
<group
android:id="#+id/group_actions_user">
<item
android:icon="#drawable/ic_drawer_add_publication_24dp"
android:id="#+id/drawer_item_add_publication"
android:title="#string/drawer_menu_item_add_publication" />
<item
android:icon="#drawable/ic_drawer_my_publications_24dp"
android:id="#+id/drawer_item_my_publications"
android:title="#string/drawer_menu_item_my_publications" />
<item
android:icon="#drawable/ic_drawer_edit_profil_24dp"
android:id="#+id/drawer_item_edit_profil"
android:title="#string/drawer_menu_item_edit_profil" />
<item
android:icon="#drawable/ic_drawer_delete_account_24dp"
android:id="#+id/drawer_item_delete_account"
android:title="#string/drawer_menu_item_delete_account" />
<item
android:icon="#drawable/ic_drawer_logout_24dp"
android:id="#+id/drawer_item_logout"
android:title="#string/drawer_menu_item_logout" />
</group>
</menu>
</item>
</group>
And my method to update my NavigationView
private void setUpNavigationDrawer()
{
boolean isLoggedIn = sessionManager.isLoggedIn();
navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().findItem(R.id.drawer_item_login).setVisible(!isLoggedIn);
navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().setGroupVisible(R.id.group_actions_user, isLoggedIn);
}
After some operations I called setUpNavigationDrawer() but the menu was not updated !
Instead of referencing group and then finding a item from that group and setting it's visibility try to reference the item directly like this ...
navView.getMenu().findItem(R.id.drawer_item_login).setVisible(!isLoggedin);
navView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().setGroupVisible(R.id.group_actions_user, !isLoggedin);
It works for me. I hope it helps you.
I basically copy & pasted the code from right above and replaced the necessary tidbits, yet I still get this error. I'm trying to make a "Refresh" item in my menu.
XML code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/refresh_tasks"
android:title="#string/refresh"/>
</menu>
HomeActivity.java code:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh_tasks: //line where I get error
loadTasksFromAPI(TASKS_URL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I've searched across the internet, but I haven't managed to fix this. I assume something is wrong with my XML, but I don't see what it could be.
So, apparently my R.java stopped updating for some reason. My code was correct, but it wouldn't update. Editing the AndroidManifest.xml file and then saving seems to force R.java into updating. It's as simple as adding a space and then deleting the space from your AndroidManifest.xml file before you save (though, you could leave the space if you wish... It shouldn't cause an error).
Make sure there are no errors in any of your resources
. This would cause AAPT to stop generating a new R.class for you
Clean and rebuild the project
EDIT: Try to add this at the beginning of the xml:
<?xml version="1.0" encoding="utf-8"?>
I am trying to implement the SearchView ActionBar item as android developers says but I am having some trouble.
(http://developer.android.com/guide/topics/ui/actionbar.html).
There are two mistakes that although I have looked for a lot, I have not been able to find the solution.
1) I have a problem with the class MenuItemCompat. It says:
The method getActionView(MenuItem) is undefined for the type MenuItemCompat
I can only use for this class the following methods:
setShowAsAction(item, actionEnum)
setActionView(item, view)
Here it is the code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.restloader, menu);
MenuItem searchItem = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
return super.onCreateOptionsMenu(menu);
}
2) There is a problem with this:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
I don't understand why it is used but if google says it, it must be appropriate.
Error message:
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.pruebahttp3'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.pruebahttp3'
<?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/search_menu"
android:orderInCategory="100"
android:title="#string/search"
android:icon="#drawable/ic_search_category_default"
myapp:showAsAction="ifRoom|collapseActionView"
myapp:actionViewClass="android.support.v7.widget.SearchView">
</item>
Thank you very much!
i have got the same problem, i solved it by using the follow code. Be care of your namespace.`
<!-- Search, should appear as action button -->
<item
android:id="#+id/action_search"
android:icon="#drawable/abc_ic_search"
share:showAsAction="ifRoom"
share:actionViewClass="android.support.v7.widget.SearchView"
android:title="#string/abc_searchview_description_search" />
`
For the 1st:Fixing the second one will fix this :)
For the 2nd:
<?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" >
Change myapp to you application namespace com.xxx.xxx
Try to copy the lib files directly from yourFolder\sdk\extras\android\support\v7\appcompat\libs
I have a similar problem,but It occurs to me when i directly copy the JAR library file rather than following the android support library procedure. Try the opposite it might work for you.
Kinda weird if you ask me.
I am creating an option menu. it has three option HOME, EMAIL, VISIT. All three options coming at the same row. i have to show HOME option above and rest two below to Home Option(according to attached Screen Shot).
Any Ideas??
Thanks.
Here is my XML file.
<menu android:id="#+id/new_game"
android:icon="#drawable/red_no_bg"
android:title="Home" />
<item android:id="#+id/email"
android:icon="#drawable/red_no_bg"
android:title="Email" />
<item android:id="#+id/help"
android:icon="#drawable/red_no_bg"
android:title="Visit Microsite" />
</menu>
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.overview_menu, menu);
return true;
}
Using the above code i am getting the all option in Same line.
You can Create in two ways
1.Statically.
2.Dynamically.
Am explaining the easiest method of adding menus.
Step 1: Create a new Android Application.
Step 2: After onCreate() method write onCreateOptionMenu();
Step 3: In onCreateOptionMenu() Write these lines
menu.add(11, 21, 1, "Gowtham").setIcon(R.drawable.ic_launcher);
Here "Gowtham" is title of your menu item You can Home&Email use instead of "Gowtham".
Ans use Any images Instead of ic.launcher