Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 893
RajeshVijayakumar Avatar asked Feb 19 '13 13:02

RajeshVijayakumar


People also ask

How do you add action items to the action bar in Android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.

What is the ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button. In general an ActionBar consists of the following four components: App Icon: App branding logo or icon will be displayed here.

What is an ActionBar?

In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and contains some of the frequently used elements for the users.


1 Answers

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>
like image 146
Yalla T. Avatar answered Sep 21 '22 09:09

Yalla T.