Java Code Examples for android.widget.PopupWindow#setWidth()

The following examples show how to use android.widget.PopupWindow#setWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViemoPlayerMenu.java    From Android-VimeoPlayer with MIT License 6 votes vote down vote up
@NonNull
private PopupWindow createPopupWindow() {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(inflater == null)
        throw new RuntimeException("can't access LAYOUT_INFLATER_SERVICE");

    View view = inflater.inflate(R.layout.player_menu, null);

    RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
    setUpRecyclerView(recyclerView);

    PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

    return popupWindow;
}
 
Example 2
Source File: InsertionHandleController.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public PastePopupMenu() {
    mContainer = new PopupWindow(mContext, null,
            android.R.attr.textSelectHandleWindowStyle);
    mContainer.setSplitTouchEnabled(true);
    mContainer.setClippingEnabled(false);

    mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

    final int[] POPUP_LAYOUT_ATTRS = {
        android.R.attr.textEditPasteWindowLayout,
        android.R.attr.textEditNoPasteWindowLayout,
        android.R.attr.textEditSidePasteWindowLayout,
        android.R.attr.textEditSideNoPasteWindowLayout,
    };

    mPasteViews = new View[POPUP_LAYOUT_ATTRS.length];
    mPasteViewLayouts = new int[POPUP_LAYOUT_ATTRS.length];

    TypedArray attrs = mContext.obtainStyledAttributes(POPUP_LAYOUT_ATTRS);
    for (int i = 0; i < attrs.length(); ++i) {
        mPasteViewLayouts[i] = attrs.getResourceId(attrs.getIndex(i), 0);
    }
    attrs.recycle();
}
 
Example 3
Source File: SelectRemindWayPopup.java    From Android-AlarmManagerClock with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public SelectRemindWayPopup(Context context) {
    mContext = context;
    mPopupWindow = new PopupWindow(context);
    mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    mPopupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
    mPopupWindow.setHeight(WindowManager.LayoutParams.FILL_PARENT);
    mPopupWindow.setTouchable(true);
    mPopupWindow.setFocusable(true);
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setAnimationStyle(R.style.AnimBottom);
    mPopupWindow.setContentView(initViews());

    mPopupWindow.getContentView().setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mPopupWindow.setFocusable(false);
            mPopupWindow.dismiss();
            return true;
        }
    });

}
 
Example 4
Source File: EditTextWithCustomError.java    From WidgyWidgets with Apache License 2.0 6 votes vote down vote up
private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
	int wid = tv.getPaddingLeft() + tv.getPaddingRight();
	int ht = tv.getPaddingTop() + tv.getPaddingBottom();

	/*
	 * Figure out how big the text would be if we laid it out to the
	 * full width of this view minus the border.
	 */
	int cap = getWidth() - wid;
	if (cap < 0) {
		cap = 200; // We must not be measured yet -- setFrame() will fix it.
	}

	Layout l = new StaticLayout(text, tv.getPaint(), cap,
			Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
	float max = 0;
	for (int i = 0; i < l.getLineCount(); i++) {
		max = Math.max(max, l.getLineWidth(i));
	}

	/*
	 * Now set the popup size to be big enough for the text plus the border.
	 */
	pop.setWidth(wid + (int) Math.ceil(max));
	pop.setHeight(ht + l.getHeight());
}
 
Example 5
Source File: SelectRemindCyclePopup.java    From Android-AlarmManagerClock with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public SelectRemindCyclePopup(Context context) {
    mContext = context;
    mPopupWindow = new PopupWindow(context);
    mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    mPopupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
    mPopupWindow.setHeight(WindowManager.LayoutParams.FILL_PARENT);
    mPopupWindow.setTouchable(true);
    mPopupWindow.setFocusable(true);
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setAnimationStyle(R.style.AnimBottom);
    mPopupWindow.setContentView(initViews());
    mPopupWindow.getContentView().setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mPopupWindow.setFocusable(false);
            // mPopupWindow.dismiss();
            return true;
        }
    });

}
 
