0

I'm attempting to grab a menuItem when onCreateOptionsMenu is called and override the default view by using setActionView (I'm aware I could use CardScrollView and CardScrollAdapter but this is a lot of work just get a menu up and running).

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/test_item"
        android:title="Test 1">

I'll attempt to override this programmatically in onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    View testView = new CardBuilder(this, CardBuilder.Layout.TEXT)
            .setText("Test 2.")
            .getView();

    MenuItem item = menu.findItem(R.id.test_item);

    item.setActionView(testView);

    return true;
}

However, when I view this options menu through the glass, Test 1 still shows. What am I doing wrong?

pt2121
  • 11,720
  • 8
  • 52
  • 69
Kurt Mueller
  • 3,173
  • 2
  • 29
  • 50

1 Answers1

1

If you want to change the menu title dynamically, you can override onPrepareOptionsMenu.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item = menu.findItem(R.id.test_item);
    // may be if case to check the condition
    // String title = titleShouldBe1 ? "Test 1" : "Test 2";
    // item.setTitle(title);
    item.setTitle("Test 2");
    return true;
}

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

Hope this helps.

pt2121
  • 11,720
  • 8
  • 52
  • 69
  • Hi, thanks for your help. I actually wanted to override the card's layout to another layout. Hence why I wanted to change the card's view. – Kurt Mueller Nov 25 '14 at 03:44