Add toggle button to the menu to stop and start service

后端 未结 6 831
抹茶落季
抹茶落季 2020-12-11 03:24

I am trying to enable the user to stops and starts service which I am implementing from the Menu where the text is will be changed when he clicks it so I want to add T

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 04:16

    It is easy. Rather you will have your toggle button on Toolbar.

    
    

    And this is your show_protected_switch.xml layout.

    
    
    
    
     
    

    And in code:

     ToggleButton mSwitchShowSecure;
     mSwitchShowSecure = (ToggleButton) menu.findItem(R.id.show_secure).getActionView().findViewById(R.id.switch_show_protected);
            mSwitchShowSecure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(b){
                        //Your code when checked
    
                    } else {
                        //Your code when unchecked
                    }Y
                }
            });
    

    Output!

    It is rather big but you can adjust its size, obviously

提交回复
热议问题