Example 6
Source File: MainActivity.java    From qingyang with Apache License 2.0 6 votes vote down vote up
/**
 * 显示菜单
 * 
 * @param x
 * @param y
 */
public void showPopMenu(int x, int y) {
	LinearLayout layout = (LinearLayout) LayoutInflater.from(
			MainActivity.this).inflate(R.layout.pop_menu, null);
	ListView listView = (ListView) layout.findViewById(R.id.menu_list);
	listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
			R.layout.pop_menu_item, R.id.tv_text, title));

	popupWindow = new PopupWindow(MainActivity.this);
	popupWindow.setBackgroundDrawable(new BitmapDrawable());
	if (application.isTablet()) {
		popupWindow.setWidth(screenWidth / 4);
	} else {
		popupWindow.setWidth(screenWidth / 3);
	}
	popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
	popupWindow.setOutsideTouchable(true);
	popupWindow.setFocusable(true);
	popupWindow.setContentView(layout);
	// 需要指定Gravity,默认情况是center
	popupWindow.showAtLocation(view, Gravity.RIGHT | Gravity.TOP, x, y);
	listView.setOnItemClickListener(new MenuItemClick());
}
 
Example 7
Source File: BootstrapDropDown.java    From Android-Bootstrap with MIT License 6 votes vote down vote up
private void createDropDown() {
    ScrollView dropdownView = createDropDownView();
    dropdownWindow = new PopupWindow();
    dropdownWindow.setFocusable(true);
    dropdownWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

    if (!isInEditMode()) {
        dropdownWindow.setBackgroundDrawable(DrawableUtils.resolveDrawable(android.R.drawable
                                                                                   .dialog_holo_light_frame, getContext()));
    }

    dropdownWindow.setContentView(dropdownView);
    dropdownWindow.setOnDismissListener(this);
    dropdownWindow.setAnimationStyle(android.R.style.Animation_Activity);
    float longestStringWidth = measureStringWidth(getLongestString(dropdownData))
            + DimenUtils.dpToPixels((baselineItemRightPadding + baselineItemLeftPadding) * bootstrapSize);

    if (longestStringWidth < getMeasuredWidth()) {
        dropdownWindow.setWidth(DimenUtils.dpToPixels(getMeasuredWidth()));
    }
    else {
        dropdownWindow.setWidth((int) longestStringWidth + DimenUtils.dpToPixels(8));
    }
}
 
Example 8
Source File: ShoppingDetailActivity.java    From ShoppingCartActivity with Apache License 2.0 6 votes vote down vote up
/**
 * 弹出悬浮窗体,显示菜单
 */
private void popuWindowDialog() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
    LinearLayout linMessage= (LinearLayout) layout.findViewById(R.id.popuMessage);
    LinearLayout linMain= (LinearLayout) layout.findViewById(R.id.popuMain);
    LinearLayout linShare= (LinearLayout) layout.findViewById(R.id.popuShare);

    linMessage.setOnClickListener(this);
    linMain.setOnClickListener(this);
    linShare.setOnClickListener(this);
    pwMyPopWindow = new PopupWindow(layout);
    pwMyPopWindow.setFocusable(true);
    // 加上这个popupwindow中的ListView才可以接收点击事件
    // 控制popupwindow的宽度和高度自适应
    pwMyPopWindow.setWidth(400);
    pwMyPopWindow.setHeight(300);
    // 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上
    pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.bg_popupwindow));
    // 控制popupwindow点击屏幕其他地方消失
    pwMyPopWindow.setOutsideTouchable(true);
}
 
