I am using Android ICS 4.0.3 and I downloaded Ancal project and studying it. I added some option menu in a activity but it can't call opOptionsItemSelected method. Here is my code:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
String displayText = dateFormatFull.format(new Date());
switch(iCurrentAgendaViewType) {
case AgendaView.viewMode.DAY:
displayText = dateFormatFull.format(CurrentAgendaView.GetViewStartDate().getTime()).toString();
break;
case AgendaView.viewMode.WEEK:
displayText = dateFormatFull.format(CurrentAgendaView.GetViewStartDate().getTime()).toString();
break;
case AgendaView.viewMode.MONTH:
displayText = dateFormatMonth.format(CurrentAgendaView.GetCurrentSelectedMonthAsCalendar().getTime()).toString();
break;
}
if (iCurrentAgendaViewType == AgendaView.viewMode.TODAY) {
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else {
menu.add(Menu.NONE, R.drawable.ic_arrow_left, 0, "").setIcon(R.drawable.ic_arrow_left).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, R.drawable.ic_arrow_right, 2, "").setIcon(R.drawable.ic_arrow_right).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (DEBUG) {
Log.d(TAG, "============ onOptionsItemSelected ===========");
}
switch (item.getItemId())
{
case R.drawable.ic_arrow_left:
CurrentAgendaView.SetPrevViewItem();
RefreshAgendaAfterViewItemChange();
return true;
case R.drawable.ic_arrow_right:
CurrentAgendaView.SetNextViewItem();
RefreshAgendaAfterViewItemChange();
return true;
case miNewAppt:
openActAppointment(-1, -1, -1);
return true;
case miNewTask:
openActTask(-1);
return true;
case miNewNote:
openActNote(-1);
return true;
case miShowAllTasks:
{
item.setChecked(!item.isChecked());
prefs.bShowAllTasks = item.isChecked();
prefs.Save();
refreshData();
menuItemUpdateIcons(item);
return true;
}
case miOptions:
openActOptions();
return true;
case mTimeZone:
showTimeZone();
return true;
case miAbout:
openActViewAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I added actionbar menus at run time like the image but when I debug the above code, nothing call onOptionsItemSelected method.
What is wrong about this?
Thanks in advance.
I found the cause. The above activity extends CommonActivity. CommonActivity already overided onMenuItemSelected method. So I override onMenuItemSelected method like below:
public abstract class CommonActivity extends Activity {
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
if (item.getItemId() == android.R.id.home){
finish();
}
return true;
}
}
public class AnCal extends CommonActivity implements OnNavigationListener{
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
String displayText = getCurrentViewDateText();
if (iCurrentAgendaViewType == AgendaView.viewMode.TODAY) {
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
} else {
menu.add(Menu.NONE, android.R.id.button1, 0, "Prev").setIcon(R.drawable.ic_arrow_left).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, android.R.id.button2, 1, displayText).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, android.R.id.button3, 2, "Next").setIcon(R.drawable.ic_arrow_right).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return super.onCreateOptionsMenu(menu);
}
Related
I am creating a menu, but both of menu options were intent to same class so how to fix this? Sorry I am a starter developer, so, please give me solution of that problem.
public void btnclick(View v){
openOptionsMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0, 1, 0, "What's in it");
menu.add(0, 2, 0,"send");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intt=new Intent(this,Help.class);
startActivity(intt);
return true;
}
Since you are adding 2 menu items at the onCreateOptionsMenu: 1st with ItemId = 1 and 2nd with ItemId = 2, you can use getItemId() to distinguish between menu items, like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case 1:
//do stuff like:
Intent intt=new Intent(this,Help.class);
startActivity(intt);
break;
case 2:
//do another stuff, like launching another activity
break;
default:
break;
}
return true;
}
In my app i am using the following code to display the menu.But it is working in device,not working in tablet.In my app i dont want to display title bar,for that i am using the following code in manifest file.
"....android:theme="#android:style/Theme.NoTitleBar"...".By using this menu is working in device and not working in tablet.But when i removing "no titilebar " line in manifest it is working in tablet.but title bar is visible.Is there any possible menu is working without title bar.please help me.Thanks in advance
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_ZOOM_OUT, Menu.NONE, "Zoom Out").setIcon(getZoomOutImageResource());
menu.add(Menu.NONE, MENU_ZOOM_IN, Menu.NONE, "Zoom In").setIcon(getZoomInImageResource());
menu.add(Menu.NONE, MENU_MAIL, Menu.NONE, "Mail").setIcon(getMailImageResource());
menu.add(Menu.NONE, MENU_PRINT, Menu.NONE, "Print").setIcon(getPrintImageResource());
if (HardReference.sKeepCaches)
menu.add(Menu.NONE, MENU_CLEANUP, Menu.NONE, "Clear Caches");
return true;
}
/**
* Called when a menu item is selected.
*/
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_NEXT_PAGE: {
nextPage();
break;
}
case MENU_PREV_PAGE: {
prevPage();
break;
}
case MENU_GOTO_PAGE: {
gotoPage();
break;
}
case MENU_ZOOM_IN: {
zoomIn();
break;
}
case MENU_ZOOM_OUT: {
zoomOut();
break;
}
case MENU_BACK: {
finish();
break;
}
case MENU_CLEANUP: {
HardReference.cleanup();
break;
}
case MENU_MAIL: {
Uri mUri = getIntent().getParcelableExtra("file");
try {
sendemail(mUri);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case MENU_PRINT: {
myUri = getIntent().getParcelableExtra("file");
printUSingPrinterShare();
break;
}
}
return true;
}
I am trying to replace the menu item to another menu item after selection. I tried the following but it is not working as expected. Any solutions to this. thanks.
The menu.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/add" android:visible="true" android:enabled="true" android:title="Add"></item>
<item android:id="#+id/delete" android:visible="false" android:enabled="true" android:title="Delete"></item>
</menu>
The code inside MyActivity.java is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.defaultmenu, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(isAdded) {
menu.removeItem(R.id.add);
menu.add(0, R.id.delete, 0, "Delete");
} else {
menu.removeItem(R.id.delete);
menu.add(0, R.id.add, 0, "Add");
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add:
isLogin = true;
return true;
case R.id.delete:
isLogin = false;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
To refresh your menu call invalidateOptionsMenu();
And i guess in onPrepareOptionsMenu, you can do..
if(isAdded) {
menu.findItem(R.id.add).setVisible(false);
menu.findItem(R.id.delete).setVisible(true);
return true;
} else {
menu.findItem(R.id.add).setVisible(true);
menu.findItem(R.id.delete).setVisible(false);
return true;
}
return super.onPrepareOptionsMenu(menu);
Try like this
public static final int ADD_CATEGORY_INDEX = Menu.FIRST;
public static final int DELETE_CATEGORY_INDEX= Menu.FIRST+1;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, ADD_CATEGORY_INDEX, 0, "Add");
menu.add(0, DELETE_CATEGORY_INDEX, 0, "delete");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ADD_CATEGORY_INDEX:
break;
}
case DELETE_CATEGORY_INDEX:
break;
}
return true;
}
I Changed the onPrepareOptionsMenu, now it's working fine.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
//menu.clear();
if(isadded) {
menu.removeItem(R.id.add);
menu.removeItem(R.id.delete);
menu.add(0, R.id.delete, 0, "Delete");
} else {
menu.removeItem(R.id.add);
menu.removeItem(R.id.delete);
menu.add(0, R.id.add, 0, "Add");
}
return super.onPrepareOptionsMenu(menu);
}
I`m trying to request search from ActionBarSherlock.
My class extends SherlockListActivity.
This is how I`m adding Menu button to the ActionBar:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
The problem is that I am unable to invoke it by id:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case ?????: {
onSearchRequested();
return true;
}
}
return true;
}
How people usually solve this problem?
Thanks.
If you inflate you menu from an XML menu file you will set an id which you can then use in your onOptionsItemSelected method. Like this:
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" android:title="#string/search"/>
</menu>
Fragment
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_search:
onSearchRequested();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Also you can use
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 1, 1, "Refresh").setIcon(R.drawable.ic_refresh).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(0, 2, 2, "Search").setIcon(R.drawable.ic_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()){
case 1:
...
break;
case 2:
...
break;
}
return true;
}
I use phonegap,
When linking to another page with simple href my menu stop working.
In my Java class I override onKeyDown() and return false for menu button
#Override
public boolean onKeyDown(int i,KeyEvent e){
if (i==KeyEvent.KEYCODE_MENU){
return false;
}else{
return true;
}
}
When I loadUrl from the java class I can open the menu (in about.html)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.exit:
this.finish();
return true;
case R.id.about:
super.loadUrl("file:///android_asset/www/about.html");
return true;
default:
return super.onOptionsItemSelected(item);
}
}