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

Total Post:27

Posted on    November-15-2014 3:26 AM

 Android Android sdk 

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

Tom Cruser
Tom Cruser

Total Post:28

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:showAsAction="ifRoom" />

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

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


          android:showAsAction="never" />


Use this override method

// This method will show menu in action bar


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


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;


            return super.onOptionsItemSelected(item);



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

Follow MindStick