Example 9
Source File: LordFragment.java    From MyHearts with Apache License 2.0 5 votes vote down vote up
private void initPopup() {
        View popupWindow = LayoutInflater.from(getContext())
                .inflate(R.layout.lord_popup_window_layout, null);

        mReAddGroup = (RelativeLayout) popupWindow.findViewById(R.id.re_add_group);
        mReAddGroup.setOnClickListener(this);
        mReSearchGroup = (RelativeLayout) popupWindow.findViewById(R.id.re_search_group);
        mReSearchGroup.setOnClickListener(this);
        mPopupWindow = new PopupWindow(popupWindow);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getContext().getResources()));

        mLlDismiss = (LinearLayout) popupWindow.findViewById(R.id.ll_dismiss);
        mLlDismiss.setOnClickListener(view -> {
            mPopupWindow.dismiss();
        });

//        // 设置背景颜色变暗
//        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
//        lp.alpha = 0.7f;
//        getActivity().getWindow().setAttributes(lp);
//        mPopupWindow.setOnDismissListener(() -> {
//            WindowManager.LayoutParams lp1 = getActivity().getWindow().getAttributes();
//            lp1.alpha = 1f;
//            getActivity().getWindow().setAttributes(lp1);
//        });
    }
 
Example 10
Source File: PopupShowAsDropDownActivity.java    From AndroidSamples with Apache License 2.0 5 votes vote down vote up
private void showPopupWindow() {
    View contentView = LayoutInflater.from(PopupShowAsDropDownActivity.this).inflate(R.layout.show_as_drop_down_activity_popup, null);
    mPopWindow = new PopupWindow(contentView);
    mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

    TextView tv1 = contentView.findViewById(R.id.pop_computer);
    TextView tv2 = contentView.findViewById(R.id.pop_financial);
    TextView tv3 = contentView.findViewById(R.id.pop_manage);
    tv1.setOnClickListener(this);
    tv2.setOnClickListener(this);
    tv3.setOnClickListener(this);

    mPopWindow.showAsDropDown(mMenuTv);
}
 
