Add toggle button to the menu to stop and start service

后端 未结 6 823
抹茶落季
抹茶落季 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 03:58

    In the menu xml you add items not widgets (no buttons/textviews etc)

    You simply specify an ID and an ICON for the item, then inflate them in your activities onCreateOptionsMenu() method.

    then there is a method called onOptionsItemSelected(MenuItem item) check items id against the ids your expecting.

    If its equal to your toggle service option determine service state and alter, if you want to have a toggle button function you can use item.setIcon(drawable) here.

提交回复
热议问题