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!
Other approach would be to use a custom layout for your ActionBar:
Basically you define a layout that contains your Toggle:
ALTERNATIVE 1: Then in your Activity or Fragment container you do:
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:
create xml file in menu:
menu.xml
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
In your MainActivity class copy and paste this code:
MainActivity.java
Or follow this link about this stuff
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
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.If like me the actionLayout isn't working for you, try
app:actionLayout="@layout/actionbar_service_toggle"
instead ofandroid:actionLayout="@layout/actionbar_service_toggle"
as well asapp:showAsAction="always"
instead ofandroid:showAsAction="always"
This is because if you use appCompat the android namespace won't be used.So here's the final version:
and