Example usage for android.util TypedValue getDimension

List of usage examples for android.util TypedValue getDimension

Introduction

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

Prototype

public float getDimension(DisplayMetrics metrics) 

Source Link

Document

Return the data for this value as a dimension.

Usage

From source file:Main.java

public static float getAttributeDimension(final Context context, final Resources.Theme theme, final int resId) {
    final TypedValue typedValue = new TypedValue(); // create a new typed value to received the resolved attribute
    // value//from  w w  w  .j a va  2  s.  c o m
    final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    if (!theme.resolveAttribute(resId, typedValue, true)) // if we can't resolve the value
    {
        throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(resId));
    }
    if (typedValue.type != TypedValue.TYPE_DIMENSION) // if the value isn't of the correct type
    {
        throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(resId) + " type #0x"
                + Integer.toHexString(typedValue.type) + " is not valid");
    }
    return typedValue.getDimension(displayMetrics); // return the value of the attribute in terms of the display
}

From source file:org.mariotaku.twidere.view.ExtendedRecyclerView.java

/**
 * Ported from View.getVerticalScrollFactor.
 *//*from   w ww.j  a v a2 s.c o  m*/
private float getScrollFactorBackport() {
    if (mScrollFactor == Float.MIN_VALUE) {
        TypedValue outValue = new TypedValue();
        if (getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, outValue, true)) {
            mScrollFactor = outValue.getDimension(getContext().getResources().getDisplayMetrics());
        } else {
            return 0; //listPreferredItemHeight is not defined, no generic scrolling
        }

    }
    return mScrollFactor;
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    boolean measure = false;

    widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);

    final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor;

    if (tv.type != TypedValue.TYPE_NULL) {
        final int min;
        if (tv.type == TypedValue.TYPE_DIMENSION) {
            min = (int) tv.getDimension(metrics);
        } else if (tv.type == TypedValue.TYPE_FRACTION) {
            min = (int) tv.getFraction(metrics.widthPixels, metrics.widthPixels);
        } else {/*  ww w.j a va2  s  .  co m*/
            min = 0;
        }

        if (width < min) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
            measure = true;
        }
    }

    // TODO: Support height?

    if (measure) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

From source file:de.msal.shoutemo.activities.MainActivity.java

/**
 * Setup Navigation Drawer width as per Material Design guidelines: calculate the Toolbar height.
 *//*from w w  w .ja va  2 s  .  co m*/
private void setupToolbar(Bundle savedInstanceState) {
    Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolBar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolBar, 0, 0) {
        public void onDrawerClosed(View view) {
            drawerOpen = false;
        }

        public void onDrawerOpened(View drawerView) {
            drawerOpen = true;
        }
    };
    mNavigationDrawer = findViewById(R.id.navigation_drawer);

    TypedValue tv = new TypedValue();
    int toolbarHeight;
    if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
        toolbarHeight = (int) (tv.getDimension(getResources().getDisplayMetrics())
                / getResources().getDisplayMetrics().density);
        if (toolbarHeight > 0) {
            int drawerMaxWidth = (int) (getResources().getDimension(R.dimen.navigation_drawer_max_width)
                    / getResources().getDisplayMetrics().density);
            Configuration configuration = getResources().getConfiguration();
            int screenWidthDp = configuration.screenWidthDp;
            int drawerWidth = screenWidthDp - toolbarHeight;

            ViewGroup.LayoutParams layout_description = mNavigationDrawer.getLayoutParams();
            layout_description.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    drawerWidth < drawerMaxWidth ? drawerWidth : drawerMaxWidth,
                    getResources().getDisplayMetrics());
            mNavigationDrawer.setLayoutParams(layout_description);
        }
    }

    mDrawerLayout.addDrawerListener(mDrawerToggle);

    mDrawerList = (ListView) mNavigationDrawer.findViewById(android.R.id.list);
    mDrawerList.setAdapter(new NavigationDrawerAdapter(getApplicationContext(), 0));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    if (savedInstanceState == null) {
        mDrawerList.performItemClick(mDrawerList, 0, mDrawerList.getAdapter().getItemId(0)); //preselect on start
    }
    if (drawerOpen) {
        mDrawerLayout.openDrawer(mNavigationDrawer);
    }
}

From source file:de.msal.shoutemo.ui.MainActivity.java

/**
 * Setup Navigation Drawer width as per Material Design guidelines: calculate the Toolbar height.
 *//*from   w  ww .ja v a  2s  .  c o  m*/
private void setupToolbar(Bundle savedInstanceState) {
    Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolBar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolBar, 0, 0) {
        public void onDrawerClosed(View view) {
            drawerOpen = false;
        }

        public void onDrawerOpened(View drawerView) {
            drawerOpen = true;
        }
    };
    mNavigationDrawer = findViewById(R.id.navigation_drawer);

    TypedValue tv = new TypedValue();
    int toolbarHeight;
    if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
        toolbarHeight = (int) (tv.getDimension(getResources().getDisplayMetrics())
                / getResources().getDisplayMetrics().density);
        if (toolbarHeight > 0) {
            int drawerMaxWidth = (int) (getResources().getDimension(R.dimen.navigation_drawer_max_width)
                    / getResources().getDisplayMetrics().density);
            Configuration configuration = getResources().getConfiguration();
            int screenWidthDp = configuration.screenWidthDp;
            int drawerWidth = screenWidthDp - toolbarHeight;

            ViewGroup.LayoutParams layout_description = mNavigationDrawer.getLayoutParams();
            layout_description.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    drawerWidth < drawerMaxWidth ? drawerWidth : drawerMaxWidth,
                    getResources().getDisplayMetrics());
            mNavigationDrawer.setLayoutParams(layout_description);
        }
    }

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerList = (ListView) mNavigationDrawer.findViewById(android.R.id.list);
    mDrawerList.setAdapter(new NavigationDrawerAdapter(getApplicationContext(), 0));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    if (savedInstanceState == null) {
        mDrawerList.performItemClick(mDrawerList, 0, mDrawerList.getAdapter().getItemId(0)); //preselect on start
    }
    if (drawerOpen) {
        mDrawerLayout.openDrawer(mNavigationDrawer);
    }
}