Example 11
Source File: BasePop.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
private void initPop()
{
    mPopupWindow = new PopupWindow(mContext);
    mPopupWindow.setFocusable(setFocusable());
    boolean outsideTouchacle = setOutsideTouchable();
    mPopupWindow.setOutsideTouchable(outsideTouchacle);
    mPopupWindow.setBackgroundDrawable(new ShapeDrawable());
    mPopupWindow.setWidth(setLayoutWidthParams());
    mPopupWindow.setHeight(setLayoutHeightParams());
    int animStyle = setAnimStyle();
    if (animStyle != 0)
        mPopupWindow.setAnimationStyle(animStyle);
    //设置内容布局
    mContentView = mContext.getLayoutInflater().inflate(setContentViewId()
            , (ViewGroup) mContext.findViewById(android.R.id.content), false);
    mContentView.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
    mPopupWindow.setContentView(mContentView);
    //设置点击外部关闭pop
    if (outsideTouchacle)
        mPopupWindow.setTouchInterceptor(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    mPopupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
}
 
Example 12
Source File: AutoRunCommandListEditText.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private void init() {
    mCommandAdapter = new CommandListSuggestionsAdapter(getContext());
    mCommandAdapter.setClickListener(this);
    List<CommandAliasManager.CommandAlias> additionalItems = new ArrayList<>();
    additionalItems.add(CommandAliasManager.CommandAlias.raw("wait", "<seconds>", ""));
    additionalItems.add(CommandAliasManager.CommandAlias.raw("wait-for", "<channel>", ""));
    mCommandAdapter.setAdditionalItems(additionalItems);

    mSuggestionsList = new RecyclerView(getContext());
    mSuggestionsList.setAdapter(mCommandAdapter);
    mSuggestionsList.setLayoutManager(new LinearLayoutManager(getContext()));

    mPopupAnchor = new View(getContext());

    mPopupWindow = new PopupWindow(getContext(), null, android.R.attr.listPopupWindowStyle);
    mPopupWindow.setContentView(mSuggestionsList);
    mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

    mPopupItemHeight = StyledAttributesHelper.getDimensionPixelSize(getContext(),
            android.R.attr.listPreferredItemHeightSmall, 0);
    mMaxPopupHeight = getResources().getDimensionPixelSize(R.dimen.list_popup_max_height);

    addTextChangedListener(new SimpleTextWatcher((Editable s) -> {
        if (enoughToFilter())
            performFiltering(false);
        else
            dismissDropDown();
    }));
}
 
Example 13
Source File: CallActivity.java    From sealrtc-android with MIT License 5 votes vote down vote up
private void showPopupWindowList(PopupWindow popupWindow, View view) {
    popupWindow.setOutsideTouchable(true);
    popupWindow.setWidth(getResources().getDimensionPixelSize(R.dimen.popup_width));
    popupWindow.setHeight(getResources().getDimensionPixelSize(R.dimen.popup_width));
    int[] location = new int[2];
    view.getLocationInWindow(location);
    int x = location[0] - popupWindow.getWidth();
    int y = location[1] + view.getHeight() / 2 - popupWindow.getHeight() / 2;
    popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
}
 
Example 14
Source File: PlayerFragment.java    From edx-app-android with Apache License 2.0 4 votes vote down vote up
private void showSettingsPopup(final Point p) {
    try{
        if(player!=null){
            player.getController().setAutoHide(!getTouchExploreEnabled());
            Activity context = getActivity();

            float popupHeight =  getResources().getDimension(R.dimen.settings_popup_height);
            float popupWidth =  getResources().getDimension(R.dimen.settings_popup_width);

            // Inflate the popup_layout.xml
            LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.setting_popup);
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.panel_settings_popup, viewGroup);

            // Creating the PopupWindow
            settingPopup = new PopupWindow(context);
            settingPopup.setContentView(layout);
            settingPopup.setWidth((int)popupWidth);
            settingPopup.setHeight((int)popupHeight);
            settingPopup.setFocusable(true);
            settingPopup.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    hideTransparentImage();
                    if(player!=null){
                        player.getController().setSettingsBtnDrawable(false);
                        player.getController().setAutoHide(!getTouchExploreEnabled());
                    }
                }
            });

            // Clear the default translucent background
            settingPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            // Displaying the popup at the specified location, + offsets.
            settingPopup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x-(int)popupWidth, p.y-(int)popupHeight);

            TextView tv_closedCaption = (TextView) layout.findViewById(R.id.tv_closedcaption);
            if ((langList != null) && (langList.size() > 0))
            {
                tv_closedCaption.setBackgroundResource(R.drawable.white_rounded_selector);
                tv_closedCaption.setOnClickListener(new View.OnClickListener(){
                    public void onClick(View paramAnonymousView) {
                        showCCFragmentPopup();
                    }
                });
            }else{
                tv_closedCaption.setBackgroundResource(R.drawable.grey_roundedbg);
                tv_closedCaption.setOnClickListener(null);
            }

            layout.findViewById(R.id.tv_video_speed).setOnClickListener(v -> showVideoSpeedFragmentPopup());
        }
    }catch(Exception e){
        logger.error(e);
    }
}
 
