Home > DeveloperSection > Forums > How to perform action on Actionbar icon?
Lillian Martin

Total Post:27

Points:189
Posted on    November-15-2014 3:26 AM

 Android Android sdk 
Ratings:


 1 Reply(s)
 551  View(s)
Rate this:
Actionbar icon in android


Tom Cruser
Tom Cruser

Total Post:28

Points:196
Posted on    November-15-2014 6:54 AM

Add menu  in res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Search, should appear as action button -->

    <item android:id="@+id/action_search"

          android:icon="@drawable/ic_action_search"

          android:title="@string/action_search"

          android:showAsAction="ifRoom" />

    <!-- Settings, should always be in the overflow -->

    <item android:id="@+id/action_settings"

          android:title="@string/action_settings"

          android:showAsAction="never" />

</menu>

Use this override method

// This method will show menu in action bar

@Override

public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu items for use in the action bar

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.main_activity_actions, menu);

    return super.onCreateOptionsMenu(menu);

}

 

And now again use below override method

@Override

public boolean onOptionsItemSelected(MenuItem item) {

    // Handle presses on the action bar items

    switch (item.getItemId()) {

        case R.id.action_search:

// your code here      

            return true;

        case R.id.action_settings:

// your code here      

            return true;

        default:

            return super.onOptionsItemSelected(item);

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick