Example usage for android.support.v4.widget PopupMenuCompat getDragToOpenListener

List of usage examples for android.support.v4.widget PopupMenuCompat getDragToOpenListener

Introduction

In this page you can find the example usage for android.support.v4.widget PopupMenuCompat getDragToOpenListener.

Prototype

public static OnTouchListener getDragToOpenListener(Object obj) 

Source Link

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
 *///ww  w .  j ava2 s .  c  om
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:edu.usf.cutr.opentripplanner.android.fragments.DirectionListFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ImageButton btnDisplayMap = (ImageButton) header.findViewById(R.id.btnDisplayMap);
    ImageButton btnShareDirections = (ImageButton) header.findViewById(R.id.btnShareDirections);
    ImageButton btnAlarmDirections = (ImageButton) header.findViewById(R.id.btnAlarmDirections);
    final OtpFragment ofl = this.getFragmentListener();
    final DirectionListFragment dlf = this;
    OnClickListener oclDisplayDirection = new OnClickListener() {
        @Override//from  ww  w  .  j a  v  a  2  s .c  o m
        public void onClick(View arg0) {
            ofl.onSwitchedToMainFragment(dlf);
        }
    };
    btnDisplayMap.setOnClickListener(oclDisplayDirection);
    btnShareDirections.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            // create popup menu
            View menuItemView = getView().findViewById(R.id.btnShareDirections);
            PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
            popup.getMenuInflater().inflate(R.menu.share_menu, popup.getMenu());
            menuItemView.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(popup));

            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {

                    Intent itn = new Intent();
                    itn.setAction(Intent.ACTION_SEND);
                    itn.setType("text/plain");

                    // fill intend content based on chosen menu item
                    switch (item.getItemId()) {
                    case R.id.btnShareDirectionsShort:
                        itn.putExtra(Intent.EXTRA_TEXT, getDepartureArrivalHeaders(false));
                        break;
                    case R.id.btnShareDirectionsDetailed:
                        itn.putExtra(Intent.EXTRA_TEXT, getDepartureArrivalHeaders(true));
                        break;
                    default:
                        break;
                    }
                    startActivity(Intent.createChooser(itn, "Share via"));
                    return true;
                }
            });

            popup.show();
        }
    });
    btnAlarmDirections.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            // create popup menu
            View menuItemView = getView().findViewById(R.id.btnAlarmDirections);
            PopupMenu popup = new PopupMenu(getActivity(), menuItemView);
            popup.getMenuInflater().inflate(R.menu.alarm_menu, popup.getMenu());
            menuItemView.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(popup));

            //registering popup with OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {

                    switch (item.getItemId()) {
                    case R.id.btnAlarmDirectionsAlarm:
                        setAlarmItinerary();
                        break;
                    case R.id.btnAlarmDirectionsNotifications:
                        setNotificationsItinerary();
                        break;
                    case R.id.btnAlarmDirectionsCalendar:
                        setCalendarItinerary();
                        break;
                    default:
                        break;
                    }
                    return true;
                }
            });

            popup.show();
        }
    });

    fromHeader = (TextView) header.findViewById(R.id.fromHeader);
    toHeader = (TextView) header.findViewById(R.id.toHeader);
    departureTimeHeader = (TextView) header.findViewById(R.id.departureTimeHeader);
    arrivalTimeHeader = (TextView) header.findViewById(R.id.arrivalTimeHeader);
    tripList = (Spinner) header.findViewById(R.id.itinerarySelection);

    if (savedInstanceState != null) {
        otpBundle = (OTPBundle) savedInstanceState.getSerializable(OTPApp.BUNDLE_KEY_OTP_BUNDLE);
        fragmentListener.setOTPBundle(otpBundle);
    } else {
        otpBundle = fragmentListener.getOTPBundle();
    }

    fromHeader.setText(otpBundle.getFromText());
    toHeader.setText(otpBundle.getToText());
    setDepartureArrivalHeaders();

    ArrayList<Leg> currentItinerary = new ArrayList<Leg>();
    currentItinerary.addAll(fragmentListener.getCurrentItinerary());
    ArrayList<Itinerary> itineraryList = new ArrayList<Itinerary>();
    itineraryList.addAll(fragmentListener.getCurrentItineraryList());
    int currentItineraryIndex = fragmentListener.getCurrentItineraryIndex();

    ArrayList<Direction> directions = new ArrayList<Direction>();
    DirectionsGenerator dirGen = new DirectionsGenerator(currentItinerary,
            getActivity().getApplicationContext());
    ArrayList<Direction> tempDirections = dirGen.getDirections();
    if (tempDirections != null && !tempDirections.isEmpty()) {
        directions.addAll(tempDirections);
    }

    final Activity activity = this.getActivity();
    String[] itinerarySummaryList = new String[itineraryList.size()];

    boolean isTransitIsTagSet = false;
    for (int i = 0; i < itinerarySummaryList.length; i++) {
        isTransitIsTagSet = false;
        Itinerary it = itineraryList.get(i);
        for (Leg leg : it.legs) {
            TraverseMode traverseMode = TraverseMode.valueOf(leg.mode);
            if (traverseMode.isTransit()) {
                itinerarySummaryList[i] = ConversionUtils.getRouteShortNameSafe(leg.routeShortName,
                        leg.routeLongName, getActivity().getApplicationContext()) + ". ";
                isTransitIsTagSet = true;
                break;
            }
        }
        if (!isTransitIsTagSet) {
            itinerarySummaryList[i] = Integer.toString(i + 1) + ".   ";//Shown index is i + 1, to use 1-based indexes for the UI instead of 0-based
        }
    }

    for (int i = 0; i < itinerarySummaryList.length; i++) {
        Itinerary it = itineraryList.get(i);
        long tripDuration;
        if (PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext())
                .getInt(OTPApp.PREFERENCE_KEY_API_VERSION, OTPApp.API_VERSION_V1) == OTPApp.API_VERSION_V1) {
            tripDuration = it.duration;
        } else {
            tripDuration = it.duration / 1000;
        }
        itinerarySummaryList[i] += getString(R.string.step_by_step_total_duration) + " " + ConversionUtils
                .getFormattedDurationTextNoSeconds(tripDuration, false, getActivity().getApplicationContext());
        if (isTransitIsTagSet) {
            itinerarySummaryList[i] += "   " + getString(R.string.step_by_step_walking_duration) + " "
                    + ConversionUtils.getFormattedDurationTextNoSeconds(it.walkTime, false,
                            getActivity().getApplicationContext());
        }
    }

    ArrayAdapter<String> itineraryAdapter = new ArrayAdapter<String>(activity,
            android.R.layout.simple_spinner_item, itinerarySummaryList);

    itineraryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    tripList.setAdapter(itineraryAdapter);

    AdapterView.OnItemSelectedListener itinerarySpinnerListener = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (fragmentListener.getCurrentItineraryIndex() != position) {
                fragmentListener.onItinerarySelected(position, 3);
            }

            setDepartureArrivalHeaders();

            if (!isFragmentFirstLoad) {
                ArrayList<Direction> directions = new ArrayList<Direction>();
                DirectionsGenerator dirGen = new DirectionsGenerator(fragmentListener.getCurrentItinerary(),
                        getActivity().getApplicationContext());
                ArrayList<Direction> tempDirections = dirGen.getDirections();
                if (tempDirections != null && !tempDirections.isEmpty()) {
                    directions.addAll(tempDirections);
                }

                Direction direction_data[] = directions.toArray(new Direction[directions.size()]);

                DirectionExpandableListAdapter adapter = new DirectionExpandableListAdapter(
                        DirectionListFragment.this.getActivity(), R.layout.list_direction_item,
                        R.layout.list_subdirection_item, direction_data);

                elv.setAdapter(adapter);

            }
            openIfNonTransit();

            isFragmentFirstLoad = false;

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
    tripList.setSelection(currentItineraryIndex);
    tripList.setOnItemSelectedListener(itinerarySpinnerListener);

    // Populate list with our static array of titles.
    elv = getExpandableListView();

    Direction direction_data[] = directions.toArray(new Direction[directions.size()]);

    DirectionExpandableListAdapter adapter = new DirectionExpandableListAdapter(this.getActivity(),
            R.layout.list_direction_item, R.layout.list_subdirection_item, direction_data);

    elv.addHeaderView(header);

    elv.setAdapter(adapter);

    elv.setGroupIndicator(null); // Get rid of the down arrow

    openIfNonTransit();

    if (savedInstanceState == null) {
        if (otpBundle.isFromInfoWindow()) {
            elv.expandGroup(otpBundle.getCurrentStepIndex());
            elv.setSelectedGroup(otpBundle.getCurrentStepIndex());
            otpBundle.setFromInfoWindow(false);
        }
    }
}

