Add toggle button to the menu to stop and start service

后端 未结 6 812
抹茶落季
抹茶落季 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:54

    You cannot put any widget in <menu> and expect it to work. What you can put there is documented here and it's basically limited to menu <item> and <group>. No buttons, toggles and other widgets are supported. If that would be sufficient you can use android:checkable on the <item> or use old-skool approach and alter menu item depending on the state (if service is on, then your item should read turn service off and vice versa).

    0 讨论(0)
  • 2020-12-11 03:55

    As mentioned above, you can't add toggle button to the menu. You can use the android:checkable property in your menu item to handle the two states.

    Something like:

    Menu:

    <item
        android:id="@+id/checkable_menu"
        android:checkable="true"
        android:title="@string/checkable" />
    

    Activity:

    private boolean isChecked = false;
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem checkable = menu.findItem(R.id.checkable_menu);
        checkable.setChecked(isChecked);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.checkable_menu:
                isChecked = !item.isChecked();
                item.setChecked(isChecked);
                return true;
            default:
                return false;
        }
    }
    

    PS: Copied the code from here.

    Or you can just update your item icon on click event to show the two states with item.setIcon(yourDrawable));

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-11 03:59

    I know its very a long time to post an answer, but it may help someone :)

    I followed this link and update some of the implemented solution as the app was crashed before these modifications

    And below is the full solution: 1- Create a new xml file under layout folder and name it switch_layout.xml and put the below:

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Switch
            android:id="@+id/switchAB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
    

    2- Add the below menu item in the main.xml file under menu folder:

    <item
        android:id="@+id/switchId"
        android:title=""
        app:actionLayout="@layout/switch_layout"
        app:showAsAction="always" />
    

    3- Go to your activity and below is a full implementation for onCreateOptionsMenu method:

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem item = (MenuItem) menu.findItem(R.id.switchId);
        item.setActionView(R.layout.switch_layout);
        Switch switchAB = item
                .getActionView().findViewById(R.id.switchAB);
        switchAB.setChecked(false);
    
        switchAB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(getApplication(), "ON", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    Toast.makeText(getApplication(), "OFF", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
        return true;
    }
    
    0 讨论(0)
  • 2020-12-11 04:06

    Menu resources are distinct from conventional layouts; you cannot simply add widgets into them and expect them to work. The only elements allowed inside a menu resource is <item> or <group>.

    Using a custom layout inside a menu isn't possible, I'm afraid. You may instead want to replace the entire menu with a PopupWindow, and supply your layouts there instead.

    You may want to consider two alternatives:

    • Using a conventional menu entry as a toggle, or
    • Placing the ToggleButton immediately inside the Actionbar/Toolbar, instead of inside the menu.
    0 讨论(0)
  • 2020-12-11 04:16

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

    <item
        android:id="@+id/show_secure"
        android:enabled="true"
        android:title=""
        android:visible="true"
        app:actionLayout="@layout/show_protected_switch"
        app:showAsAction="ifRoom" />
    

    And this is your show_protected_switch.xml layout.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <ToggleButton
        android:id="@+id/switch_show_protected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/switch_ptotected_btn_selector"
        android:textOff=""
        android:textOn=""/>
     </RelativeLayout>
    

    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

    0 讨论(0)
提交回复
热议问题