Android examples for User Interface:PopupMenu
show popwindow on clickview top, bottom ,right or left
import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; public class Main{ //from w w w .ja v a 2 s .c o m private static PopupWindow showPopWindow(Context context, View clickView, View contentView, int layoutId, int location) { PopupWindow mPopupWindows = new PopupWindow(context); View layout = null; if (contentView != null) { layout = contentView; } else if (layoutId > 0) { layout = LayoutInflater.from(context).inflate(layoutId, null); } else { throw new RuntimeException( "the contentView should not be null or the layoutId > 0 is request"); } mPopupWindows.setContentView(layout); mPopupWindows.setTouchable(true); mPopupWindows.setFocusable(true); mPopupWindows.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); mPopupWindows.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); mPopupWindows.setBackgroundDrawable(new BitmapDrawable()); if (location == 0) { // bottom mPopupWindows.showAsDropDown(clickView); } else if (location == 1) { // top int[] local = new int[2]; clickView.getLocationOnScreen(local); mPopupWindows.showAtLocation(clickView, Gravity.NO_GRAVITY, local[0], local[1] - mPopupWindows.getContentView() .getMeasuredHeight()); } else if (location == 2) { // left int[] local = new int[2]; clickView.getLocationOnScreen(local); mPopupWindows.showAtLocation(clickView, Gravity.NO_GRAVITY, local[0] - mPopupWindows.getContentView() .getMeasuredWidth(), local[1]); } else if (location == 3) { // right int[] local = new int[2]; clickView.getLocationOnScreen(local); mPopupWindows.showAtLocation(clickView, Gravity.NO_GRAVITY, local[0] + clickView.getMeasuredWidth(), local[1]); } return mPopupWindows; } }