Example 15
Source File: EmojiPopup.java    From hipda with GNU General Public License v2.0 4 votes vote down vote up
private EmojiPopup(final View rootView, final EmojiEditText emojiEditText, @Nullable final RecentEmoji recent) {
    this.context = rootView.getContext();
    this.rootView = rootView;
    this.emojiEditText = emojiEditText;
    this.recentEmoji = recent != null ? recent : new RecentEmojiManager(context);
    this.keyBoardHeight = getPreferences().getInt(PREF_KEYBOARD_HEIGHT, 0);

    popupWindow = new PopupWindow(context);
    popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null)); // To avoid borders & overdraw

    final EmojiView emojiView = new EmojiView(context, new OnEmojiClickedListener() {
        @Override
        public void onEmojiClicked(final Emoji emoji) {
            emojiEditText.input(emoji);
            recentEmoji.addEmoji(emoji);

            if (onEmojiClickedListener != null) {
                onEmojiClickedListener.onEmojiClicked(emoji);
            }
        }
    }, this.recentEmoji);

    emojiView.setOnEmojiBackspaceClickListener(new OnEmojiBackspaceClickListener() {
        @Override
        public void onEmojiBackspaceClicked(final View v) {
            emojiEditText.backspace();

            if (onEmojiBackspaceClickListener != null) {
                onEmojiBackspaceClickListener.onEmojiBackspaceClicked(v);
            }
        }
    });

    popupWindow.setContentView(emojiView);
    popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
    popupWindow.setHeight((int) context.getResources().getDimension(R.dimen.emoji_keyboard_height));
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            if (onEmojiPopupDismissListener != null) {
                onEmojiPopupDismissListener.onEmojiPopupDismiss();
            }
        }
    });

    setupListener();
}
 
Example 16
Source File: AnalyzerViews.java    From audio-analyzer-for-android with Apache License 2.0 4 votes vote down vote up
private PopupWindow popupMenuCreate(String[] popUpContents, int resId) {

        // initialize a pop up window type
        PopupWindow popupWindow = new PopupWindow(activity);

        // the drop down list is a list view
        ListView listView = new ListView(activity);

        // set our adapter and pass our pop up window contents
        ArrayAdapter<String> aa = popupMenuAdapter(popUpContents);
        listView.setAdapter(aa);

        // set the item click listener
        listView.setOnItemClickListener(activity);

        // button resource ID, so we can trace back which button is pressed
        listView.setTag(resId);

        // get max text width
        Paint mTestPaint = new Paint();
        mTestPaint.setTextSize(listItemTextSize);
        float w = 0;  // max text width in pixel
        float wi;
        for (String popUpContent : popUpContents) {
            String sts[] = popUpContent.split("::");
            if (sts.length == 0) continue;
            String st = sts[0];
            if (sts.length == 2 && sts[1].equals("0")) {
                mTestPaint.setTextSize(listItemTitleTextSize);
                wi = mTestPaint.measureText(st);
                mTestPaint.setTextSize(listItemTextSize);
            } else {
                wi = mTestPaint.measureText(st);
            }
            if (w < wi) {
                w = wi;
            }
        }

        // left and right padding, at least +7, or the whole app will stop respond, don't know why
        w = w + 23 * DPRatio;
        if (w < 40 * DPRatio) {
            w = 40 * DPRatio;
        }

        // some other visual settings
        popupWindow.setFocusable(true);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // Set window width according to max text width
        popupWindow.setWidth((int)w);
        // also set button width
        ((Button) activity.findViewById(resId)).setWidth((int)(w + 5 * DPRatio));
        // Set the text on button in loadPreferenceForView()

        // set the list view as pop up window content
        popupWindow.setContentView(listView);

        return popupWindow;
    }
 
