Search code examples
androidandroid-actionbaractionbarsherlock

Android Sherlock ActionBar OnCreateOption


I'm trying to add a next button in my app action bar ( Sherlock ), I'm new and couldn't find tut's anywhere so I tried using guides from Android.com, I want to add a next button into my first activity action bar

this is my Code at the StartActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    android.view.MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.StartActivity, menu);
    return true;
}

and this is the xml of main.xml

  <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/bNext"
          android:title="Next"
          android:showAsAction="ifRoom|withText" />
    </menu>

I get an error from R."menu".StartActivity

menu cannot be resolved or is not a field

Solution

  • First go into your menu folder and and add an .xml file which looks like this :

      <?xml version="1.0" encoding="utf-8"?>
      <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
         <item 
              android:id="@+id/share"
              android:title="Share"
              android:showAsAction="always"
              android:icon="@drawable/actionbar_share" />
    
       </menu>
    

    Then you go into your activity where you are calling the action bar and do this :

          @Override
      public boolean onCreateOptionsMenu(Menu menu) 
         {
              MenuInflater inflater = getSupportMenuInflater();
              inflater.inflate(R.menu.action_bar_menu, menu);
              return super.onCreateOptionsMenu(menu);
         }
    

    where action_bar is the xml file you created in the first step Then after you get your button in your action bar you need to add the

             public boolean onOptionsItemSelected(MenuItem item) 
    

    method and add whatever code you want there to be