How to track the Share button clicked on the ActionBar? - android

I have added Share option to my ActionBarSherlock, this way:
public boolean onCreateOptionsMenu(final Menu menu) {
menu.add("Share")
.setIcon(R.drawable.ic_title_share_default)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
and on clicking this Icon i want to do something. how can i track the click on this ShareIcon??

You should create an XML file to define menu items.
For example mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/share"
android:icon="#drawable/ic_title_share_default"
android:title="#string/share"
android:showAsAction="ifRoom"/>
</menu>
Then in the onCreateOptionsMenu you will do:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
To handle the item selection:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
//do something for share
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You can see more information in here:
http://developer.android.com/guide/topics/ui/menus.html
http://actionbarsherlock.com/usage.html

Related

Where to add menu to work in all activities

I have a menu.xml and the code to show the menu in action bar. Do i have to add the code in every activity? Cause after this i use a switch case to get click, and more code, so i dont thing its the wright way to copy-paste the same code in every activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
Is there a better way to do it as to work in all app (all activities)?
Create a BaseActivity class.
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
}
Now all activities that extend your base activity will have the same menu.
this is a guide for Android Menu
this is an example menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
And then inflating it within your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
whe item clicked:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
good luck and read the doc official for android

How to add search and checkbox icon with their functions in android action bar?

I want to add a searchview and checkbox into action bar menu. And this checkbox will be visible if searchview is opened. And in it's opposite case it will be hidden. How I can do this?
I do something below . But it doesnt work correctly. I want hide checkbox (In my notes) when searchview is closed.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/search_button"
android:icon="#drawable/ic_icon_search"
android:title="Arama"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView">
</item>
<item
android:id="#+id/search_in_my_notes_checkbox"
app:showAsAction="ifRoom"
android:title="#string/search_in_my_notes"
android:checkable="true"
android:visible="false"
/>
</menu>
HomeActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.search_button){
MenuItem searchInMyNotesCheckbox = (MenuItem)menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(true);
}
return super.onOptionsItemSelected(item);
}
I found an alternative solution. I will use two checkbox image with checked and unchecked. And I will override onCreateOptionsMenu like this :
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem search = (MenuItem)menu.findItem(R.id.search_button2);
final MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
MenuItemCompat.setOnActionExpandListener(search,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// Return true to allow the action view to expand
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
MenuItem searchInMyNotesCheckbox = (MenuItem) menu.findItem(R.id.search_in_my_notes_checkbox);
searchInMyNotesCheckbox.setVisible(false);
return true;
}
});
searchInMyNotesCheckbox.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getIcon() == R.drawable.checked_checkbox){
item.setIcon(R.drawable.unchecked_checkbox);
}
else {
item.setIcon(R.drawable.checked_checkbox);
}
return false;
}
});
return super.onCreateOptionsMenu(menu);
}

MenuInflater not working - menu not showing

My menu is not showing in the ActionBar (onCreateOptionsMenu) is called properly. The icon and string are available. The code works fine in my other projects. I am using android.support.v7.app.ActionBarActivity for android:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Intent addIntent = new Intent(this, RoomAddActivity.class);
startActivity(addIntent);
break;
}
return super.onOptionsItemSelected(item);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
android:title="#string/action_add"/>
</menu>
Try to return true explicitly:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Updated:
By the way, you are not obliged to call super method. As to my experience it works all right without it.

OnMenuItemSelected isn't called when layout is set for menu item

I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.

android menu items do not display

there is an activity in my android application. I override the 'onCreateOptionsMenu' method, adding four menu items in the activity. But the menu items do not display. I can not figure out what is the problem. Could somebody give me an clue to fix that or explaination?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, FeaturedActivity.MENU_FEATURED, 0, R.string.menu_featured).setIcon(R.drawable.icon_tabbar_featured);
menu.add(0, FeaturedActivity.MENU_THE_DRINK, 1, R.string.menu_the_drink).setIcon(R.drawable.icon_tabbar_drinks);
menu.add(0, FeaturedActivity.MENU_PLAYER, 2, R.string.menu_player).setIcon(R.drawable.icon_tabbar_player);
menu.add(0, FeaturedActivity.MENU_SHARE, 3, R.string.menu_share).setIcon(R.drawable.icon_tabbar_share);
return true;
}
in your activity use
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.info:
startActivity(new Intent(this, AboutApp.class));
return true;
case R.id.exit:
finish();
return true;
}
return false;
}
and create a folder menu in res and now create an xml in res/menu like optionsmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/info" android:title="Info"
android:icon="#drawable/info_menubtn" />
<item android:id="#+id/exit" android:title="Exit" />
</menu>
Hope this will work for you
remove the line super.onCreateOptionsMenu(menu); and try .
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("this is menu");
menu.add("this is another");
return super.onCreateOptionsMenu(menu);
}
edit into above code and add return super.onCreateOptionsMenu(menu); at last and remove it from first line

Categories

Resources