Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Associate your actionbar popup menu with the hardware menu button in Android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.91k
    Comment on it

    Google Kills Android Menu Button, Replaces It with Action Bar

    But still so many devices have hardware menu button so now we can combine hardware menu button along with action bar option popup menu by just doinig a small trick. When you clicks on the device's hardware menu button you can open a custom popup menu which has options pertaining to action bar. According to android documentation, only context menu and options menu can be made, and there is no way to access the hardware menu button's functions. But here i can show you how to do that you just have to follow the code given below :-

    here is the trick for xml -:

    This is to add options to the menu -:

    Create an XML file for action bar menu optionmenu.xml

    res/menu/optionmenu.xml

    <item
        android:id="@+id/action_dropdown"
        yourapp:showAsAction="always"
        android:icon="@drawable/settings"
        android:orderInCategory="100"
        android:title="">
    </item>
    

    Create a xml file for popupmenu dropdow popup.xml

    res/menu/popup.xml

    <item
        android:id="@+id/action_home"
        android:showAsAction="always"
        android:title="@string/home"/>
    <item
        android:id="@+id/action_products"
        android:showAsAction="always"
        android:title="@string/title_activity_products"/>
    <item
        android:id="@+id/action_order"
        android:showAsAction="always"
        android:title="@string/title_activity_orders"/>
    <item
        android:id="@+id/action_reports"
        android:showAsAction="always"
        android:title="@string/title_activity_reports"/>
    <item
        android:id="@+id/action_more"
        android:showAsAction="always"
        android:title="@string/title_activity_more"/>
    

    here is the trick for Activity Class

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
    
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.optionmenu, menu);
        return true;
    }
    
    
               //  this is to access hardware menu button
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
        case KeyEvent.KEYCODE_MENU:
            View menuItemView = findViewById(R.id.action_dropdown);
            PopupMenu popupMenu = new PopupMenu(this, menuItemView);
            popupMenu.getMenuInflater().inflate(R.menu.popup,
                    popupMenu.getMenu());
            popupMenu
    .setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                            Toast.makeText(Orders.this,
                                    "You Clicked : " + item.getTitle(),
                                    Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    });
            popupMenu.show();
            return true;
    
        }
        return super.onKeyUp(keyCode, event);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            this.finish();
            return true;
        case R.id.action_dropdown:
            View menuItemView = findViewById(R.id.action_dropdown);
            PopupMenu popupMenu = new PopupMenu(this, menuItemView);
            popupMenu.getMenuInflater().inflate(R.menu.popup,
                    popupMenu.getMenu());
            popupMenu
    .setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                            Toast.makeText(Orders.this,
                                    "You Clicked : " + item.getTitle(),
                                    Toast.LENGTH_SHORT).show();
                            return true;
                        }
                    });
            popupMenu.show();
    
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
    
    
    

    Hope this will help you all the best. Cheers.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: