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\'
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>
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>
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>
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