Android Open Source - UltraExplorer Popup Menu






From Project

Back to project page UltraExplorer.

License

The source code is released under:

GNU General Public License

If you think the Android project UltraExplorer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mirrorlabs.menupopup;
/*w w  w.  jav  a2  s . c o m*/
import java.util.ArrayList;
import java.util.List;

import com.mirrorlabs.filebrowser.FilebrowserULTRAActivity.Fonts;
import com.mirrorlabs.filebrowser.R;



import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopupMenu {

    private Context mContext;
    private LayoutInflater mInflater;
    private WindowManager mWindowManager;

    private PopupWindow mPopupWindow;
    private View mContentView;
    private ListView mItemsView;
    private TextView mHeaderTitleView;
    private ImageView mHeaderImageView;
    private OnItemSelectedListener mListener;
    private float screen_density;
      
    private List<MenuItem> mItems;
    private int mWidth = 240;
    private float mScale;

    public PopupMenu(Context context) {
      
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(metrics);
        mScale = metrics.scaledDensity;
        screen_density = context.getResources().getDisplayMetrics().density;
        mItems = new ArrayList<MenuItem>();

        mPopupWindow = new PopupWindow(context);
        mPopupWindow.setTouchInterceptor(new OnTouchListener() {

            
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    mPopupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });

        setContentView(mInflater.inflate(R.layout.popup_menu, null));
    }

    /**
     * Sets the popup's content.
     *
     * @param contentView
     */
    private void setContentView(View contentView) {
        mContentView = contentView;
        
        mItemsView = (ListView) contentView.findViewById(R.id.items);
        mHeaderTitleView = (TextView) contentView.findViewById(R.id.header_title);
        mHeaderTitleView.setTypeface(Fonts.ICS);
        mHeaderImageView =(ImageView)contentView.findViewById(R.id.header_icon);
        mPopupWindow.setContentView(contentView);
    }

    /**
     * Add menu item.
     *
     * @param itemId
     * @param titleRes
     * @param iconRes
     *
     * @return item
     */
    public MenuItem add(int itemId, int titleRes) {
        MenuItem item = new MenuItem();
        item.setItemId(itemId);
        item.setTitle(mContext.getString(titleRes));
        mItems.add(item);

        return item;
    }

    /**
     * Show popup menu.
     */
    public void show() {
        show(null);
    }

    /**
     * Show popup menu.
     *
     * @param anchor
     */
    public void show(View anchor) {

        if (mItems.size() == 0) {
            throw new IllegalStateException("PopupMenu#add was not called with a menu item to display.");
        }

        preShow();

        MenuItemAdapter adapter = new MenuItemAdapter(mContext, mItems);
        mItemsView.setAdapter(adapter);
        mItemsView.setOnItemClickListener(new OnItemClickListener() {

            
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                if (mListener != null) {
                    mListener.onItemSelected(mItems.get(position));
                }
                mPopupWindow.dismiss();
            }
        });

        if (anchor == null) {
            View parent = ((Activity)mContext).getWindow().getDecorView();
            mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
            return;
        }

        int xPos, yPos;
        int[] location = new int[2];
        anchor.getLocationOnScreen(location);

        Rect anchorRect = new Rect(location[0], location[1],
                location[0] + anchor.getWidth(),
                location[0] + anchor.getHeight());

        mContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mContentView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        int rootHeight = mContentView.getMeasuredHeight();
        mWindowManager.getDefaultDisplay().getHeight();

        // Set x-coordinate to display the popup menu
       // xPos = anchorRect.centerX() - mPopupWindow.getWidth() / 2;
          xPos = scalePX(120);
          yPos = scalePX(120);
        
        //yPos =(screenHeight/2)-(rootHeight+20);;

        /*
        int dyTop = anchorRect.top;
        int dyBottom = screenHeight + rootHeight;
        boolean onTop = dyTop > dyBottom;

        // Set y-coordinate to display the popup menu
        if (onTop) {
            yPos = anchorRect.top - rootHeight;
        } else {
            if (anchorRect.bottom > dyTop) {
                yPos = anchorRect.bottom - 20;
            } else {
                yPos = anchorRect.top - anchorRect.bottom ;
            }
        } 
            */
        mPopupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos); 
    }

    private void preShow() {
        int width = (int) (mWidth * mScale);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
       
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        
       
        mPopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.panel_background));
    }

    /**
     * Dismiss the popup menu.
     */
    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }
    
    private int scalePX(int dp_size) {
    int px_size = (int) (dp_size * screen_density + 0.5f);
    return px_size;
  }

    /**
     * Sets the popup menu header's title.
     *
     * @param title
     */
    public void setHeaderTitle(CharSequence title) {
        mHeaderTitleView.setText(title);
        mHeaderTitleView.setVisibility(View.VISIBLE);
        mHeaderTitleView.requestFocus();
    }
    
    public void setHeaderIcon(Drawable image ) {
        mHeaderImageView.setImageDrawable(image);
        mHeaderImageView.setVisibility(View.VISIBLE);
        mHeaderImageView.requestFocus();
    }

    /**
     * Change the popup's width.
     *
     * @param width
     */
    public void setWidth(int width) {
        mWidth = width;
    }

    /**
     * Register a callback to be invoked when an item in this PopupMenu has
     * been selected.
     *
     * @param listener
     */
    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        mListener = listener;
    }

    /**
     * Interface definition for a callback to be invoked when
     * an item in this PopupMenu has been selected.
     */
    public interface OnItemSelectedListener {
        public void onItemSelected(MenuItem item);
    }

    static class ViewHolder {
        ImageView icon;
        TextView title;
        
    }

    private class MenuItemAdapter extends ArrayAdapter<MenuItem> {

        public MenuItemAdapter(Context context, List<MenuItem> objects) {
            super(context, 0, objects);
            
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.popupmenu_list_item, null);
                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                holder.title = (TextView) convertView.findViewById(R.id.title);
                holder.title.setTypeface(Fonts.ICS);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            MenuItem item = getItem(position);
            if (item.getIcon() != null) {
                holder.icon.setImageDrawable(item.getIcon());
                holder.icon.setVisibility(View.VISIBLE);
            } else {
                holder.icon.setVisibility(View.GONE);
            }
            holder.title.setText(item.getTitle());

            return convertView;
        }
    }

  
  
}