From source file:com.audiokernel.euphonyrmt.MainMenuActivity.java

private PopupMenu initializeHeaderOverflowPopup(final View headerOverflowMenu) {
    final PopupMenu resultPopupMenu;

    if (headerOverflowMenu == null) {
        resultPopupMenu = null;//ww w. j a  v a 2s  .  com
    } else {
        final PopupMenu popupMenu = new PopupMenu(this, headerOverflowMenu);
        popupMenu.getMenuInflater().inflate(R.menu.mpd_mainmenu, popupMenu.getMenu());
        popupMenu.getMenuInflater().inflate(R.menu.mpd_playlistmenu, popupMenu.getMenu());
        popupMenu.getMenu().removeItem(R.id.PLM_EditPL);
        popupMenu.setOnMenuItemClickListener(this);

        headerOverflowMenu.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(popupMenu));

        headerOverflowMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mSlidingLayout != null && mSlidingLayout.isPanelExpanded()) {
                    prepareNowPlayingMenu(popupMenu.getMenu());
                    popupMenu.show();
                }
            }
        });

        resultPopupMenu = popupMenu;
    }

    return resultPopupMenu;
}

From source file:com.audiokernel.euphonyrmt.fragments.SongsFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.songs, container, false);
    mList = (AbsListView) view.findViewById(R.id.list);
    registerForContextMenu(mList);//from  w  ww  .ja  v a2s .  c  o m
    mList.setOnItemClickListener(this);

    mLoadingView = view.findViewById(R.id.loadingLayout);
    mLoadingTextView = (TextView) view.findViewById(R.id.loadingText);
    mNoResultView = view.findViewById(R.id.noResultLayout);
    mLoadingTextView.setText(getLoadingText());

    final View headerView = inflater.inflate(R.layout.song_header, null, false);
    mCoverArt = (ImageView) view.findViewById(R.id.albumCover);
    if (mCoverArt != null) {
        mHeaderArtist = (TextView) view.findViewById(R.id.tracks_artist);
        mHeaderInfo = (TextView) view.findViewById(R.id.tracks_info);
        mCoverArtProgress = (ProgressBar) view.findViewById(R.id.albumCoverProgress);
        mAlbumMenu = (ImageButton) view.findViewById(R.id.album_menu);
    } else {
        mHeaderArtist = (TextView) headerView.findViewById(R.id.tracks_artist);
        mHeaderInfo = (TextView) headerView.findViewById(R.id.tracks_info);
        mCoverArt = (ImageView) headerView.findViewById(R.id.albumCover);
        mCoverArtProgress = (ProgressBar) headerView.findViewById(R.id.albumCoverProgress);
        mAlbumMenu = (ImageButton) headerView.findViewById(R.id.album_menu);
    }

    mCoverArtListener = new AlbumCoverDownloadListener(mCoverArt, mCoverArtProgress, false);
    mCoverHelper = new CoverAsyncHelper();
    mCoverHelper.setCoverMaxSizeFromScreen(getActivity());
    final ViewTreeObserver vto = mCoverArt.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (mCoverHelper != null) {
                mCoverHelper.setCachedCoverMaxSize(mCoverArt.getMeasuredHeight());
            }
            return true;
        }
    });
    mCoverHelper.addCoverDownloadListener(mCoverArtListener);

    ((TextView) headerView.findViewById(R.id.separator_title)).setText(R.string.songs);
    ((ListView) mList).addHeaderView(headerView, null, false);

    mPopupMenu = new PopupMenu(getActivity(), mAlbumMenu);
    mPopupMenu.getMenu().add(Menu.NONE, ADD, Menu.NONE, R.string.addAlbum);
    mPopupMenu.getMenu().add(Menu.NONE, ADD_REPLACE, Menu.NONE, R.string.addAndReplace);
    mPopupMenu.getMenu().add(Menu.NONE, ADD_REPLACE_PLAY, Menu.NONE, R.string.addAndReplacePlay);
    mPopupMenu.getMenu().add(Menu.NONE, ADD_PLAY, Menu.NONE, R.string.addAndPlay);
    mPopupMenu.getMenu().add(Menu.NONE, GOTO_ARTIST, Menu.NONE, R.string.goToArtist);

    mPopupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(final MenuItem item) {
            final int itemId = item.getItemId();
            if (itemId == GOTO_ARTIST) {
                final Intent intent = new Intent(getActivity(), SimpleLibraryActivity.class);
                intent.putExtra("artist", mAlbum.getArtist());
                startActivityForResult(intent, -1);
            } else {
                mApp.oMPDAsyncHelper.execAsync(new Runnable() {
                    @Override
                    public void run() {
                        boolean replace = false;
                        boolean play = false;
                        switch (itemId) {
                        case ADD_REPLACE_PLAY:
                            replace = true;
                            play = true;
                            break;
                        case ADD_REPLACE:
                            replace = true;
                            break;
                        case ADD_PLAY:
                            play = true;
                            break;
                        default:
                            break;
                        }
                        try {
                            mApp.oMPDAsyncHelper.oMPD.add(mAlbum, replace, play);
                            Tools.notifyUser(R.string.albumAdded, mAlbum);
                        } catch (final IOException | MPDException e) {
                            Log.e(TAG, "Failed to add, replace, play.", e);
                        }
                    }
                });
            }
            return true;
        }
    });

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

    mCoverPopupMenu = new PopupMenu(getActivity(), mCoverArt);
    mCoverPopupMenu.getMenu().add(POPUP_COVER_BLACKLIST, POPUP_COVER_BLACKLIST, 0, R.string.otherCover);
    mCoverPopupMenu.getMenu().add(POPUP_COVER_SELECTIVE_CLEAN, POPUP_COVER_SELECTIVE_CLEAN, 0,
            R.string.resetCover);
    mCoverPopupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(final MenuItem item) {
            final int groupId = item.getGroupId();
            boolean result = true;

            if (groupId == POPUP_COVER_BLACKLIST) {
                final AlbumInfo albumInfo = new AlbumInfo(mAlbum);

                CoverManager.getInstance().markWrongCover(albumInfo);
                updateCover(albumInfo);
                updateNowPlayingSmallFragment(albumInfo);
            } else if (groupId == POPUP_COVER_SELECTIVE_CLEAN) {
                final AlbumInfo albumInfo = new AlbumInfo(mAlbum);

                CoverManager.getInstance().clear(albumInfo);
                updateCover(albumInfo);
                updateNowPlayingSmallFragment(albumInfo);
            } else {
                result = false;
            }

            return result;
        }
    });

    mCoverArt.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            mCoverPopupMenu.show();
            return false;
        }
    });

    updateFromItems();

    return view;
}