From source file:ro.expectations.expenses.widget.dialog.CategoryPickerAdapter.java

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    mCursor.moveToPosition(position);//from   w ww. j a va 2 s .  c  o  m

    // Set the icon background color
    GradientDrawable bgShape = (GradientDrawable) holder.mCategoryIconBackground.getBackground();
    bgShape.setColor(0xFF000000 | ContextCompat.getColor(mContext, R.color.colorPrimary));

    // Set the icon
    int iconId;
    if (getItemId(position) == 0) {
        iconId = R.drawable.ic_clear_black_24dp;
    } else {
        iconId = R.drawable.ic_question_mark_black_24dp;
    }
    holder.mCategoryIcon.setImageDrawable(DrawableHelper.tint(mContext, iconId, R.color.colorWhite));

    // Set the category name
    String name = mCursor.getString(COLUMN_CATEGORY_NAME);
    holder.mCategoryName.setText(name);

    // Set the proper padding for a dialog.
    TypedValue value = new TypedValue();
    mContext.getTheme().resolveAttribute(R.attr.dialogPreferredPadding, value, true);
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
    int padding = (int) value.getDimension(metrics);
    holder.mCategoryItem.setPadding(padding, holder.mCategoryItem.getPaddingTop(), padding,
            holder.mCategoryItem.getPaddingBottom());
}

From source file:com.hippo.conductor.dialog.DialogContentView.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final boolean isPortrait = getResources()
            .getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    final TypedValue tv = isPortrait ? minWidthMinor : minWidthMajor;
    boolean measure = false;

    if (widthMode == MeasureSpec.AT_MOST && tv.type != TypedValue.TYPE_NULL) {
        final int min;
        final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        if (tv.type == TypedValue.TYPE_DIMENSION) {
            min = (int) tv.getDimension(metrics);
        } else if (tv.type == TypedValue.TYPE_FRACTION) {
            min = (int) tv.getFraction(metrics.widthPixels, metrics.widthPixels);
        } else {//www .j  av a 2  s. c  om
            min = 0;
        }

        if (getMeasuredWidth() < min) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, MeasureSpec.EXACTLY);
            measure = true;
        }
    }

    if (measure) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;

    final int widthMode = getMode(widthMeasureSpec);
    final int heightMode = getMode(heightMeasureSpec);

    boolean fixedWidth = false;
    if (widthMode == AT_MOST) {
        final TypedValue tvw = isPortrait ? mFixedWidthMinor : mFixedWidthMajor;
        if (tvw != null && tvw.type != TypedValue.TYPE_NULL) {
            int w = 0;
            if (tvw.type == TypedValue.TYPE_DIMENSION) {
                w = (int) tvw.getDimension(metrics);
            } else if (tvw.type == TypedValue.TYPE_FRACTION) {
                w = (int) tvw.getFraction(metrics.widthPixels, metrics.widthPixels);
            }/*from  w w  w  . ja  va 2s.com*/
            if (w > 0) {
                w -= (mDecorPadding.left + mDecorPadding.right);
                final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(w, widthSize), EXACTLY);
                fixedWidth = true;
            }
        }
    }

    if (heightMode == AT_MOST) {
        final TypedValue tvh = isPortrait ? mFixedHeightMajor : mFixedHeightMinor;
        if (tvh != null && tvh.type != TypedValue.TYPE_NULL) {
            int h = 0;
            if (tvh.type == TypedValue.TYPE_DIMENSION) {
                h = (int) tvh.getDimension(metrics);
            } else if (tvh.type == TypedValue.TYPE_FRACTION) {
                h = (int) tvh.getFraction(metrics.heightPixels, metrics.heightPixels);
            }
            if (h > 0) {
                h -= (mDecorPadding.top + mDecorPadding.bottom);
                final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(h, heightSize), EXACTLY);
            }
        }
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();
    boolean measure = false;

    widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);

    if (!fixedWidth && widthMode == AT_MOST) {
        final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor;
        if (tv != null && tv.type != TypedValue.TYPE_NULL) {
            int min = 0;
            if (tv.type == TypedValue.TYPE_DIMENSION) {
                min = (int) tv.getDimension(metrics);
            } else if (tv.type == TypedValue.TYPE_FRACTION) {
                min = (int) tv.getFraction(metrics.widthPixels, metrics.widthPixels);
            }
            if (min > 0) {
                min -= (mDecorPadding.left + mDecorPadding.right);
            }
            if (width < min) {
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
                measure = true;
            }
        }
    }

    if (measure) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

From source file:com.google.android.apps.gutenberg.ScannerActivity.java

private int calcSlideDistance() {
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int height = metrics.heightPixels;
    TypedValue value = new TypedValue();
    getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, value, true);
    float itemHeight = value.getDimension(metrics);
    float paddingTop = getResources().getDimension(R.dimen.list_vertical_padding);
    return (int) ((height - itemHeight + paddingTop) / 2);
}

From source file:inc.bait.jubilee.ui.fragments.ContactsListFragment.java

private int getListPreferredItemHeight() {
    final TypedValue typedValue = new TypedValue();
    getActivity().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, typedValue, true);
    final DisplayMetrics metrics = new android.util.DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) typedValue.getDimension(metrics);
}