Java Source Code List

com.android.gestures.CreateGestureActivity.java
com.android.gestures.GestureBuilderActivity.java
com.android.gestures.GestureMonitorActivity.java
com.markupartist.android.widget.ActionBar.java
com.markupartist.android.widget.ScrollingTextView.java
com.mirrorlabs.customcheckboxwidget.DontPressWithParentCheckBox.java
com.mirrorlabs.customtoast.Toaster.java
com.mirrorlabs.filebrowser.BackupManager.java
com.mirrorlabs.filebrowser.BaseActivity.java
com.mirrorlabs.filebrowser.BitmapManager.java
com.mirrorlabs.filebrowser.BookmarksProvider.java
com.mirrorlabs.filebrowser.CMDProcessor.java
com.mirrorlabs.filebrowser.CompressManager.java
com.mirrorlabs.filebrowser.DesEncrypter.java
com.mirrorlabs.filebrowser.DrawableManager.java
com.mirrorlabs.filebrowser.DrawableThreadLoader.java
com.mirrorlabs.filebrowser.DuplicatesManager.java
com.mirrorlabs.filebrowser.ExtractManager.java
com.mirrorlabs.filebrowser.FileUtils.java
com.mirrorlabs.filebrowser.FilebrowserULTRAActivity.java
com.mirrorlabs.filebrowser.ImageThreadLoader.java
com.mirrorlabs.filebrowser.LinuxShell.java
com.mirrorlabs.filebrowser.MimeTypes.java
com.mirrorlabs.filebrowser.MyApplication.java
com.mirrorlabs.filebrowser.PDFViewer.java
com.mirrorlabs.filebrowser.PreferenceActivity.java
com.mirrorlabs.filebrowser.ProcessManager.java
com.mirrorlabs.filebrowser.RootUtils.java
com.mirrorlabs.filebrowser.SearchFilesDialog.java
com.mirrorlabs.filebrowser.SearchFilesWidget.java
com.mirrorlabs.filebrowser.UltraBaseAdapter.java
com.mirrorlabs.imageviewer.EclairMotionEvent.java
com.mirrorlabs.imageviewer.ExpandImage.java
com.mirrorlabs.imageviewer.TouchImageView.java
com.mirrorlabs.imageviewer.WrapMotionEvent.java
com.mirrorlabs.menupopup.MenuItem.java
com.mirrorlabs.menupopup.PopupMenu.java
com.mirrorlabs.musicplayer.DBHelper.java
com.mirrorlabs.musicplayer.PlayerActivity.java
com.mirrorlabs.quickaction3D.ActionItem.java
com.mirrorlabs.quickaction3D.PopupWindows.java
com.mirrorlabs.quickaction3D.QuickAction.java
com.mirrorlabs.quickaction.ActionItem.java
com.mirrorlabs.quickaction.PopupWindows.java
com.mirrorlabs.quickaction.QuickAction.java
com.mirrorlabs.ui.widgets.ClickableSlidingDrawer.java
com.mirrorlabs.ui.widgets.DropDownMenu.java
com.mirrorlabs.ui.widgets.IcsListPopupWindow.java
com.mirrorlabs.ui.widgets.JavaYoutubeDownloader.java
com.mirrorlabs.ui.widgets.Panel.java
com.mirrorlabs.ui.widgets.ScrollPager.java
com.mirrorlabs.ui.widgets.SlidingFrameLayout.java
com.mirrorlabs.widgets.ExampleAppWidgetProvider1.java
com.mirrorlabs.widgets.ExampleAppWidgetProvider.java
com.mirrorlabs.widgets.UpdateWidgetService1.java
com.mirrorlabs.widgets.UpdateWidgetService.java
org.zeroxlab.widget.AnimationLayout.java
org.zeroxlab.widget.MyHorizontalScrollView.java