2

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 Answer 1

5

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! That did the trick! Is there a way to make my button a rectangle rather than the traditional square?
I know you can set custom layout to menu item by using android:actionLayout="@layout/layout_name" in item tag. But maybe better ask another question for this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.