From source file:com.audiokernel.euphonyrmt.fragments.NowPlayingFragment.java

/**
 * Run during fragment initialization, this sets up the song info popup menu.
 *
 * @param view The view in which to setup the song info View for this class.
 * @return The song info view used as a field in this class.
 *//*from   w ww.ja v a  2  s  .co  m*/
private View getSongInfo(final View view) {
    final View songInfo = view.findViewById(R.id.songInfo);

    final PopupMenu popupMenu = new PopupMenu(mActivity, songInfo);
    final Menu menu = popupMenu.getMenu();
    menu.add(Menu.NONE, POPUP_ALBUM, Menu.NONE, R.string.goToAlbum);
    menu.add(Menu.NONE, POPUP_ARTIST, Menu.NONE, R.string.goToArtist);
    menu.add(Menu.NONE, POPUP_ALBUM_ARTIST, Menu.NONE, R.string.goToAlbumArtist);
    menu.add(Menu.NONE, POPUP_FOLDER, Menu.NONE, R.string.goToFolder);
    menu.add(Menu.NONE, POPUP_CURRENT, Menu.NONE, R.string.goToCurrent);
    menu.add(Menu.NONE, POPUP_SHARE, Menu.NONE, R.string.share);
    popupMenu.setOnMenuItemClickListener(this);
    mPopupMenuTouchListener = PopupMenuCompat.getDragToOpenListener(popupMenu);

    final PopupMenu popupMenuStream = new PopupMenu(mActivity, songInfo);
    final Menu menuStream = popupMenuStream.getMenu();
    menuStream.add(Menu.NONE, POPUP_STREAM, Menu.NONE, R.string.goToStream);
    menuStream.add(Menu.NONE, POPUP_CURRENT, Menu.NONE, R.string.goToCurrent);
    menuStream.add(Menu.NONE, POPUP_SHARE, Menu.NONE, R.string.share);
    popupMenuStream.setOnMenuItemClickListener(this);
    mPopupMenuStreamTouchListener = PopupMenuCompat.getDragToOpenListener(popupMenuStream);

    songInfo.setOnClickListener(new OnClickListener() {

        /**
         * Checks whether the album artist should be on the popup menu for the current track.
         *
         * @return True if the album artist popup menu entry should be visible, false otherwise.
         */
        private boolean isAlbumArtistVisible() {
            boolean albumArtistEnabled = false;
            final String albumArtist = mCurrentSong.getAlbumArtist();

            if (albumArtist != null && !albumArtist.isEmpty()) {
                final String artist = mCurrentSong.getArtist();

                if (isArtistVisible() && !albumArtist.equals(artist)) {
                    albumArtistEnabled = true;
                }
            }

            return albumArtistEnabled;
        }

        /**
         * Checks whether the album should be on the popup menu for the current track.
         *
         * @return True if the album popup menu entry should be visible, false otherwise.
         */
        private boolean isAlbumVisible() {
            final boolean isAlbumVisible;
            final String album = mCurrentSong.getAlbum();

            if (album != null && !album.isEmpty()) {
                isAlbumVisible = true;
            } else {
                isAlbumVisible = false;
            }

            return isAlbumVisible;
        }

        /**
         * Checks whether the artist should be on the popup menu for the current track.
         *
         * @return True if the artist popup menu entry should be visible, false otherwise.
         */
        private boolean isArtistVisible() {
            final boolean isArtistVisible;
            final String artist = mCurrentSong.getArtist();

            if (artist != null && !artist.isEmpty()) {
                isArtistVisible = true;
            } else {
                isArtistVisible = false;
            }

            return isArtistVisible;
        }

        /**
         * This method checks the dynamic entries for visibility prior to showing the song info
         * popup menu.
         *
         * @param v The view for the song info popup menu.
         */
        @Override
        public void onClick(final View v) {
            if (mCurrentSong != null) {
                if (mCurrentSong.isStream()) {
                    popupMenuStream.show();
                } else {
                    // Enable / Disable menu items that need artist and album defined.
                    menu.findItem(POPUP_ALBUM).setVisible(isAlbumVisible());
                    menu.findItem(POPUP_ARTIST).setVisible(isArtistVisible());
                    menu.findItem(POPUP_ALBUM_ARTIST).setVisible(isAlbumArtistVisible());
                    popupMenu.show();
                }
            }
        }
    });

    return songInfo;
}