Example usage for android.widget PopupMenu PopupMenu

List of usage examples for android.widget PopupMenu PopupMenu

Introduction

In this page you can find the example usage for android.widget PopupMenu PopupMenu.

Prototype

public PopupMenu(Context context, View anchor) 

Source Link

Document

Constructor to create a new popup menu with an anchor view.

Usage

From source file:app.umitems.greenclock.DeskClockFragment.java

/**
 * Installs click and touch listeners on a fake overflow menu button.
 *
 * @param menuButton the fragment's fake overflow menu button
 *//*from  www.  j a va 2  s. c  o  m*/
public void setupFakeOverflowMenuButton(View menuButton) {
    final PopupMenu fakeOverflow = new PopupMenu(menuButton.getContext(), menuButton) {
        @Override
        public void show() {
            getActivity().onPrepareOptionsMenu(getMenu());
            super.show();
        }
    };
    fakeOverflow.inflate(R.menu.desk_clock_menu);
    fakeOverflow.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return getActivity().onOptionsItemSelected(item);
        }
    });

    menuButton.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(fakeOverflow));
    menuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fakeOverflow.show();
        }
    });
}

From source file:MainActivity.java

public void showPopupMenu(View view) {
    PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
    popupMenu.inflate(R.menu.menu_popup);
    popupMenu.setOnMenuItemClickListener(mOnMenuItemClickListener);
    popupMenu.show();/*www .  j  a  v  a2 s .c o  m*/
}

From source file:com.siva.animation_sample.HomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    //getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setDisplayShowCustomEnabled(true);
    getActionBar().setCustomView(R.layout.custom_action_bar_layout);
    custom_action_view = getActionBar().getCustomView();
    ImageView more_icon = (ImageView) custom_action_view.findViewById(R.id.moreimage);
    more_icon.setOnClickListener(new View.OnClickListener() {

        @Override/*from w w  w. j  a v a2s .  c  om*/
        public void onClick(View v) {
            // TODO Auto-generated method stub
            PopupMenu popup = new PopupMenu(HomeActivity.this, v);
            MenuInflater inflate = popup.getMenuInflater();
            inflate.inflate(R.menu.home, popup.getMenu());
            popup.show();
            popup.setOnMenuItemClickListener(HomeActivity.this);

        }
    });

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    Create_alert_box();

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),
            FragmentTab1.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),
            FragmentTab2.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),
            FragmentTab3.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab4").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),
            FragmentTab4.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab5").setIndicator(null, getResources().getDrawable(R.drawable.ic_launcher)),
            FragmentTab5.class, null);

    mTabHost.setCurrentTab(2);

}

From source file:com.battlelancer.seriesguide.ui.MoviesWatchListFragment.java

@Override
public void onPopupMenuClick(View v, final int movieTmdbId) {
    PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
    popupMenu.getMenu().add(0, CONTEXT_WATCHLIST_REMOVE_ID, 0, R.string.watchlist_remove);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override/*from   w  ww  .  j av a 2 s. c  o m*/
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case CONTEXT_WATCHLIST_REMOVE_ID: {
                MovieTools.removeFromWatchlist(getActivity(), movieTmdbId);
                fireTrackerEvent("Remove from watchlist");
                return true;
            }
            }
            return false;
        }
    });
    popupMenu.show();
}

From source file:com.battlelancer.seriesguide.ui.MoviesCollectionFragment.java

@Override
public void onPopupMenuClick(View v, final int movieTmdbId) {
    PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
    popupMenu.getMenu().add(0, CONTEXT_COLLECTION_REMOVE_ID, 0, R.string.action_collection_remove);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override//from w  w  w .ja  v  a  2  s  . c  o  m
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case CONTEXT_COLLECTION_REMOVE_ID: {
                MovieTools.removeFromCollection(getActivity(), movieTmdbId);
                fireTrackerEvent("Remove from collection");
                return true;
            }
            }
            return false;
        }
    });
    popupMenu.show();
}

From source file:org.y20k.transistor.helpers.StationContextMenu.java

public void show() {

    PopupMenu popup = new PopupMenu(mActivity, mView);
    popup.inflate(R.menu.menu_main_list_item);
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override/*from w w w  .j av a2 s . co m*/
        public boolean onMenuItemClick(MenuItem item) {

            switch (item.getItemId()) {

            // CASE ICON
            case R.id.menu_icon:

                // send local broadcast (needed by MainActivityFragment)
                Intent i = new Intent();
                i.setAction(ACTION_IMAGE_CHANGE_REQUESTED);
                i.putExtra(STATION_ID, mStationID);
                LocalBroadcastManager.getInstance(mActivity.getApplication()).sendBroadcast(i);

                return true;

            // CASE RENAME
            case R.id.menu_rename:
                // get name of station
                String stationName = mCollection.getStations().get(mStationID).getStationName();
                // construct rename dialog
                DialogRename dialogRename = new DialogRename(mActivity, mCollection, stationName, mStationID);
                dialogRename.setStationRenamedListener(new DialogRename.StationRenamedListener() {
                    @Override
                    public void stationRenamed() {
                        if (mStationChangedListener != null) {
                            mStationChangedListener.stationChanged();
                        }
                    }
                });
                // run dialog
                dialogRename.show();
                return true;

            // CASE DELETE
            case R.id.menu_delete:
                // construct delete dialog
                DialogDelete dialogDelete = new DialogDelete(mActivity, mCollection, mStationID);
                // run dialog
                dialogDelete.show();
                return true;

            // CASE DEFAULT
            default:
                return false;
            }
        }
    });
    popup.show();
}

