Example usage for android.util TypedValue complexToDimensionPixelSize

List of usage examples for android.util TypedValue complexToDimensionPixelSize

Introduction

In this page you can find the example usage for android.util TypedValue complexToDimensionPixelSize.

Prototype

public static int complexToDimensionPixelSize(int data, DisplayMetrics metrics) 

Source Link

Document

Converts a complex data value holding a dimension to its final value as an integer pixel size.

Usage

From source file:com.miz.functions.MizLib.java

/**
 * Add a margin with a height of the ActionBar to the top of a given View contained in a FrameLayout
 * @param c/*from   w  w w  .  ja v a  2  s .  c om*/
 * @param v
 */
public static void addActionBarMarginBottom(Context c, View v) {
    int mActionBarHeight = 0;
    TypedValue tv = new TypedValue();
    if (c.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                c.getResources().getDisplayMetrics());
    else
        mActionBarHeight = 0; // No ActionBar style (pre-Honeycomb or ActionBar not in theme)

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    params.setMargins(0, 0, 0, mActionBarHeight);
    v.setLayoutParams(params);
}

From source file:com.linkbubble.util.Util.java

public static Integer getSystemActionBarHeight(Context context) {
    // Calculate ActionBar height
    try {// w  w w  . j  a va 2  s .co m
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
    } catch (Exception e) {
    }

    return null;
}

From source file:com.miz.functions.MizLib.java

public static int getActionBarHeight(Context c) {
    int actionBarHeight = 0;
    TypedValue tv = new TypedValue();
    if (c.getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, c.getResources().getDisplayMetrics());
    else//from ww  w  .j av  a 2  s.  co  m
        actionBarHeight = 0; // No ActionBar style (pre-Honeycomb or ActionBar not in theme)

    return actionBarHeight;
}

From source file:android.support.v7.app.AppCompatDelegateImplV7.java

@Override
ActionMode startSupportActionModeFromWindow(ActionMode.Callback callback) {
    if (mActionMode != null) {
        mActionMode.finish();// w w w  . ja va  2  s  . c  o m
    }

    final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapper(callback);
    final Context context = getActionBarThemedContext();

    if (mActionModeView == null) {
        if (mIsFloating) {
            mActionModeView = new ActionBarContextView(context);
            mActionModePopup = new PopupWindow(context, null, R.attr.actionModePopupWindowStyle);
            mActionModePopup.setContentView(mActionModeView);
            mActionModePopup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

            TypedValue heightValue = new TypedValue();
            mContext.getTheme().resolveAttribute(R.attr.actionBarSize, heightValue, true);
            final int height = TypedValue.complexToDimensionPixelSize(heightValue.data,
                    mContext.getResources().getDisplayMetrics());
            mActionModeView.setContentHeight(height);
            mActionModePopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            mShowActionModePopup = new Runnable() {
                public void run() {
                    mActionModePopup.showAtLocation(mActionModeView, Gravity.TOP | Gravity.FILL_HORIZONTAL, 0,
                            0);
                }
            };
        } else {
            ViewStubCompat stub = (ViewStubCompat) mSubDecor.findViewById(R.id.action_mode_bar_stub);
            if (stub != null) {
                // Set the layout inflater so that it is inflated with the action bar's context
                stub.setLayoutInflater(LayoutInflater.from(context));
                mActionModeView = (ActionBarContextView) stub.inflate();
            }
        }
    }

    if (mActionModeView != null) {
        mActionModeView.killMode();
        ActionMode mode = new StandaloneActionMode(context, mActionModeView, wrappedCallback,
                mActionModePopup == null);
        if (callback.onCreateActionMode(mode, mode.getMenu())) {
            mode.invalidate();
            mActionModeView.initForMode(mode);
            mActionModeView.setVisibility(View.VISIBLE);
            mActionMode = mode;
            if (mActionModePopup != null) {
                mWindow.getDecorView().post(mShowActionModePopup);
            }
            mActionModeView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);

            if (mActionModeView.getParent() != null) {
                ViewCompat.requestApplyInsets((View) mActionModeView.getParent());
            }
        } else {
            mActionMode = null;
        }
    }
    if (mActionMode != null && mAppCompatCallback != null) {
        mAppCompatCallback.onSupportActionModeStarted(mActionMode);
    }
    return mActionMode;
}

From source file:com.NewCleanWeather.widget.ViewDragHelper.java

private int getActionBarHeight() {
    int actionBarHeight = 0;
    TypedValue tv = new TypedValue();
    if (mCapturedView.getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                mCapturedView.getContext().getResources().getDisplayMetrics());
    }//from w  w  w. j  a va  2s.c om
    return actionBarHeight;
}

From source file:android.support.v7ox.app.AppCompatDelegateImplV7.java

