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

23,271

Solution 1

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>

Solution 2

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>

Solution 3

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>

Solution 4

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

Share:
23,271

Related videos on Youtube

RajeshVijayakumar
Author by

RajeshVijayakumar

Exploring new technologies

Updated on July 09, 2022

Comments

  • RajeshVijayakumar
    RajeshVijayakumar almost 2 years

    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!

    • Matthias Robbers
      Matthias Robbers about 11 years
      why don't you want to use a custom action layout?
    • RajeshVijayakumar
      RajeshVijayakumar about 11 years
      Already I had used custom layout for Action bar Title
    • CommonsWare
      CommonsWare about 11 years
      You cannot put a ToggleButton in the overflow menu. However, the overflow menu may support checkable items -- I have not tried that.
    • RajeshVijayakumar
      RajeshVijayakumar about 11 years
      I don't want toggle button in overflow menu item, instead i needed it as a toggle button as an action menu item
    • Gaurav Agarwal
      Gaurav Agarwal almost 11 years
      why don't you accept the answer by @Yalla T.
    • Gaurav Agarwal
      Gaurav Agarwal almost 11 years
      @CommonsWare What is your take on Yalla T. answer, it is contrary your comment.
    • CommonsWare
      CommonsWare almost 11 years
      @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.
  • wutzebaer
    wutzebaer almost 11 years
    additionaly you can grab the toggle button icon from the android-sdk folder
  • Rudolf Real
    Rudolf Real over 9 years
    But the view is not a Toggle Button, it's a normal <item>