From source file:org.dmfs.webcal.NavbarActivity.java

protected void setupNavbar() {
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // this is a hack to get a Menu
    Menu menu = new PopupMenu(this, null).getMenu();
    getMenuInflater().inflate(R.menu.side_navigation, menu);

    // remove invisible menu items
    int itemCount = menu.size();
    int i = 0;/*from  w  w  w.  j  a  va2s.c om*/
    while (i < itemCount) {
        MenuItem item = menu.getItem(i);
        if (!item.isVisible()) {
            menu.removeItem(item.getItemId());
            --itemCount;
        } else {
            ++i;
        }
    }

    MenuAdapter menuAdapter = new MenuAdapter(this, R.layout.navdrawer_item, android.R.id.title, menu);
    mDrawerList.setAdapter(menuAdapter);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            String activityTitle = getActivityTitle();
            setTitle(activityTitle);
            invalidateOptionsMenu();
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            setTitle(R.string.app_name);
            invalidateOptionsMenu();
            super.onDrawerOpened(drawerView);
        }
    };
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(this);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

From source file:com.example.ray.firstapp.bottombar.MiscUtils.java

/**
 * A hacky method for inflating menus from xml resources to an array
 * of BottomBarTabs.//from w  w w . jav  a 2  s .c o m
 *
 * @param activity the activity context for retrieving the MenuInflater.
 * @param menuRes  the xml menu resource to inflate
 * @return an Array of BottomBarTabs.
 */
protected static BottomBarTab[] inflateMenuFromResource(Activity activity, @MenuRes int menuRes) {
    // A bit hacky, but hey hey what can I do
    PopupMenu popupMenu = new PopupMenu(activity, null);
    Menu menu = popupMenu.getMenu();
    activity.getMenuInflater().inflate(menuRes, menu);

    int menuSize = menu.size();
    BottomBarTab[] tabs = new BottomBarTab[menuSize];

    for (int i = 0; i < menuSize; i++) {
        MenuItem item = menu.getItem(i);
        BottomBarTab tab = new BottomBarTab(item.getIcon(), String.valueOf(item.getTitle()));
        tab.id = item.getItemId();
        tabs[i] = tab;
    }

    return tabs;
}

From source file:com.cyanogenmod.eleven.utils.PopupMenuHelper.java

/**
 * Call this to inflate and show the pop up menu
 * @param view the view to anchor the popup menu against
 * @param position the item that was clicked in the popup menu (or -1 if not relevant)
 *//*from   w w w.j a v  a  2  s.co m*/
public void showPopupMenu(final View view, final int position) {
    // create the popup menu
    PopupMenu popupMenu = new PopupMenu(mActivity, view);
    final Menu menu = popupMenu.getMenu();

    // hook up the click listener
    popupMenu.setOnMenuItemClickListener(this);

    // figure what type of pop up menu it is
    mType = onPreparePopupMenu(position);
    if (mType != null) {
        // inflate the menu
        createPopupMenu(menu);
        // show it
        popupMenu.show();
    }
}

From source file:fr.outadev.splatcompanion.ActivityMain.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Instantiate fragments
    fragmentRegularBattles = new FragmentRegularBattles();
    fragmentRankedBattles = new FragmentRankedBattles();

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    // Setup view pager to display the different fragments
    viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);

    countdown = (TextView) findViewById(R.id.txt_countdown);
    progressSpinner = (ProgressBar) findViewById(R.id.progress_spinner);

    // Set up the overflow button, using a popup menu
    final ImageButton buttonOverflow = (ImageButton) findViewById(R.id.overflow_button);
    final PopupMenu menu = new PopupMenu(this, buttonOverflow);
    menu.inflate(R.menu.menu_main);//from w w  w  .  jav  a 2s .co  m

    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_item_refresh:
                (new FetchRotationSchedule()).execute(true);
                return true;
            case R.id.menu_item_about:
                startActivity(new Intent(ActivityMain.this, ActivityAbout.class));
                return true;
            default:
                return false;
            }
        }

    });

    buttonOverflow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            menu.show();
        }

    });
}