Example usage for android.graphics.drawable Drawable getPadding

List of usage examples for android.graphics.drawable Drawable getPadding

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable getPadding.

Prototype

public boolean getPadding(@NonNull Rect padding) 

Source Link

Document

Return in padding the insets suggested by this Drawable for placing content inside the drawable's bounds.

Usage

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

@Deprecated
private int getBackgroundBottomPadding() {
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        return mTempRect.bottom;
    }/*from   w w  w.  j  av a2 s . c o  m*/
    return 0;
}

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

@Deprecated
private int getBackgroundTopPadding() {
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        return mTempRect.top;
    }//  www .  java  2  s  .co m
    return 0;
}

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * Sets the width of the popup window by the size of its content. The final width may be
 * larger to accommodate styled window dressing.
 *
 * @param width Desired width of content in pixels.
 *//*  w  w w  .  j  a va2s  .c o m*/
public void setContentWidth(int width) {
    Drawable popupBackground = mPopup.getBackground();
    if (popupBackground != null) {
        popupBackground.getPadding(mTempRect);
        mDropDownWidth = mTempRect.left + mTempRect.right + width;
    } else {
        setWidth(width);
    }
}

From source file:com.github.shareme.gwsmaterialuikit.library.material.widget.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 */// w ww  .  jav  a  2 s.c o  m
@SuppressWarnings("Range")
private int buildDropDown() {
    int otherHeights = 0;

    if (mDropDownList == null) {
        ViewGroup dropDownView;
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Timber.e("Invalid hint position " + mPromptPosition);
                break;
            }

            // measure the hint's height to find how much more vertical space
            // we need to add to the drop down's height
            int widthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.AT_MOST);
            int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);

    } else {
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    int systemBarsReservedSpace = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        //  getMaxAvailableHeight() on Lollipop seems to ignore the system bars.
        systemBarsReservedSpace = Math.max(getSystemBarHeight("status_bar_height"),
                getSystemBarHeight("navigation_bar_height"));
    }

    // Max height available on the screen for a popup.
    boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = mPopup.getMaxAvailableHeight(getAnchorView(),
            mDropDownVerticalOffset /*, ignoreBottomDecorations*/) - systemBarsReservedSpace;

    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:android.support.v7ox.widget.ListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *///from w  w  w .ja  v a  2s  .c o  m
private int buildDropDown() {
    ViewGroup dropDownView;
    int otherHeights = 0;

    if (mDropDownList == null) {
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = new DropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    DropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.mListSelectionHidden = false;
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // Measure the hint's height to find how much more vertical
            // space we need to add to the drop down's height.
            final int widthSize;
            final int widthMode;
            if (mDropDownWidth >= 0) {
                widthMode = MeasureSpec.AT_MOST;
                widthSize = mDropDownWidth;
            } else {
                widthMode = MeasureSpec.UNSPECIFIED;
                widthSize = 0;
            }
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
            final int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);
    } else {
        dropDownView = (ViewGroup) mPopup.getContentView();
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        if (!mDropDownVerticalOffsetSet) {
            mDropDownVerticalOffset = -mTempRect.top;
        }
    } else {
        mTempRect.setEmpty();
    }

    // Max height available on the screen for a popup.
    final boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    final int maxHeight = getMaxAvailableHeight(getAnchorView(), mDropDownVerticalOffset,
            ignoreBottomDecorations);
    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(
                mContext.getResources().getDisplayMetrics().widthPixels - (mTempRect.left + mTempRect.right),
                MeasureSpec.EXACTLY);
        break;
    default:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            DropDownListView.NO_POSITION, maxHeight - otherHeights, -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (listContent > 0)
        otherHeights += padding;

    return listContent + otherHeights;
}

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * <p>Builds the popup window's content and returns the height the popup
 * should have. Returns -1 when the content already exists.</p>
 *
 * @return the content's height or -1 if content already exists
 *///from  w ww  .  ja  va  2s  .c o  m
