1

How do you create a right action button in the Navigation bar - getSupportActionBar() that only contains text and no image?

Tim Nuwin
  • 2,775
  • 2
  • 29
  • 63

1 Answers1

1

This worked for me:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_next"
        app:showAsAction="always|withText"
        android:title="Next"/>
</menu>

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         super.onCreateOptionsMenu(menu);
         getMenuInflater().inflate(R.menu.test_next, menu);
         return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_next:
            // .. do your thing
            return true;
        // ... other actions
        }
        return super.onOptionsItemSelected(item);
    }
kris larson
  • 30,387
  • 5
  • 62
  • 74
  • Right now my activity extends: "extends AppCompatActivity", what would I have to extend to override the onCreateOptionsMenu? – Tim Nuwin Nov 30 '15 at 16:01
  • AppCompatActivity extends FragmentActivity, which extends Activity, which defines the method. You shouldn't need to change anything with your activity. You should be able to just put your override for onCreateOptionsMenu right in your activity. – kris larson Nov 30 '15 at 16:08
  • It throws this error: (Menu) in Activity cannot be applied to (Menu, android.view.MenuInflater) – Tim Nuwin Nov 30 '15 at 16:13
  • 1
    Ah. You must be using API 23. I updated my answer to work with your project. – kris larson Nov 30 '15 at 16:44
  • Awesome, that was it! Quick side question though, in the XML I'm trying to set the color to: android:textColor="@color/colorPrimary", that doesn't appear to work and it's all white. When I call this in the Java code: actionBarToolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimary)); It changes the title and the back button color to the primary color. How do I set the item text color? – Tim Nuwin Nov 30 '15 at 17:00
  • Have a look at this question: http://stackoverflow.com/questions/6072226/how-to-style-the-menu-items-on-an-android-action-bar – kris larson Nov 30 '15 at 17:27