3

I want to put an action bar with a menu button into a single android activity. I've put this in my activity XML:

<android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />

I've put this in my JAVA file:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.chat_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.view_profile:
                finish();
                return true;
            case R.id.report_match:
                finish();
                return true;
            case R.id.add_match:
                finish();
                return true;
            case R.id.unmatch:
                finish();
                return true;


            default:
                return super.onOptionsItemSelected(item);
        }
    }

And I've put this in the menu XML

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/title"
        android:title="@string/options"
        android:showAsAction="never"
        tools:ignore="AppCompatResource" />
    <item android:id="@+id/view_profile"
        android:title="@string/view_profile" />
    <item android:id="@+id/report_match"
        android:title="@string/report_match" />
    <item android:id="@+id/add_match"
        android:title="@string/add_match" />
    <item android:id="@+id/unmatch"
        android:title="@string/unmatch" />
</menu>

Currently the action bar shows up empty when I launch the app (IMAGE) Which is strange because when I edit the menu XML the preview shows it the way I want it to (IMAGE). Thanks in advance!

Muhryn
  • 53
  • 5

1 Answers1

1

Have you added the Toolbar to the activity?

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);

setSupportActionBar(myToolbar);

More on setting up the Toolbar

Adí
  • 854
  • 12
  • 27