Question

In my activity test I need to verify that a menu item has certain title.

I can get it by ID but it returns a view.

final View menuItem = (View) mActivity.findViewById(R.id.do_it);

But how can I get the title from this view then?

Note: I need to do it in ActivityInstrumentationTestCase2 test.

Update

I am hoping to find a way to get menu item's title in my test case without writing any additional code in activity, like storing menu in a variable.

Was it helpful?

Solution

Use getTitle():

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

    MenuItem menuItem = menu.findItem(R.id.do_it);
    String title = menuItem.getTitle();

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top