@Override
ActionMode startSupportActionModeFromWindow(ActionMode.Callback callback) {
    endOnGoingFadeAnimation();/*from  w  w  w  . ja  v  a 2 s .  c  o m*/
    if (mActionMode != null) {
        mActionMode.finish();
    }

    final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapperV7(callback);
    ActionMode mode = null;
    if (mAppCompatCallback != null && !isDestroyed()) {
        try {
            mode = mAppCompatCallback.onWindowStartingSupportActionMode(wrappedCallback);
        } catch (AbstractMethodError ame) {
            // Older apps might not implement this callback method.
        }
    }

    if (mode != null) {
        mActionMode = mode;
    } else {
        if (mActionModeView == null) {
            if (mIsFloating) {
                // Use the action bar theme.
                final TypedValue outValue = new TypedValue();
                final Resources.Theme baseTheme = mContext.getTheme();
                baseTheme.resolveAttribute(R.attr.actionBarTheme_ox, outValue, true);

                final Context actionBarContext;
                if (outValue.resourceId != 0) {
                    final Resources.Theme actionBarTheme = mContext.getResources().newTheme();
                    actionBarTheme.setTo(baseTheme);
                    actionBarTheme.applyStyle(outValue.resourceId, true);

                    actionBarContext = new ContextThemeWrapper(mContext, 0);
                    actionBarContext.getTheme().setTo(actionBarTheme);
                } else {
                    actionBarContext = mContext;
                }

                mActionModeView = new ActionBarContextView(actionBarContext);
                mActionModePopup = new PopupWindow(actionBarContext, null,
                        R.attr.actionModePopupWindowStyle_ox);
                PopupWindowCompat.setWindowLayoutType(mActionModePopup,
                        WindowManager.LayoutParams.TYPE_APPLICATION);
                mActionModePopup.setContentView(mActionModeView);
                mActionModePopup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

                actionBarContext.getTheme().resolveAttribute(R.attr.actionBarSize_ox, outValue, true);
                final int height = TypedValue.complexToDimensionPixelSize(outValue.data,
                        actionBarContext.getResources().getDisplayMetrics());
                mActionModeView.setContentHeight(height);
                mActionModePopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                mShowActionModePopup = new Runnable() {
                    public void run() {
                        mActionModePopup.showAtLocation(mActionModeView, Gravity.TOP | Gravity.FILL_HORIZONTAL,
                                0, 0);
                        endOnGoingFadeAnimation();
                        ViewCompat.setAlpha(mActionModeView, 0f);
                        mFadeAnim = ViewCompat.animate(mActionModeView).alpha(1f);
                        mFadeAnim.setListener(new ViewPropertyAnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(View view) {
                                ViewCompat.setAlpha(mActionModeView, 1f);
                                mFadeAnim.setListener(null);
                                mFadeAnim = null;
                            }

                            @Override
                            public void onAnimationStart(View view) {
                                mActionModeView.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                };
            } else {
                ViewStubCompat stub = (ViewStubCompat) mSubDecor.findViewById(R.id.action_mode_bar_stub);
                if (stub != null) {
                    // Set the layout inflater so that it is inflated with the action bar's context
                    stub.setLayoutInflater(LayoutInflater.from(getActionBarThemedContext()));
                    mActionModeView = (ActionBarContextView) stub.inflate();
                }
            }
        }

        if (mActionModeView != null) {
            endOnGoingFadeAnimation();
            mActionModeView.killMode();
            mode = new StandaloneActionMode(mActionModeView.getContext(), mActionModeView, wrappedCallback,
                    mActionModePopup == null);
            if (callback.onCreateActionMode(mode, mode.getMenu())) {
                mode.invalidate();
                mActionModeView.initForMode(mode);
                mActionMode = mode;
                ViewCompat.setAlpha(mActionModeView, 0f);
                mFadeAnim = ViewCompat.animate(mActionModeView).alpha(1f);
                mFadeAnim.setListener(new ViewPropertyAnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(View view) {
                        ViewCompat.setAlpha(mActionModeView, 1f);
                        mFadeAnim.setListener(null);
                        mFadeAnim = null;
                    }

                    @Override
                    public void onAnimationStart(View view) {
                        mActionModeView.setVisibility(View.VISIBLE);
                        mActionModeView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
                        if (mActionModeView.getParent() != null) {
                            ViewCompat.requestApplyInsets((View) mActionModeView.getParent());
                        }
                    }
                });
                if (mActionModePopup != null) {
                    mWindow.getDecorView().post(mShowActionModePopup);
                }
            } else {
                mActionMode = null;
            }
        }
    }
    if (mActionMode != null && mAppCompatCallback != null) {
        mAppCompatCallback.onSupportActionModeStarted(mActionMode);
    }
    return mActionMode;
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private void setChipDimensions(final Context context, final AttributeSet attrs) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecipientEditTextView, 0, 0);
    final Resources r = getContext().getResources();
    mChipBackground = a.getDrawable(R.styleable.RecipientEditTextView_chipBackground);
    if (mChipBackground == null)
        mChipBackground = ContextCompat.getDrawable(context, R.drawable.circle_background_chips);
    mChipBackgroundPressed = a.getDrawable(R.styleable.RecipientEditTextView_chipBackgroundPressed);
    if (mChipBackgroundPressed == null)
        mChipBackgroundPressed = ContextCompat.getDrawable(context, R.drawable.chip_background_selected);
    mChipDelete = a.getDrawable(R.styleable.RecipientEditTextView_chipDelete);
    if (mChipDelete == null)
        mChipDelete = ContextCompat.getDrawable(context, R.drawable.chip_delete);
    mChipPadding = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipPadding, -1);
    if (mChipPadding == -1)
        mChipPadding = (int) r.getDimension(R.dimen.chip_padding);
    mAlternatesLayout = a.getResourceId(R.styleable.RecipientEditTextView_chipAlternatesLayout, -1);
    if (mAlternatesLayout == -1)
        mAlternatesLayout = R.layout.chips_alternate_item;
    mDefaultContactPhoto = BitmapFactory.decodeResource(r, R.drawable.ic_contact_picture);
    mMoreItem = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.more_item, null);
    mChipHeight = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipHeight, -1);
    if (mChipHeight == -1)
        mChipHeight = r.getDimension(R.dimen.chip_height);
    mChipFontSize = a.getDimensionPixelSize(R.styleable.RecipientEditTextView_chipFontSize, -1);
    if (mChipFontSize == -1)
        mChipFontSize = r.getDimension(R.dimen.chip_text_size);
    mInvalidChipBackground = a.getDrawable(R.styleable.RecipientEditTextView_invalidChipBackground);
    if (mInvalidChipBackground == null)
        mInvalidChipBackground = ContextCompat.getDrawable(context, R.drawable.chip_background_invalid);
    mLineSpacingExtra = r.getDimension(R.dimen.line_spacing_extra);
    mMaxLines = r.getInteger(R.integer.chips_max_lines);
    final TypedValue tv = new TypedValue();
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
        mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
    a.recycle();/*from w w w . java2 s  .c  o  m*/
}

