Question

Hii i want to create a PopUpMenu inside the onCreateOptionMenu() i am targeting API level 10 and my minSdk version is 8 for tablet as you know there is no hard menu key so for soft menu key i am using onCreateOtionMenu() to create a menu option below my screen and it is also working.when i am presssing the Soft menuOption in my screen its displaying the PopUpMenu for the firsttime and the next time onwards its not showing anything.

here is my code snippets for PopUpMenu inside OnCreateOptionssMenu() method

  @Override 
    public boolean  onCreateOptionsMenu(Menu menu) {

    PopUpMenu popup=new PopUpMenu(this,txtView);
                        popup.getMenuInflater().inflate(R.menu.option,popup.getMenu());
     popup.show();

                        //popup.dismiss();
                        //menu.clear();
                        //getMenuInflater().inflate(R.menu.fragmenttwo_menu, menu);
                            return (super.onCreateOptionsMenu(menu));


                        }

Note: here txtView is a TextView you can say a view Anchor below that i am displaying the PopUpMenu Items. For calrification here i am using ViewPager

Was it helpful?

Solution 2

After did lots of R&D i found that with softmenu key Displaying PopUpMenu for target version 10 is not possible.PopUpMenu is only available after 11+ API Level.In my case sometimes its displaying and some time its not displaying.If you have any solution please let me inform.

OTHER TIPS

you can use this way :

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0) {

            Toast.makeText(MainActivity.this, "hii", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Edited: in above code there is Toast that's why it dismiss after some time. its depend on your code what you done inside method. now check below code. dialog is show till you dismiss it.

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0) {

            PopupMenu popupMenu = new PopupMenu(MainActivity.this, lView);
            popupMenu.getMenuInflater().inflate(R.menu.activity_main,
                    popupMenu.getMenu());
            popupMenu.show();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Hope You Understand....

I used the user1381827's answer but then realized that the menu keeps disappearing. Here is the code that I ended up with:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 1) {
        ImageView v = (ImageView) findViewById(R.id.header);
        showHeaderMenu(v);
        return false;
    }

    if(keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_BACK) return super.onKeyUp(keyCode, event);
    return true;
}

But in general case last 2 lines may/should be replaced with just

return super.onKeyUp(keyCode, event);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top