Getting the screen coordinate of an action bar menu item to create an input screen

I was wondering if it is possible to get the screen coordinates of a menu item in the action bar?

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

enter image description here

+7
source share
3 answers

Here is 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]; } } 
+6
source

Currently, there are no documented and supported tools for this, except for your own custom action layouts or types of actions. The API cannot retrieve the normal contents of the action bar (home access, title, regular elements).

ShowcaseView, as mentioned in another answer, is likely doing some undocumented / unsupported things for this, like traversing the View hierarchy to access these widgets. This is risky because future versions of Android or manufacturer / ROM modifications for Android may violate this logic.

+1
source

You can see how this function is implemented in the ShowcaseView library.

0
source

Source: https://habr.com/ru/post/945343/


All Articles