20

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't want to place as a custom layout in action bar, but i want to place it as a Menu item. If anyone find solution, Please help me out.

Purpose, If I change the state of toggle button, it will sort the person based on ALphabets and again in Date of Birth.

Thanks in Advance!

6
  • why don't you want to use a custom action layout? Feb 19, 2013 at 13:06
  • Already I had used custom layout for Action bar Title Feb 19, 2013 at 13:09
  • You cannot put a ToggleButton in the overflow menu. However, the overflow menu may support checkable items -- I have not tried that. Feb 19, 2013 at 13:12
  • I don't want toggle button in overflow menu item, instead i needed it as a toggle button as an action menu item Feb 19, 2013 at 13:15
  • 1
    @codingcrow: "it is contrary your comment" -- no, it is not, as that is not a ToggleButton. It may well work, but my comment was specifically with regards to ToggleButton. Personally, I'd use a checkable item (which does work), as that's more standard. Jul 24, 2013 at 22:36

5 Answers 5

39

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.

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/menu_toogle"
    android:showAsAction="ifRoom"
    android:title="Share"
     />
</menu>
2
  • additionaly you can grab the toggle button icon from the android-sdk folder
    – wutzebaer
    Jul 20, 2013 at 21:40
  • But the view is not a Toggle Button, it's a normal <item> Dec 5, 2014 at 18:45
4

Other approach would be to use a custom layout for your ActionBar:

Basically you define a layout that contains your Toggle:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <ToggleButton
    android:id="@+id/actionbar_service_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Logging On"
    android:textOff="Logging Off" />
</RelativeLayout>

ALTERNATIVE 1: Then in your Activity or Fragment container you do:

ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
ToggleButton button = (ToggleButton) findViewById(R.id.actionbar_service_toggle);

Notice that you are having a real ToggleButton and you are handling it in code as a real object ToggleButton, which has lots of advantages compared to having you re-implement your own toggle (theme, reliability, views hierarchy, native support...).

Source code here.


ALTERNATIVE 2: Another way to do it is embed your custom view into a regular menu view:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/myswitch"
        android:title=""
        android:showAsAction="always"
        android:actionLayout="@layout/actionbar_service_toggle" />   
</menu>
2

If like me the actionLayout isn't working for you, try app:actionLayout="@layout/actionbar_service_toggle" instead of android:actionLayout="@layout/actionbar_service_toggle" as well as app:showAsAction="always" instead of android:showAsAction="always" This is because if you use appCompat the android namespace won't be used.

So here's the final version:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <ToggleButton
    android:id="@+id/actionbar_service_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Logging On"
    android:textOff="Logging Off" />
</RelativeLayout>

and

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/myswitch"
        android:title=""
        app:showAsAction="always"
        app:actionLayout="@layout/actionbar_service_toggle" />   
</menu>
1

create xml file in menu:

menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="si.ziga.switchinsideab.MainActivity">
<item android:id="@+id/switchId" android:title="" android:showasaction="always" android:actionlayout="@layout/switch_layout">
   <item android:id="@+id/action_settings" android:orderincategory="100" android:title="@string/action_settings" app:showasaction="never">
</item></item></menu>

And then navigate to your layout folder make a new xml file and name it switch_layout.xml Here's the code: switch_layout.xml

<!--?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">
</switch></relativelayout>

In your MainActivity class copy and paste this code:
MainActivity.java

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);

switchAB = (Switch)menu.findItem(R.id.switchId)
.getActionView().findViewById(R.id.switchAB);7

Or follow this link about this stuff

0

Here somthing easy

<?xml version="1.0" encoding="utf-8"?>
<item
    android:title="@string/mode"
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:switchMinWidth="56dp"
    android:layout_marginTop="120dp"
    app:showAsAction="ifRoom"
    app:actionViewClass="androidx.appcompat.widget.SwitchCompat"
    android:checked="false"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.