Example usage for android.util TypedValue TypedValue

List of usage examples for android.util TypedValue TypedValue

Introduction

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

Prototype

TypedValue

Source Link

Usage

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

protected static int getColor(Context context, int color) {
    TypedValue tv = new TypedValue();
    context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
    return tv.data;
}

From source file:com.monmonja.library.utils.ViewUtils.java

public static void makeToast(Context context, int resId) {
    TypedValue tv = new TypedValue();
    int offsetY = 0;
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        offsetY = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }/*from  w  w  w. jav  a2s.  c om*/

    Toast toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    View view = toast.getView();
    view.setBackgroundColor(context.getResources().getColor(R.color.toast_background));
    TextView text = (TextView) view.findViewById(android.R.id.message);
    text.setTextColor(context.getResources().getColor(android.R.color.white));
    toast.show();
}

From source file:com.fa.mastodon.util.ThemeUtils.java

public static @DrawableRes int getDrawableId(Context context, @AttrRes int attribute,
        @DrawableRes int fallbackDrawableId) {
    TypedValue value = new TypedValue();
    if (context.getTheme().resolveAttribute(attribute, value, true)) {
        return value.resourceId;
    } else {/*from w w w.j av  a  2s.  c o  m*/
        return fallbackDrawableId;
    }
}

From source file:Main.java

public static Bitmap decodeResource(Resources res, int resId, int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    // Determine insample size
    opts.inJustDecodeBounds = true;//  ww w  . j av  a2 s.  co  m
    BitmapFactory.decodeResource(res, resId, opts);
    opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight);

    // Decode the bitmap, regionally if necessary
    Bitmap bitmap = null;
    opts.inJustDecodeBounds = false;
    Rect rect = getCropRectIfNecessary(opts, reqWidth, reqHeight);

    InputStream stream = null;
    try {
        if (rect != null) {
            stream = res.openRawResource(resId, new TypedValue());
            if (stream == null)
                return null;
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(stream, false);
            bitmap = decoder.decodeRegion(rect, opts);
        } else {
            bitmap = BitmapFactory.decodeResource(res, resId, opts);
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to open resource " + resId, e);
    } finally {
        closeSilently(stream);
    }
    return bitmap;
}

From source file:Main.java

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context        Activity Context
 * @param styleAttrId    Theme style id//  ww  w.  jav a2s.  co  m
 * @param subStyleAttrId the sub style from the theme to look up after the first style
 * @param attributeId    if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, int subStyleAttrId, int[] attributeId) {
    if (styleAttrId == -1 || attributeId == null)
        return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    int subStyleResId = -1;
    final TypedArray parentTypedArray = theme.obtainStyledAttributes(value.resourceId,
            new int[] { subStyleAttrId });
    try {
        subStyleResId = parentTypedArray.getResourceId(0, -1);
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        parentTypedArray.recycle();
    }

    if (subStyleResId == -1)
        return null;
    final TypedArray subTypedArray = context.obtainStyledAttributes(subStyleResId, attributeId);
    if (subTypedArray != null) {
        try {
            return subTypedArray.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            subTypedArray.recycle();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context        Activity Context
 * @param styleAttrId    Theme style id/*  w w w.j a  va  2  s  . c o m*/
 * @param subStyleAttrId the sub style from the theme to look up after the first style
 * @param attributeId    if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, int subStyleAttrId, int attributeId) {
    if (styleAttrId == -1 || attributeId == -1)
        return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    int subStyleResId = -1;
    final TypedArray parentTypedArray = theme.obtainStyledAttributes(value.resourceId,
            new int[] { subStyleAttrId });
    try {
        subStyleResId = parentTypedArray.getResourceId(0, -1);
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        parentTypedArray.recycle();
    }

    if (subStyleResId == -1)
        return null;
    final TypedArray subTypedArray = context.obtainStyledAttributes(subStyleResId, new int[] { attributeId });
    if (subTypedArray != null) {
        try {
            return subTypedArray.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            subTypedArray.recycle();
        }
    }
    return null;
}

From source file:com.grarak.kerneladiutor.utils.ViewUtils.java

public static int getTextSecondaryColor(Context context) {
    TypedValue value = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.textColorSecondary, value, true);
    return value.data;
}

From source file:com.fa.mastodon.util.ThemeUtils.java

public static @ColorInt int getColor(Context context, @AttrRes int attribute) {
    TypedValue value = new TypedValue();
    if (context.getTheme().resolveAttribute(attribute, value, true)) {
        return value.data;
    } else {//from  ww w  .ja v  a 2  s.  c o m
        return Color.BLACK;
    }
}

From source file:Main.java

/**
 * Last but not least, try to pull the Font Path from the Theme, which is defined.
 *
 * @param context        Activity Context
 * @param styleAttrId    Theme style id/*  w  w  w.  j  av  a 2 s  . c o m*/
 * @param subStyleAttrId the sub style from the theme to look up after the first style
 * @param attributeId    if -1 returns null.
 * @return null if no theme or attribute defined.
 */
static String pullFontPathFromTheme(Context context, int styleAttrId, int subStyleAttrId, int attributeId) {
    if (styleAttrId == -1 || attributeId == -1)
        return null;

    final Resources.Theme theme = context.getTheme();
    final TypedValue value = new TypedValue();

    theme.resolveAttribute(styleAttrId, value, true);
    int subStyleResId = -1;
    final TypedArray parentTypedArray = theme.obtainStyledAttributes(value.resourceId,
            getAttributeArray(subStyleAttrId));
    try {
        subStyleResId = parentTypedArray.getResourceId(0, -1);
    } catch (Exception ignore) {
        // Failed for some reason.
        return null;
    } finally {
        parentTypedArray.recycle();
    }

    if (subStyleResId == -1)
        return null;
    final TypedArray subTypedArray = context.obtainStyledAttributes(subStyleResId,
            getAttributeArray(attributeId));
    if (subTypedArray != null) {
        try {
            return subTypedArray.getString(0);
        } catch (Exception ignore) {
            // Failed for some reason.
            return null;
        } finally {
            subTypedArray.recycle();
        }
    }
    return null;
}

From source file:com.agenmate.lollipop.util.ViewUtils.java

public static int getActionBarSize(@NonNull Context context) {
    TypedValue value = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.actionBarSize, value, true);
    int actionBarSize = TypedValue.complexToDimensionPixelSize(value.data,
            context.getResources().getDisplayMetrics());
    return actionBarSize;
}