michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.menu; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.Color; michael@0: import android.graphics.drawable.ColorDrawable; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.widget.LinearLayout; michael@0: import android.widget.PopupWindow; michael@0: michael@0: /** michael@0: * A popup to show the inflated MenuPanel. michael@0: */ michael@0: public class MenuPopup extends PopupWindow { michael@0: private LinearLayout mPanel; michael@0: michael@0: private int mYOffset; michael@0: private int mPopupWidth; michael@0: michael@0: public MenuPopup(Context context) { michael@0: super(context); michael@0: michael@0: setFocusable(true); michael@0: michael@0: mYOffset = context.getResources().getDimensionPixelSize(R.dimen.menu_popup_offset); michael@0: mPopupWidth = context.getResources().getDimensionPixelSize(R.dimen.menu_popup_width); michael@0: michael@0: // Setting a null background makes the popup to not close on touching outside. michael@0: setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); michael@0: setWindowLayoutMode(View.MeasureSpec.makeMeasureSpec(mPopupWidth, View.MeasureSpec.AT_MOST), michael@0: ViewGroup.LayoutParams.WRAP_CONTENT); michael@0: michael@0: LayoutInflater inflater = LayoutInflater.from(context); michael@0: mPanel = (LinearLayout) inflater.inflate(R.layout.menu_popup, null); michael@0: setContentView(mPanel); michael@0: michael@0: setAnimationStyle(R.style.PopupAnimation); michael@0: } michael@0: michael@0: /** michael@0: * Adds the panel with the menu to its content. michael@0: * michael@0: * @param view The panel view with the menu to be shown. michael@0: */ michael@0: public void setPanelView(View view) { michael@0: view.setLayoutParams(new LinearLayout.LayoutParams(mPopupWidth, michael@0: LinearLayout.LayoutParams.WRAP_CONTENT)); michael@0: michael@0: mPanel.removeAllViews(); michael@0: mPanel.addView(view); michael@0: } michael@0: michael@0: /** michael@0: * A small little offset. michael@0: */ michael@0: @Override michael@0: public void showAsDropDown(View anchor) { michael@0: showAsDropDown(anchor, 0, -mYOffset); michael@0: } michael@0: }