private int buildDropDown() {
    ViewGroup dropDownView;
    int otherHeights = 0;

    if (mDropDownList == null) {
        Context context = mContext;

        /**
         * This Runnable exists for the sole purpose of checking if the view layout has got
         * completed and if so call showDropDown to display the drop down. This is used to show
         * the drop down as soon as possible after user opens up the search dialog, without
         * waiting for the normal UI pipeline to do it's job which is slower than this method.
         */
        mShowDropDownRunnable = new Runnable() {
            public void run() {
                // View layout should be all done before displaying the drop down.
                View view = getAnchorView();
                if (view != null && view.getWindowToken() != null) {
                    show();
                }
            }
        };

        mDropDownList = createDropDownListView(context, !mModal);
        if (mDropDownListHighlight != null) {
            mDropDownList.setSelector(mDropDownListHighlight);
        }
        mDropDownList.setAdapter(mAdapter);
        mDropDownList.setOnItemClickListener(mItemClickListener);
        mDropDownList.setFocusable(true);
        mDropDownList.setFocusableInTouchMode(true);
        mDropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position != -1) {
                    XpDropDownListView dropDownList = mDropDownList;

                    if (dropDownList != null) {
                        dropDownList.setListSelectionHidden(false);
                    }
                }
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mDropDownList.setOnScrollListener(mScrollListener);

        if (mItemSelectedListener != null) {
            mDropDownList.setOnItemSelectedListener(mItemSelectedListener);
        }

        dropDownView = mDropDownList;

        View hintView = mPromptView;
        if (hintView != null) {
            // if a hint has been specified, we accomodate more space for it and
            // add a text view in the drop down menu, at the bottom of the list
            LinearLayout hintContainer = new LinearLayout(context);
            hintContainer.setOrientation(LinearLayout.VERTICAL);

            LinearLayout.LayoutParams hintParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 0, 1.0f);

            switch (mPromptPosition) {
            case POSITION_PROMPT_BELOW:
                hintContainer.addView(dropDownView, hintParams);
                hintContainer.addView(hintView);
                break;

            case POSITION_PROMPT_ABOVE:
                hintContainer.addView(hintView);
                hintContainer.addView(dropDownView, hintParams);
                break;

            default:
                Log.e(TAG, "Invalid hint position " + mPromptPosition);
                break;
            }

            // Measure the hint's height to find how much more vertical
            // space we need to add to the drop down's height.
            final int widthSize;
            final int widthMode;
            if (mDropDownWidth >= 0) {
                widthMode = MeasureSpec.AT_MOST;
                widthSize = mDropDownWidth > mDropDownMaxWidth ? mDropDownMaxWidth : mDropDownWidth;
                //                    widthSize = mDropDownWidth;
            } else {
                if (mDropDownMaxWidth >= 0) {
                    widthMode = MeasureSpec.AT_MOST;
                    widthSize = mDropDownMaxWidth;
                } else {
                    widthMode = MeasureSpec.UNSPECIFIED;
                    widthSize = 0;
                }
            }
            //noinspection Range
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
            final int heightSpec = MeasureSpec.UNSPECIFIED;
            hintView.measure(widthSpec, heightSpec);

            hintParams = (LinearLayout.LayoutParams) hintView.getLayoutParams();
            otherHeights = hintView.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;

            dropDownView = hintContainer;
        }

        mPopup.setContentView(dropDownView);
    } else {
        dropDownView = (ViewGroup) mPopup.getContentView();
        final View view = mPromptView;
        if (view != null) {
            LinearLayout.LayoutParams hintParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            otherHeights = view.getMeasuredHeight() + hintParams.topMargin + hintParams.bottomMargin;
        }
    }

    // getMaxAvailableHeight() subtracts the padding, so we put it back
    // to get the available height for the whole window
    int padding = 0;
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(mTempRect);
        padding = mTempRect.top + mTempRect.bottom;

        // If we don't have an explicit vertical offset, determine one from the window
        // background so that content will line up.
        //            if (!mDropDownVerticalOffsetSet) {
        //                mDropDownVerticalOffset = -mTempRect.top;
        //            }
    } else {
        mTempRect.setEmpty();
    }

    final int verticalMargin = mMargins.top + mMargins.bottom;

    // Max height available on the screen for a popup.
    final boolean ignoreBottomDecorations = mPopup.getInputMethodMode() == PopupWindow.INPUT_METHOD_NOT_NEEDED;
    //        final int maxHeight = getMaxAvailableHeight(getAnchorView(), mDropDownVerticalOffset, ignoreBottomDecorations);
    final int maxHeight = getMaxAvailableHeight(getAnchorView(), ignoreBottomDecorations);
    if (mDropDownAlwaysVisible || mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
        return maxHeight - verticalMargin + padding;
    }

    final int childWidthSpec;
    switch (mDropDownWidth) {
    case ViewGroup.LayoutParams.WRAP_CONTENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(getAnchorView().getWidth()
                - (mMargins.left + mMargins.right) - (mTempRect.left + mTempRect.right), MeasureSpec.AT_MOST);
        break;
    case ViewGroup.LayoutParams.MATCH_PARENT:
        childWidthSpec = MeasureSpec.makeMeasureSpec(mContext.getResources().getDisplayMetrics().widthPixels
                - (mMargins.left + mMargins.right) - (mTempRect.left + mTempRect.right), MeasureSpec.EXACTLY);
        break;
    case PREFERRED:
        int widthSize;
        int widthMode;
        if (mDropDownMaxWidth >= 0) {
            widthSize = mDropDownMaxWidth - (mMargins.left + mMargins.right)
                    - (mTempRect.left + mTempRect.right);
            widthMode = MeasureSpec.AT_MOST;
            childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
        } else {
            widthMode = MeasureSpec.AT_MOST;
            if (mDropDownMaxWidth == WRAP_CONTENT) {
                widthSize = getAnchorView().getWidth() - (mMargins.left + mMargins.right)
                        - (mTempRect.left + mTempRect.right);
            } else { // MATCH_PARENT
                widthSize = mContext.getResources().getDisplayMetrics().widthPixels
                        - (mMargins.left + mMargins.right) - (mTempRect.left + mTempRect.right);
            }
            childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
        }
        break;
    default:
        //noinspection Range
        childWidthSpec = MeasureSpec.makeMeasureSpec(mDropDownWidth, MeasureSpec.EXACTLY);
        break;
    }

    final int listPadding = mDropDownList.getPaddingTop() + mDropDownList.getPaddingBottom();
    final int listContent = mDropDownList.measureHeightOfChildrenCompat(childWidthSpec, 0,
            XpDropDownListView.NO_POSITION, maxHeight - otherHeights - verticalMargin - listPadding + padding,
            -1);
    // add padding only if the list has items in it, that way we don't show
    // the popup if it is not needed
    if (otherHeights > 0 || listContent > 0)
        otherHeights += padding + listPadding;

    return listContent + otherHeights;
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardViewBase.java

