1

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();
        }
    });
AnonymousZA
  • 121
  • 1
  • 11

2 Answers2

1

Use this if (itemId == R.id.one) instead of if ( itemName == "TOOL 7 - THE FORCE" || itemId == "one" )

Try this

    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) {

                   int itemId = item.getItemId();

                    if (itemId == R.id.one) {
                        Intent i = new Intent(getApplicationContext(), Screen108_Force.class);
                        startActivity(i);
                    } else if (itemId == R.id.two) {
                        Intent i = new Intent(getApplicationContext(), Screen120_Focus.class);
                        startActivity(i);
                    } else if (itemId == R.id.three) {
                        Intent i = new Intent(getApplicationContext(), Screen119_Action.class);
                        startActivity(i);
                    } else if (itemId == R.id.four) {
                        Intent i = new Intent(getApplicationContext(), Screen136_Questions.class);
                        startActivity(i);
                    }
                    return true;
                }
            });
            popup.show();
        }
    });
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

you need to initialize the menu first. Add this portion after finishing your onCreate()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_main,menu);
    return true;
}

then handle the item pressed options here.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //here initialize your button pressed.

    return super.onOptionsItemSelected(item);
}
Sayem
  • 78
  • 1
  • 10