From source file:org.mariotaku.twidere.util.ThemeUtils.java

public static int getActionBarHeight(Context context) {
    final TypedValue tv = new TypedValue();
    final Resources.Theme theme = context.getTheme();
    final int attr = R.attr.actionBarSize;
    if (theme.resolveAttribute(attr, tv, true)) {
        return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }// w ww  .  java 2  s  . co m
    return 0;
}

From source file:com.example.ray.firstapp.bottombar.BottomBar.java

private static void navBarMagic(Activity activity, final BottomBar bottomBar) {
    Resources res = activity.getResources();
    int softMenuIdentifier = res.getIdentifier("config_showNavigationBar", "bool", "android");
    int navBarIdentifier = res.getIdentifier("navigation_bar_height", "dimen", "android");
    int navBarHeight = 0;

    if (navBarIdentifier > 0) {
        navBarHeight = res.getDimensionPixelSize(navBarIdentifier);
    }/*w w w.j  ava 2  s.  co  m*/

    if (!bottomBar.drawBehindNavBar() || navBarHeight == 0
            || (!(softMenuIdentifier > 0 && res.getBoolean(softMenuIdentifier)))) {
        return;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
            && ViewConfiguration.get(activity).hasPermanentMenuKey()) {
        return;
    }

    /**
     * Copy-paste coding made possible by:
     * http://stackoverflow.com/a/14871974/940036
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = activity.getWindowManager().getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        boolean hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;

        if (!hasSoftwareKeys) {
            return;
        }
    }
    /**
     * End of delicious copy-paste code
     */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
            && res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        activity.getWindow().getAttributes().flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

        if (bottomBar.useTopOffset()) {
            int offset;
            int statusBarResource = res.getIdentifier("status_bar_height", "dimen", "android");

            if (statusBarResource > 0) {
                offset = res.getDimensionPixelSize(statusBarResource);
            } else {
                offset = MiscUtils.dpToPixel(activity, 25);
            }

            if (!bottomBar.useOnlyStatusbarOffset()) {
                TypedValue tv = new TypedValue();
                if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                    offset += TypedValue.complexToDimensionPixelSize(tv.data, res.getDisplayMetrics());
                } else {
                    offset += MiscUtils.dpToPixel(activity, 56);
                }
            }

            bottomBar.getUserContainer().setPadding(0, offset, 0, 0);
        }

        final View outerContainer = bottomBar.getOuterContainer();
        final int navBarHeightCopy = navBarHeight;
        bottomBar.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void onGlobalLayout() {
                        bottomBar.shyHeightAlreadyCalculated();

                        int newHeight = outerContainer.getHeight() + navBarHeightCopy;
                        outerContainer.getLayoutParams().height = newHeight;

                        if (bottomBar.isShy()) {
                            int defaultOffset = bottomBar.useExtraOffset() ? navBarHeightCopy : 0;
                            bottomBar.setTranslationY(defaultOffset);
                            ((CoordinatorLayout.LayoutParams) bottomBar.getLayoutParams())
                                    .setBehavior(new BottomNavigationBehavior(newHeight, defaultOffset));
                        }

                        ViewTreeObserver obs = outerContainer.getViewTreeObserver();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            obs.removeOnGlobalLayoutListener(this);
                        } else {
                            obs.removeGlobalOnLayoutListener(this);
                        }
                    }
                });
    }
}