protected void resetKeyboardTheme(@NonNull KeyboardTheme theme) {
    final int keyboardThemeStyleResId = getKeyboardStyleResId(theme);

    final int[] remoteKeyboardThemeStyleable = theme.getResourceMapping()
            .getRemoteStyleableArrayFromLocal(R.styleable.AnyKeyboardViewTheme);
    final int[] remoteKeyboardIconsThemeStyleable = theme.getResourceMapping()
            .getRemoteStyleableArrayFromLocal(R.styleable.AnyKeyboardViewIconsTheme);

    final int[] padding = new int[] { 0, 0, 0, 0 };

    int keyTypeFunctionAttrId = R.attr.key_type_function;
    int keyActionAttrId = R.attr.key_type_action;
    int keyActionTypeDoneAttrId = R.attr.action_done;
    int keyActionTypeSearchAttrId = R.attr.action_search;
    int keyActionTypeGoAttrId = R.attr.action_go;

    HashSet<Integer> doneLocalAttributeIds = new HashSet<>();
    TypedArray a = theme.getPackageContext().obtainStyledAttributes(keyboardThemeStyleResId,
            remoteKeyboardThemeStyleable);
    final int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        final int remoteIndex = a.getIndex(i);
        final int localAttrId = R.styleable.AnyKeyboardViewTheme[remoteIndex];
        if (setValueFromTheme(a, padding, localAttrId, remoteIndex)) {
            doneLocalAttributeIds.add(localAttrId);
            if (localAttrId == R.attr.keyBackground) {
                //keyTypeFunctionAttrId and keyActionAttrId are remote
                final int[] keyStateAttributes = theme.getResourceMapping()
                        .getRemoteStyleableArrayFromLocal(KEY_TYPES);
                keyTypeFunctionAttrId = keyStateAttributes[0];
                keyActionAttrId = keyStateAttributes[1];
            }/*from w w w  .j a v a  2 s .co m*/
        }
    }
    a.recycle();
    // taking icons
    int iconSetStyleRes = getKeyboardIconsStyleResId(theme);
    if (iconSetStyleRes != 0) {
        a = theme.getPackageContext().obtainStyledAttributes(iconSetStyleRes,
                remoteKeyboardIconsThemeStyleable);
        final int iconsCount = a.getIndexCount();
        for (int i = 0; i < iconsCount; i++) {
            final int remoteIndex = a.getIndex(i);
            final int localAttrId = R.styleable.AnyKeyboardViewIconsTheme[remoteIndex];
            if (setKeyIconValueFromTheme(theme, a, localAttrId, remoteIndex)) {
                doneLocalAttributeIds.add(localAttrId);
                if (localAttrId == R.attr.iconKeyAction) {
                    //keyActionTypeDoneAttrId and keyActionTypeSearchAttrId and keyActionTypeGoAttrId are remote
                    final int[] keyStateAttributes = theme.getResourceMapping()
                            .getRemoteStyleableArrayFromLocal(ACTION_KEY_TYPES);
                    keyActionTypeDoneAttrId = keyStateAttributes[0];
                    keyActionTypeSearchAttrId = keyStateAttributes[1];
                    keyActionTypeGoAttrId = keyStateAttributes[2];
                }
            }
        }
        a.recycle();
    }
    // filling what's missing
    KeyboardTheme fallbackTheme = KeyboardThemeFactory.getFallbackTheme(getContext().getApplicationContext());
    final int keyboardFallbackThemeStyleResId = getKeyboardStyleResId(fallbackTheme);
    a = fallbackTheme.getPackageContext().obtainStyledAttributes(keyboardFallbackThemeStyleResId,
            R.styleable.AnyKeyboardViewTheme);

    final int fallbackCount = a.getIndexCount();
    for (int i = 0; i < fallbackCount; i++) {
        final int index = a.getIndex(i);
        final int attrId = R.styleable.AnyKeyboardViewTheme[index];
        if (doneLocalAttributeIds.contains(attrId))
            continue;
        setValueFromTheme(a, padding, attrId, index);
    }
    a.recycle();
    // taking missing icons
    int fallbackIconSetStyleId = fallbackTheme.getIconsThemeResId();
    a = fallbackTheme.getPackageContext().obtainStyledAttributes(fallbackIconSetStyleId,
            R.styleable.AnyKeyboardViewIconsTheme);

    final int fallbackIconsCount = a.getIndexCount();
    for (int i = 0; i < fallbackIconsCount; i++) {
        final int index = a.getIndex(i);
        final int attrId = R.styleable.AnyKeyboardViewIconsTheme[index];
        if (doneLocalAttributeIds.contains(attrId))
            continue;
        setKeyIconValueFromTheme(fallbackTheme, a, attrId, index);
    }
    a.recycle();
    //creating the key-drawable state provider, as we suppose to have the entire data now
    mDrawableStatesProvider = new KeyDrawableStateProvider(keyTypeFunctionAttrId, keyActionAttrId,
            keyActionTypeDoneAttrId, keyActionTypeSearchAttrId, keyActionTypeGoAttrId);

    // settings.
    // don't forget that there are THREE padding,
    // the theme's and the
    // background image's padding and the
    // View
    Drawable keyboardBackground = super.getBackground();
    if (keyboardBackground != null) {
        Rect backgroundPadding = new Rect();
        keyboardBackground.getPadding(backgroundPadding);
        padding[0] += backgroundPadding.left;
        padding[1] += backgroundPadding.top;
        padding[2] += backgroundPadding.right;
        padding[3] += backgroundPadding.bottom;
    }
    setPadding(padding[0], padding[1], padding[2], padding[3]);

    final Resources res = getResources();
    final int viewWidth = (getWidth() > 0) ? getWidth() : res.getDisplayMetrics().widthPixels;
    mKeyboardDimens.setKeyboardMaxWidth(viewWidth - padding[0] - padding[2]);

    mPaint.setTextSize(mKeyTextSize);

    mKeyBackground.getPadding(mKeyBackgroundPadding);
}

