Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42690

Getting screen coordinate of action bar menu item for creating introduction screen

I was wondering, if there is any possibility of getting the screen coordinates of an action bar menu item?

As I would like to create an introduction screen, to draw an arrow pointing to the desired action bar menu item, so that the user knows where to start.

enter image description here

Upvotes: 8

Views: 3270

Answers (2)

penduDev
penduDev

Reputation: 4785

Here's how I did it:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId().equals(R.id.my_menu_item)) {
        View menuView = findViewById(R.id.menu_item_search);
        int[] location = new int[2];
        menuView.getLocationOnScreen(location);
        int menuViewX = location[0];
        int menuViewY = location[1];
    }
}

Upvotes: 7

CommonsWare
CommonsWare

Reputation: 1006674

Presently, there is no documented and supported means for doing this, except for your own custom action layouts or action views. There is no API to retrieve ordinary contents of the action bar (home affordance, title, regular items).

ShowcaseView, as mentioned in the other answer, probably does some undocumented/unsupported things for this, such as traversing the View hierarchy to get at these widgets. That's risky, insofar as future versions of Android, or manufacturer/ROM modder changes to Android, may break that logic.

Upvotes: 1

Related Questions