Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get Action View Of Menu Item?

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

like image 637
Sandeep Mane Avatar asked Oct 23 '15 11:10

Sandeep Mane


Video Answer


2 Answers

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"
like image 127
Rafa0809 Avatar answered Sep 18 '22 17:09

Rafa0809


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);
}
like image 36
TouchBoarder Avatar answered Sep 21 '22 17:09

TouchBoarder