From source file:com.actionbarsherlock.internal.widget.IcsSpinner.java

int measureContentWidth(SpinnerAdapter adapter, Drawable background) {
    if (adapter == null) {
        return 0;
    }/*from   w  ww.  java  2s  .c om*/

    int width = 0;
    View itemView = null;
    int itemType = 0;
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    // Make sure the number of items we'll measure is capped. If it's a huge data set
    // with wildly varying sizes, oh well.
    int start = Math.max(0, getSelectedItemPosition());
    final int end = Math.min(adapter.getCount(), start + MAX_ITEMS_MEASURED);
    final int count = end - start;
    start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
    for (int i = start; i < end; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }
        itemView = adapter.getView(i, itemView, this);
        if (itemView.getLayoutParams() == null) {
            itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        width = Math.max(width, itemView.getMeasuredWidth());
    }

    // Add background padding to measured width
    if (background != null) {
        background.getPadding(mTempRect);
        width += mTempRect.left + mTempRect.right;
    }

    return width;
}

From source file:android.support.v7.widget.AppCompatSpinner.java

private int compatMeasureContentWidth(SpinnerAdapter adapter, Drawable background) {
    if (adapter == null) {
        return 0;
    }/*from ww w .j a  v a  2 s .c  o  m*/

    int width = 0;
    View itemView = null;
    int itemType = 0;
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.UNSPECIFIED);

    // Make sure the number of items we'll measure is capped. If it's a huge data set
    // with wildly varying sizes, oh well.
    int start = Math.max(0, getSelectedItemPosition());
    final int end = Math.min(adapter.getCount(), start + MAX_ITEMS_MEASURED);
    final int count = end - start;
    start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
    for (int i = start; i < end; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }
        itemView = adapter.getView(i, itemView, this);
        if (itemView.getLayoutParams() == null) {
            itemView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        width = Math.max(width, itemView.getMeasuredWidth());
    }

    // Add background padding to measured width
    if (background != null) {
        background.getPadding(mTempRect);
        width += mTempRect.left + mTempRect.right;
    }

    return width;
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java

