First, define the layout containing 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>
Then you have two alternatives to continue:
Using the layoput action:
<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>
Software upgrade: In Activity or Snippet :
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);
source share