10

This is My Code

home.xml

<item
    android:id="@+id/action_shopping_cart"
    android:actionLayout="@layout/action_bar_cart_button"
    android:showAsAction="ifRoom"
    android:title="Shopping Cart" />
<item
    android:id="@+id/action_notifications"
    android:actionLayout="@layout/action_bar_notifications_button"
    android:showAsAction="ifRoom"
    android:title="Notifications" />
<item
    android:id="@+id/menu_overflow"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_overflow"
    android:showAsAction="always"
    android:title="OverFlow">

    <menu>
        <item
            android:id="@+id/action_my_account"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="My Account" />
        <item
            android:id="@+id/action_current_orders"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="My Orders" />
        <item
            android:id="@+id/action_wish_list"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="My Wishlist" />
        <item
            android:id="@+id/action_contact_us"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Contact Us" />
        <item
            android:id="@+id/action_logout"
            android:orderInCategory="100"
            android:showAsAction="never"
            android:title="Logout" />
    </menu>
</item>

I want the View "menu_overflow" how can I get that?

I tried Following way :

Activity.java code

public boolean onCreateOptionsMenu(Menu menu) {

      final MenuItem item = menu.findItem(R.id.menu_overflow);
      View view = (View) findViewById(R.id.menu_overflow);
      new MaterialShowcaseView.Builder(this)
                        .setTarget(mOverFlowIcon)
                        .setRadius(10)
                        .setMaskColour(Color.argb(150, 0, 0, 0))
                        .setContentText("Find Your Wishlist Here") // optional but starting animations immediately in onCreate can make them choppy
                        .setDismissOnTouch(true)
                        .show();
    return super.onCreateOptionsMenu(menu);
}

But is returning view is null.

Please help...thanx

Sandeep Mane
  • 223
  • 1
  • 2
  • 9

4 Answers4

13

Are you inflating the menu in onCreateOptionsMenu? It appears that when onCreateOptionsMenu is executed, the xml inflation is added to the event queue, rather than inflating immediately. This is what causes your null pointer when you execute findViewById(). Try putting your view lookup inside a Handler. The code within the Handler is added to the message queue and will therefore be executed after the menu is inflated. This will resolve your null pointer. It will look something like this:

@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);
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            View view = (View) findViewById(R.id.menu_overflow);
            new MaterialShowcaseView.Builder(this)
                    .setTarget(view)
                    .setRadius(10)
                    .setMaskColour(Color.argb(150, 0, 0, 0))
                    .setContentText("Find Your Wishlist Here") // optional but starting animations immediately in onCreate can make them choppy
                    .setDismissOnTouch(true)
                    .show();
        }
    });
    return true;
}
schrieveslaach
  • 1,689
  • 1
  • 15
  • 32
davehenry
  • 1,026
  • 9
  • 24
5

You can get a reference to the MenuItem by overriding the onPrepareOptionsMenu(Menu) method:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem actionViewItem = menu.findItem(R.id.your_menu_item);
    return super.onPrepareOptionsMenu(menu);
}

If you want a reference to the ActionView you can do this with the method MenuItemCompat.getActionView(MenuItem):

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem actionViewItem = menu.findItem(R.id.your_menu_item);
    View v = MenuItemCompat.getActionView(actionViewItem);
    return super.onPrepareOptionsMenu(menu);
}
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
  • 1
    `MenuItemCompat.getActionView` is deprecated. `MenuItem.getActionView` should be used instead. – fdermishin Apr 02 '18 at 20:51
  • @fdermishin you mean `View v = actionViewItem.getActionView();` don't you? x – Mr Heelis Sep 16 '19 at 09:31
  • In some occasions while the menu item is already there, the action view turns out to be still null, as it seems to first add the menu, then inflate the UI. Any tricks on how to wait for the UI? – Sarah Multitasker Mar 27 '23 at 13:35
1

Try with:

<item
    android:id="@+id/menu_overflow"
    android:orderInCategory="100"
    android:icon="@drawable/ic_action_overflow"
    android:title="OverFlow"
    app:showAsAction="always"
    app:actionViewClass="android.widget.ImageButton">

Note this lines:

    app:showAsAction="always"
    app:actionViewClass="android.widget.ImageButton"
Rafa0809
  • 1,733
  • 21
  • 24
0

Did you try this?

@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);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        switch (item.getItemId()){
            case R.id.action_shopping_cart:
                Toast.makeText(this, "action_shopping_cart", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_notifications:
                Toast.makeText(this, "action_notifications", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_overflow:
                Toast.makeText(this, "menu_overflow", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_my_account:
                Toast.makeText(this, "action_my_account", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_current_orders:
                Toast.makeText(this, "action_current_orders", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_wish_list:
                Toast.makeText(this, "action_wish_list", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_contact_us:
                Toast.makeText(this, "action_contact_us", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_logout:
                Toast.makeText(this, "action_logout", Toast.LENGTH_SHORT).show();
                break;



        }



        return super.onOptionsItemSelected(item);
    }

Take care of the name of your xml (that is home.xml)

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
  • 1
    Thanks for the reply...I want the menu_overflow view on onOptionsCreate. I am showcasing tutorial for overflow icon, thats why I want view of over Flow menu..Do you know how to get that? – Sandeep Mane Oct 24 '15 at 06:12