Example 17
Source File: CustomKeyboardView.java    From FirefoxReality with Mozilla Public License 2.0 4 votes vote down vote up
private void showKey(final int keyIndex) {
    final PopupWindow previewPopup = mPreviewPopup;
    final Key[] keys = mKeys;
    if (keyIndex < 0 || keyIndex >= mKeys.length) return;
    Key key = keys[keyIndex];
    if (key.icon != null) {
        mPreviewText.setCompoundDrawables(null, null, null,
                key.iconPreview != null ? key.iconPreview : key.icon);
        mPreviewText.setText(null);
    } else {
        mPreviewText.setCompoundDrawables(null, null, null, null);
        mPreviewText.setText(getPreviewText(key));
        if (key.label.length() > 1 && key.codes.length < 2) {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
            mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
            mPreviewText.setTypeface(Typeface.DEFAULT);
        }
    }
    mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
            + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
    final int popupHeight = mPreviewHeight;
    LayoutParams lp = mPreviewText.getLayoutParams();
    if (lp != null) {
        lp.width = popupWidth;
        lp.height = popupHeight;
    }
    if (!mPreviewCentered) {
        mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + getPaddingLeft();
        mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
    } else {
        // TODO: Fix this if centering is brought back
        mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
        mPopupPreviewY = - mPreviewText.getMeasuredHeight();
    }
    mHandler.removeMessages(MSG_REMOVE_PREVIEW);
    getLocationInWindow(mCoordinates);
    mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
    mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero

    // Set the preview background state
    mPreviewText.getBackground().setState(
            key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    mPopupPreviewX += mCoordinates[0];
    mPopupPreviewY += mCoordinates[1];

    // If the popup cannot be shown above the key, put it on the side
    getLocationOnScreen(mCoordinates);
    if (mPopupPreviewY + mCoordinates[1] < 0) {
        // If the key you're pressing is on the left side of the keyboard, show the popup on
        // the right, offset by enough to see at least one key to the left/right.
        if (key.x + key.width <= getWidth() / 2) {
            mPopupPreviewX += (int) (key.width * 2.5);
        } else {
            mPopupPreviewX -= (int) (key.width * 2.5);
        }
        mPopupPreviewY += popupHeight;
    }

    if (previewPopup.isShowing()) {
        previewPopup.update(mPopupPreviewX, mPopupPreviewY,
                popupWidth, popupHeight);
    } else {
        previewPopup.setWidth(popupWidth);
        previewPopup.setHeight(popupHeight);
        previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
                mPopupPreviewX, mPopupPreviewY);
    }
    mPreviewText.setVisibility(VISIBLE);
}
 
Example 18
Source File: MyKeyboardView.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private void showKey(final int keyIndex) {
    final PopupWindow previewPopup = mPreviewPopup;
    final Key[] keys = mKeys;
    if (keyIndex < 0 || keyIndex >= mKeys.length) return;
    Key key = keys[keyIndex];
    if (key.icon != null) {
        mPreviewText.setCompoundDrawables(null, null, null,
                key.iconPreview != null ? key.iconPreview : key.icon);
        mPreviewText.setText(null);
    } else {
        mPreviewText.setCompoundDrawables(null, null, null, null);
        mPreviewText.setText(getPreviewText(key));
        if (key.label.length() > 1 && key.codes.length < 2) {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
            mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
            mPreviewText.setTypeface(Typeface.DEFAULT);
        }
    }
    mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
            + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
    final int popupHeight = mPreviewHeight;
    LayoutParams lp = mPreviewText.getLayoutParams();
    if (lp != null) {
        lp.width = popupWidth;
        lp.height = popupHeight;
    }
    if (!mPreviewCentered) {
        mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
        mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
    } else {
        // TODO: Fix this if centering is brought back
        mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
        mPopupPreviewY = -mPreviewText.getMeasuredHeight();
    }
    mHandler.removeMessages(MSG_REMOVE_PREVIEW);
    getLocationInWindow(mCoordinates);
    mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
    mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero

    // Set the preview background state
    mPreviewText.getBackground().setState(
            key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    mPopupPreviewX += mCoordinates[0];
    mPopupPreviewY += mCoordinates[1];

    // If the popup cannot be shown above the key, put it on the side
    getLocationOnScreen(mCoordinates);
    if (mPopupPreviewY + mCoordinates[1] < 0) {
        // If the key you're pressing is on the left side of the keyboard, show the popup on
        // the right, offset by enough to see at least one key to the left/right.
        if (key.x + key.width <= getWidth() / 2) {
            mPopupPreviewX += (int) (key.width * 2.5);
        } else {
            mPopupPreviewX -= (int) (key.width * 2.5);
        }
        mPopupPreviewY += popupHeight;
    }

    if (previewPopup.isShowing()) {
        previewPopup.update(mPopupPreviewX, mPopupPreviewY,
                popupWidth, popupHeight);
    } else {
        previewPopup.setWidth(popupWidth);
        previewPopup.setHeight(popupHeight);
        previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
                mPopupPreviewX, mPopupPreviewY);
    }
    mPreviewText.setVisibility(VISIBLE);
}
 
