I have created a Menu
with 4 menuitems
.I need to retrieve the menuitems
Id upon being pressed
and then a check must happen to see the what Id has been pressed
and go to a new Activity
. I have tried usingitem.getTitle()
and item.getItemId()
but it does not go into the if statement
.
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/one"
android:title="TOOL 7 - THE FORCE"/>
<item
android:id="@+id/two"
android:title="TOOL 5 - FOCUS"/>
<item
android:id="@+id/three"
android:title="TOOL 3 - ACTION"/>
<item
android:id="@+id/four"
android:title="TOOL 9 - QUESTION FIX"/>
</menu>
Code to retrieve MenuItemId
btnToolbox = (Button) findViewById(R.id.btnToolbox);
btnToolbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(Screen2_11.this,btnToolbox);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
itemName = String.valueOf(item.getTitle());
itemId = item.getItemId();
if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" ){
Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
startActivity(i);
}
else if ( itemName == "TOOL 5 - FOCUS" || itemId == "two" ) {
Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
startActivity(i);
}
else if ( itemName == "TOOL 3 - ACTION" || itemId == "three" ) {
Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
startActivity(i);
}
else if ( itemName == "TOOL 9 - QUESTION FIX" || itemId == "four" ) {
Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
startActivity(i);
}
return true;
}
});
popup.show();
}
});