How to add click event on Android Action Bar - java

I have added one star icon on Action Bar. Now I want to add click event on that star. If the star is clicked then it should be bright or Turned ON which is OFF by default. And if that star is ON and clicked, then it should go to OFF. I am not getting any idea how to perform onClick event on action bar.

You have a good example here.
Action Bar Example

To add onClickListener to a view in your action bar you have to create a reference to that view first and then in the onCreateOptionsMenu() method you can register the onClickListener.
See the answer to this question here. Its the same principle, you're simply using a 'star icon' instead of a switch.

Related

Android: Detecting Long Click on PopupMenu Items

I currently have a button that generates a popup menu and I'm trying to find a way to detect when an item within the menu has been long clicked. Once I detect the user has clicked and held on the menu item I want to prompt them to ask if they wish to delete the item.
I understand how to programatically remove menu items, ala the post here: remove popup menu item programmatically. The problem i'm having is detecting the long click on a menu item.
As far as I know there is no setMenuOnItemLongClickListener(), only a setMenuOnItemClickListener(), so am I left to make a custom solution to detect long clicks on popupmenu items?
Set action view to menu items and get them via getActionView(). To this view you can attach long click listeners.
You can attach an action view to menu item through xml as well:
<item
...
android:actionViewClass="<custom class>"
</item>

Is there a way to change the place where android menu appears

normally when the menu button is pressed , the menu appears from the center bottom of the screen. Is there a way to make it appear from sides.
On Android 3.0 and higher, items from the options menu are presented by the action bar which is standard way to provide access to menus and normally menus appears from top right corner. You should go through this once:
Menus Action Bar

Actionbar Custom Position

I'm relatively new with Android development, and I received a layout for an app where I have to put some kind of bar with images above the action bar.
I searched a lot in Google but I couldn't find anything about change the action bar position. I also found some info saying:
Google highly recommend to NOT change the action bar position(...)
Well, how I got some custom views for the action bar icons was to put the view below the action bar. However, what I am supposed to do is put the view above the action bar.
Is there a way to put a view above the action bar or change the action bar position?
Is there a way to put a view above the action bar or change the action bar position?
Not that I am aware of.

Android action bar items on the left side of the action bar

I am required to create an action bar that has its items split: four of them on the right side and four on the left (it's a tablet-only application). In portrait mode they shouldn't be split. Is it possible to implement without using a custom view for the left side of the action bar, as it seems inconsistent?
Note that I want to know how to do it with the default action items, not with the custom ones in a custom view, and that I don't have any influence on the UI decisions, so please do not suggest to change the concept.
All ActionBar items are aligned to the right. It is not possible without custom view.
But you can use actionViewStyle for buttons to make them styled as menu items.
https://stackoverflow.com/a/13143180/1366471

Refreshing the Action Bar in Android 3.0

I am trying to implement an action bar within an activity.
Is it possible to "refresh" or re-inflate the action bar with a new menu(without restarting the activity)
The only way I have found so far is to leave the action bar empty and assign it a new customView every time I want it changed.
Try calling invalidateOptionsMenu() on your activity. Quoting the documentation:
Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed.

Categories

Resources