I am developing a tutorial for an application and I need to point at a particular icon in the toolbar.
Here is an extract of the XML for the action menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/AbErase"
android:title="@string/Erase"
android:icon="@android:drawable/ic_delete"
android:orderInCategory="10"
app:showAsAction="ifRoom|collapseActionView" />
<item android:id="@+id/AbSuggest"
android:title="@string/Suggest"
android:icon="@mipmap/ic_lightbulb_outline_white_48dp"
android:orderInCategory="50"
app:showAsAction="ifRoom|collapseActionView" />
<item android:id="@+id/AbUndo"
android:title="@string/ActionBarUndo"
android:icon="@android:drawable/ic_menu_revert"
android:orderInCategory="51"
app:showAsAction="ifRoom|collapseActionView" />
...
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isBeingRestored = (savedInstanceState != null);
Toolbar scToolbar = (Toolbar) findViewById(R.id.Sc_toolbar);
setSupportActionBar(scToolbar);
scToolbar.post(new Runnable() {
@Override
public void run() {
if (!isBeingRestored) {
//View mErase = findViewById(R.id.AbErase);
View mErase = overflowMenu.getItem(0).getActionView();
int[] location = new int[2];
mErase.getLocationOnScreen(location);
eraseIconLeft = location[0];
}
}
}
With View mErase = findViewById(R.id.AbErase);
mErase
is set to null
, **** start EDIT **** which is not suprising as AbErase is the id of a MenuItem, not the id of a View. **** end EDIT ****
With View mErase = overflowMenu.getItem(0).getActionView();
location
is set to (0, 24), which is wrong as there already is a logo icon and a title in the toolbar.
How can I get the absolute X coordinate of the AbErase
view in the toolbar?
**** EDIT ****
here is the code where the initialisation of the static variable overflowMenu
can be found:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
actionBar.collapseActionView();
overflowMenu = menu;
isInitializedMenuItem = menu.findItem(R.id.AbInitialized);
isInitializedMenuItem.setChecked(isInitializeCbxChecked);
return super.onCreateOptionsMenu(menu);
}