2
votes

For some reason, I cannot get my button to appear on the Action Bar. I have defined it in an XML file in /res/menu, along with inflating it and listening for an action. The icon is present in /res/drawable-hdpi. And nothing of interest shows in logcat. :(

XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/logout"
      android:icon="@drawable/logout"
      android:title="Logout"
      android:orderInCategory="100"
      android:showAsAction="always" />
</menu>

Code in main activity:

public class MainActivity extends ActionBarActivity {

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.logout:
            //logout code
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    //rest of app
}

I followed this question for my initial problem, and it didn't help. How to add button in ActionBar(Android)?

1

1 Answers

5
votes

Try with this change:

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

    <item android:id="@+id/logout"
        android:icon="@drawable/logout"
        android:title="Logout"
        android:orderInCategory="100"
        yourapp:showAsAction="always" />
</menu>