How to differentiate MenuItems - android

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;
}

Related

Handling Click Event for menu Used for Floating Action Button Options

I used a library recently that Provides a Floating Action Button and changing to a Something like Navigation bar by clicking on it.
but i cant set click Event for my navigation bar Items.
after Pressing on items i don't see any reaction!
Anyone can help me?
fabOptions = (FabOptions) findViewById(R.id.fabcontainer);
fabOptions.setButtonsMenu(R.menu.menu1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
newBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newBack() {
Toast.makeText(this, "back", Toast.LENGTH_SHORT).show();
}
}
First of all you should have to use break whenever you use switch statement otherwise the program will go to default status. so use break; the code below.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.back:
newBack();
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}

how to use onMenuItemSelected

i am using onCreateOptionsItem as shown below in the code, but when i overide onMenuItemSelected, it is marked with red. is there an alternative to it?how
can i fix this errors
code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem miRefrsh = menu.add(0, 1, 0,"refresh");
miRefrsh.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
//进入关于页面
MenuItem miScan = menu.add(0, 2, 1, "scan");
miScan.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
//退出系统
MenuItem miCancel = menu.add(0, 3, 2, "cancel");
miCancel.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
MenuItem miExit = menu.add(0, 4, 2, "exit");
miExit.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return super.onCreateOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case MEMU_RESCAN:
this.mGP.closeConn();
this.initActivityView();
this.openDiscovery();
return true;
case MEMU_EXIT:
this.finish();
return true;
case MEMU_ABOUT:
this.openAbout();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// do Your Work Here
return super.onOptionsItemSelected(item);
}
Android knows about several types of menus (e.g. Options Menu and Context Menu). ''onMenuItemSelected'' is the generic callback. You don't need to use this usually. ''onOptionsItemSelected'' is the callback of the options menu and onContextItemSelected is the callback of the context menu.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) { //or switch-case
finish();
}
if (id == R.id.your_item1) {
}
if (id == R.id.your_item2) {
}
return super.onOptionsItemSelected(item);
}
SUDARSHAN is right. you can use onOptionsItemSelected.
use onOptionsItemSelected(MenuItem item) method and extend AppCompatActivity
In your case you should use menuItem.getTitle() and compare to the titles you programmatically added previously. Use menuItem.getId online when you're inflating a XML menu file. Hope this helps

how to get the OnClick event of Option menu button in Sherlock ActionBar in Android?

I am working on Sherlock ActionBar and using an Option button at right corner, near search icon. I am successful to generate the onClick event for SEARCH button but I am getting problems in onClick event of the option Button.
Using the below code to add the buttons :
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.add(0, 1, 1, "Search").setIcon(R.drawable.search_icon).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
menu.add(0, 2, 2, "Button").setIcon(R.drawable.menu_overflow_icon).setActionView(R.layout.action_button).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
And for click events :
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case 1:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
break;
case 2:
Toast.makeText(ActivityScreen.this, "hit it", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
I have shown my requirement in the image attached below.
Replace your break; statements in the Switch of onOptionsItemSelected to return true;
Addition to the answer
final static int BUTTON_ID = 2;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add(Menu.NONE, BUTTON_ID, Menu.NONE, R.string.action_option_name);
item.setIcon(R.drawable.action_option);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case BUTTON_ID:
Toast.makeText(this, "clicked on 2", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
Note: I haven't added MenuItem.setActionView(R.layout.action_search). I tried to use the solution at https://stackoverflow.com/a/11292843/1567588. But its not working. And i have set GroupId and OrderId to Menu.None

onOptionsItemSelected doesn't call in Android

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);
}

Launch submenu only if a particular conditions is true

Is there a way when a Menu item is clicked and before going to submenu we have to check a condition, if it is valid we launch the submenu if not we cancel. Please Help me I'm stuck with it ...
Thanks
okkk.. getting proble you should try like this..
public class Practice_TestActicvity extends Activity {
SubMenu sub;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("this is first menu");
menu.add("this is second menu");
sub = menu.addSubMenu(0, 1, 0, "SubMenu");
// sub.add(0,11,0,"SubMenu 1");
return super.onCreateOptionsMenu(menu);
}
and
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getTitle().toString().equals("SubMenu")) {
sub.clear();
//you have to put here condtion like
//if(this==this){
Toast.makeText(this, "this is cliked", Toast.LENGTH_LONG).show();
sub.add(0, 11, 0, "SubMenu 1");
//}
//else{//execute this code
//}
}
return super.onMenuItemSelected(featureId, item);
}
now let me know which type of condition you wan to check?
Yes Ofcourse:
You can add/override onOptionsItemSelected(MenuItem item) method to get the results. See the example below:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.logout:
if(islogout){
//code if islogout is setted to true and procceed further for submenu
return true;
}else{
//code if islogout is setted to False and display Toast that could not procceed further.
return false;
}
default:
return super.onOptionsItemSelected(item);
}
}
I hope this helps.
Mark this answer Correct and with UpVote it it helps you!
Thanks
sHaH

Categories

Resources