Example 19
Source File: KeyboardView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void showKey(final int keyIndex) {
    final PopupWindow previewPopup = mPreviewPopup;
    final Key[] keys = mKeys;
    if (keyIndex < 0 || keyIndex >= mKeys.length) return;
    Key key = keys[keyIndex];
    if (key.icon != null) {
        mPreviewText.setCompoundDrawables(null, null, null,
                key.iconPreview != null ? key.iconPreview : key.icon);
        mPreviewText.setText(null);
    } else {
        mPreviewText.setCompoundDrawables(null, null, null, null);
        mPreviewText.setText(getPreviewText(key));
        if (key.label.length() > 1 && key.codes.length < 2) {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
            mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
            mPreviewText.setTypeface(Typeface.DEFAULT);
        }
    }
    mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
            + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
    final int popupHeight = mPreviewHeight;
    LayoutParams lp = mPreviewText.getLayoutParams();
    if (lp != null) {
        lp.width = popupWidth;
        lp.height = popupHeight;
    }
    if (!mPreviewCentered) {
        mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
        mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
    } else {
        // TODO: Fix this if centering is brought back
        mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
        mPopupPreviewY = - mPreviewText.getMeasuredHeight();
    }
    mHandler.removeMessages(MSG_REMOVE_PREVIEW);
    getLocationInWindow(mCoordinates);
    mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
    mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero

    // Set the preview background state
    mPreviewText.getBackground().setState(
            key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
    mPopupPreviewX += mCoordinates[0];
    mPopupPreviewY += mCoordinates[1];

    // If the popup cannot be shown above the key, put it on the side
    getLocationOnScreen(mCoordinates);
    if (mPopupPreviewY + mCoordinates[1] < 0) {
        // If the key you're pressing is on the left side of the keyboard, show the popup on
        // the right, offset by enough to see at least one key to the left/right.
        if (key.x + key.width <= getWidth() / 2) {
            mPopupPreviewX += (int) (key.width * 2.5);
        } else {
            mPopupPreviewX -= (int) (key.width * 2.5);
        }
        mPopupPreviewY += popupHeight;
    }

    if (previewPopup.isShowing()) {
        previewPopup.update(mPopupPreviewX, mPopupPreviewY,
                popupWidth, popupHeight);
    } else {
        previewPopup.setWidth(popupWidth);
        previewPopup.setHeight(popupHeight);
        previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
                mPopupPreviewX, mPopupPreviewY);
    }
    mPreviewText.setVisibility(VISIBLE);
}
 
Example 20
Source File: WXMask.java    From incubator-weex-playground with Apache License 2.0 4 votes vote down vote up
@Override
protected View initComponentHostView(@NonNull Context context) {

  mContainerView = new WXMaskView(context);
  mPopupWindow = new PopupWindow(context);
  mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    mPopupWindow.setAttachedInDecor(true);
  }

  //setClippingEnabled(false) will cause INPUT_ADJUST_PAN invalid.
  //mPopupWindow.setClippingEnabled(false);

  mPopupWindow.setContentView(mContainerView);
  mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
  mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN |
      WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
  mPopupWindow.setFocusable(true);

  mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
    @Override
    public void onDismiss() {
      fireVisibleChangedEvent(false);
    }
  });

  int y = 0;
  int statusBarHeight = 0;
  int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
    statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
    y = statusBarHeight;
  }

  mPopupWindow.showAtLocation(((Activity) context).getWindow().getDecorView(),
      Gravity.TOP | Gravity.START,
      0,
      y);
  fireVisibleChangedEvent(true);

  return mContainerView;
}