Is it possible to Implement Toggle Button in Action Menu Item using Actionbar sherlock in android

后端 未结 4 1903
栀梦
栀梦 2021-02-04 02:30

I have an app, which have toggle button in action menu item, though i\'m using Actionbar Sherlock, I don\'t know, how to place the toggle button in the action menu item. I don\'

4条回答
  •  情深已故
    2021-02-04 03:05

    Just add it like a normal Menu Button, check its state with a boolean variable, and you can change the icon and title when changing the sortmode

    boolean birthSort=false;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_toggle:
    
            if(birthSort){
                //change your view and sort it by Alphabet
                item.setIcon(icon1)
                item.setTitle(title1)
                birthSort=false;
            }else{
                //change your view and sort it by Date of Birth
                item.setIcon(icon2)
                item.setTitle(title2)
                birthSort=true;
            }
            return true;
    
    
    
        }
        return super.onOptionsItemSelected(item);
    
    
    }
    

    Don't forget to add it in xml like any other menu button and configure android:showAsAction if you want to show it in overflow or outside of it.

    
    
    
    
    

提交回复
热议问题