public AnyKeyboardBaseView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    //creating the KeyDrawableStateProvider, as it suppose to be backward compatible
    int keyTypeFunctionAttrId = R.attr.key_type_function;
    int keyActionAttrId = R.attr.key_type_action;
    int keyActionTypeDoneAttrId = R.attr.action_done;
    int keyActionTypeSearchAttrId = R.attr.action_search;
    int keyActionTypeGoAttrId = R.attr.action_go;

    LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // int previewLayout = 0;
    mPreviewKeyTextSize = -1;/*  www. j  av  a2s  .  c om*/
    mPreviewLabelTextSize = -1;
    mPreviewKeyBackground = null;
    mPreviewKeyTextColor = 0xFFF;
    final int[] padding = new int[] { 0, 0, 0, 0 };

    KeyboardTheme theme = KeyboardThemeFactory.getCurrentKeyboardTheme(context.getApplicationContext());
    final int keyboardThemeStyleResId = getKeyboardStyleResId(theme);
    Log.d(TAG, "Will use keyboard theme " + theme.getName() + " id " + theme.getId() + " res "
            + keyboardThemeStyleResId);

    //creating a mapping from the remote Attribute IDs to my local attribute ID.
    //this is required in order to backward support any build-system (which may cause the attribute IDs to change)
    final SparseIntArray attributeIdMap = new SparseIntArray(R.styleable.AnyKeyboardViewTheme.length
            + R.styleable.AnyKeyboardViewIconsTheme.length + ACTION_KEY_TYPES.length + KEY_TYPES.length);

    final int[] remoteKeyboardThemeStyleable = KeyboardSupport.createBackwardCompatibleStyleable(
            R.styleable.AnyKeyboardViewTheme, context, theme.getPackageContext(), attributeIdMap);
    final int[] remoteKeyboardIconsThemeStyleable = KeyboardSupport.createBackwardCompatibleStyleable(
            R.styleable.AnyKeyboardViewIconsTheme, context, theme.getPackageContext(), attributeIdMap);

    HashSet<Integer> doneLocalAttributeIds = new HashSet<Integer>();
    TypedArray a = theme.getPackageContext().obtainStyledAttributes(keyboardThemeStyleResId,
            remoteKeyboardThemeStyleable);
    final int n = a.getIndexCount();
    for (int i = 0; i < n; i++) {
        final int remoteIndex = a.getIndex(i);
        final int localAttrId = attributeIdMap.get(remoteKeyboardThemeStyleable[remoteIndex]);
        if (setValueFromTheme(a, padding, localAttrId, remoteIndex)) {
            doneLocalAttributeIds.add(localAttrId);
            if (localAttrId == R.attr.keyBackground) {
                //keyTypeFunctionAttrId and keyActionAttrId are remote
                final int[] keyStateAttributes = KeyboardSupport.createBackwardCompatibleStyleable(KEY_TYPES,
                        context, theme.getPackageContext(), attributeIdMap);
                keyTypeFunctionAttrId = keyStateAttributes[0];
                keyActionAttrId = keyStateAttributes[1];
            }
        }
    }
    a.recycle();
    // taking icons
    int iconSetStyleRes = theme.getIconsThemeResId();
    Log.d(TAG, "Will use keyboard icons theme " + theme.getName() + " id " + theme.getId() + " res "
            + iconSetStyleRes);
    if (iconSetStyleRes != 0) {
        a = theme.getPackageContext().obtainStyledAttributes(iconSetStyleRes,
                remoteKeyboardIconsThemeStyleable);
        final int iconsCount = a.getIndexCount();
        for (int i = 0; i < iconsCount; i++) {
            final int remoteIndex = a.getIndex(i);
            final int localAttrId = attributeIdMap.get(remoteKeyboardIconsThemeStyleable[remoteIndex]);
            if (setKeyIconValueFromTheme(theme, a, localAttrId, remoteIndex)) {
                doneLocalAttributeIds.add(localAttrId);
                if (localAttrId == R.attr.iconKeyAction) {
                    //keyActionTypeDoneAttrId and keyActionTypeSearchAttrId and keyActionTypeGoAttrId are remote
                    final int[] keyStateAttributes = KeyboardSupport.createBackwardCompatibleStyleable(
                            ACTION_KEY_TYPES, context, theme.getPackageContext(), attributeIdMap);
                    keyActionTypeDoneAttrId = keyStateAttributes[0];
                    keyActionTypeSearchAttrId = keyStateAttributes[1];
                    keyActionTypeGoAttrId = keyStateAttributes[2];
                }
            }
        }
        a.recycle();
    }
    // filling what's missing
    KeyboardTheme fallbackTheme = KeyboardThemeFactory.getFallbackTheme(context.getApplicationContext());
    final int keyboardFallbackThemeStyleResId = getKeyboardStyleResId(fallbackTheme);
    Log.d(TAG, "Will use keyboard fallback theme " + fallbackTheme.getName() + " id " + fallbackTheme.getId()
            + " res " + keyboardFallbackThemeStyleResId);
    a = fallbackTheme.getPackageContext().obtainStyledAttributes(keyboardFallbackThemeStyleResId,
            R.styleable.AnyKeyboardViewTheme);

    final int fallbackCount = a.getIndexCount();
    for (int i = 0; i < fallbackCount; i++) {
        final int index = a.getIndex(i);
        final int attrId = R.styleable.AnyKeyboardViewTheme[index];
        if (doneLocalAttributeIds.contains(attrId))
            continue;
        Log.d(TAG, "Falling back theme res ID " + index);
        setValueFromTheme(a, padding, attrId, index);
    }
    a.recycle();
    // taking missing icons
    int fallbackIconSetStyleId = fallbackTheme.getIconsThemeResId();
    Log.d(TAG, "Will use keyboard fallback icons theme " + fallbackTheme.getName() + " id "
            + fallbackTheme.getId() + " res " + fallbackIconSetStyleId);
    a = fallbackTheme.getPackageContext().obtainStyledAttributes(fallbackIconSetStyleId,
            R.styleable.AnyKeyboardViewIconsTheme);

    final int fallbackIconsCount = a.getIndexCount();
    for (int i = 0; i < fallbackIconsCount; i++) {
        final int index = a.getIndex(i);
        final int attrId = R.styleable.AnyKeyboardViewIconsTheme[index];
        if (doneLocalAttributeIds.contains(attrId))
            continue;
        Log.d(TAG, "Falling back icon res ID " + index);
        setKeyIconValueFromTheme(fallbackTheme, a, attrId, index);
    }
    a.recycle();
    //creating the key-drawable state provider, as we suppose to have the entire data now
    mDrawableStatesProvider = new KeyDrawableStateProvider(keyTypeFunctionAttrId, keyActionAttrId,
            keyActionTypeDoneAttrId, keyActionTypeSearchAttrId, keyActionTypeGoAttrId);

    // settings.
    // don't forget that there are TWO paddings, the theme's and the
    // background image's padding!
    Drawable keyboardBabground = super.getBackground();
    if (keyboardBabground != null) {
        Rect backgroundPadding = new Rect();
        keyboardBabground.getPadding(backgroundPadding);
        padding[0] += backgroundPadding.left;
        padding[1] += backgroundPadding.top;
        padding[2] += backgroundPadding.right;
        padding[3] += backgroundPadding.bottom;
    }
    super.setPadding(padding[0], padding[1], padding[2], padding[3]);

    final Resources res = getResources();
    mKeyboardDimens.setKeyboardMaxWidth(res.getDisplayMetrics().widthPixels - padding[0] - padding[2]);
    mPreviewPopup = new PopupWindow(context);
    if (mPreviewKeyTextSize > 0) {
        if (mPreviewLabelTextSize <= 0)
            mPreviewLabelTextSize = mPreviewKeyTextSize;
        mPreviewLayut = inflatePreviewWindowLayout(inflate);
        mPreviewText = (TextView) mPreviewLayut.findViewById(R.id.key_preview_text);
        mPreviewText.setTextColor(mPreviewKeyTextColor);
        mPreviewText.setTypeface(mKeyTextStyle);
        mPreviewIcon = (ImageView) mPreviewLayut.findViewById(R.id.key_preview_icon);
        mPreviewPopup.setBackgroundDrawable(mPreviewKeyBackground);
        mPreviewPopup.setContentView(mPreviewLayut);
        mShowPreview = mPreviewLayut != null;
    } else {
        mPreviewLayut = null;
        mPreviewText = null;
        mShowPreview = false;
    }
    mPreviewPopup.setTouchable(false);
    mPreviewPopup
            .setAnimationStyle((mAnimationLevel == AnimationsLevel.None) ? 0 : R.style.KeyPreviewAnimation);
    mDelayBeforePreview = 0;
    mDelayAfterPreview = 10;

    mMiniKeyboardParent = this;
    mMiniKeyboardPopup = new PopupWindow(context.getApplicationContext());
    mMiniKeyboardPopup.setBackgroundDrawable(null);

    mMiniKeyboardPopup
            .setAnimationStyle((mAnimationLevel == AnimationsLevel.None) ? 0 : R.style.MiniKeyboardAnimation);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setTextSize(mKeyTextSize);
    mPaint.setTextAlign(Align.CENTER);
    mPaint.setAlpha(255);

    mKeyBackgroundPadding = new Rect(0, 0, 0, 0);
    mKeyBackground.getPadding(mKeyBackgroundPadding);

    reloadSwipeThresholdsSettings(res);

    mMiniKeyboardSlideAllowance = res.getDimension(R.dimen.mini_keyboard_slide_allowance);

    AskOnGestureListener listener = new AskGestureEventsListener(this);

    mGestureDetector = AnyApplication.getDeviceSpecific().createGestureDetector(getContext(), listener);
    mGestureDetector.setIsLongpressEnabled(false);

    MultiTouchSupportLevel multiTouchSupportLevel = AnyApplication.getDeviceSpecific()
            .getMultiTouchSupportLevel(getContext());

    mHasDistinctMultitouch = multiTouchSupportLevel == MultiTouchSupportLevel.Distinct;

    mKeyRepeatInterval = 50;

    AnyApplication.getConfig().addChangedListener(this);
}