Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Options Menu Without ActionBar?

what I'm looking for is to make an options menu but without the ActionBar. In the Google music app I saw that they have a options menu sort of thing with no action bar. Below is a picture of what I was talking about in the Google music app.

Thank you in advance! :) googleplaymusicapp

like image 244
Tssomas Avatar asked Apr 10 '14 17:04

Tssomas


People also ask

How do I Hide and show a menu item in Android Actionbar?

This example demonstrates how do I hide and show a menu item in the Android ActionBar. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

What are Android option menus?

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute.

What is Actionbar in Android with example?

In Android, ac ActionBar or a TopBar is a UI element that is present at the top of the activity screen. An ActionBar by default displays the activity name inside it. However, we can add other elements like the back button, images, options menu, etc inside an ActionBar.

What is showasaction in Android option menus?

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute. The values that can be given for the showAsAction attribute:


1 Answers

That's just a simple popop. You can do that on any view. Throw an icon on a view, like the overflow menu icone and set a click listener on it.

This example is a list of devices (smartphones) in a catalog. I populate the tag with an object so I know which one the user clicks on.

public void showDeviceMenu(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    popup.inflate(R.menu.cart_device_menu);
    DeviceTag tag = (DeviceTag) v.getTag();
    final String groupId = tag.groupId;
    final String sku = tag.sku;
    final String productId = tag.productId;
    SpannableStringBuilder text = new SpannableStringBuilder(tag.name);

    text.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    popup.getMenu().findItem(R.id.menu_name).setTitle(text);
    invalidateOptionsMenu();
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.duplicate_device:
                    duplicateDevice(sku, productId);
                    return true;
                case R.id.update_device:
                    updateWirelessItemInCart(sku,groupId);
                    return true;
                case R.id.delete_device:
                    removeItemFromCart(groupId);
                    return true;
                default:
                    return false;
           }
        }

    });
    popup.show();
}
like image 100
Martin Avatar answered Sep 18 '22 18:09

Martin