Example usage for android.content Context getTheme

List of usage examples for android.content Context getTheme

Introduction

In this page you can find the example usage for android.content Context getTheme.

Prototype

@ViewDebug.ExportedProperty(deepExport = true)
public abstract Resources.Theme getTheme();

Source Link

Document

Return the Theme object associated with this Context.

Usage

From source file:com.google.samples.apps.iosched.util.UIUtils.java

/**
 * Queries the theme of the given {@code context} for a theme color.
 *
 * @param context            the context holding the current theme.
 * @param attrResId          the theme color attribute to resolve.
 * @param fallbackColorResId a color resource id tto fallback to if the theme color cannot be
 *                           resolved.//from   w  w w.java2 s  . c  o m
 * @return the theme color or the fallback color.
 */
public static @ColorInt int getThemeColor(@NonNull Context context, @AttrRes int attrResId,
        @ColorRes int fallbackColorResId) {
    final TypedValue tv = new TypedValue();
    if (context.getTheme().resolveAttribute(attrResId, tv, true)) {
        return tv.data;
    }
    return ContextCompat.getColor(context, fallbackColorResId);
}

From source file:android.support.graphics.drawable.AnimatedVectorDrawableCompat.java

/**
 * Create a AnimatedVectorDrawableCompat object.
 *
 * @param context the context for creating the animators.
 * @param resId   the resource ID for AnimatedVectorDrawableCompat object.
 * @return a new AnimatedVectorDrawableCompat or null if parsing error is found.
 *//*from   ww  w  .  j av  a2  s . c  om*/
@Nullable
public static AnimatedVectorDrawableCompat create(@NonNull Context context, @DrawableRes int resId) {
    if (Build.VERSION.SDK_INT >= 23) {
        final AnimatedVectorDrawableCompat drawable = new AnimatedVectorDrawableCompat(context);
        drawable.mDelegateDrawable = ResourcesCompat.getDrawable(context.getResources(), resId,
                context.getTheme());
        drawable.mDelegateDrawable.setCallback(drawable.mCallback);
        drawable.mCachedConstantStateDelegate = new AnimatedVectorDrawableDelegateState(
                drawable.mDelegateDrawable.getConstantState());
        return drawable;
    }
    Resources resources = context.getResources();
    try {
        final XmlPullParser parser = resources.getXml(resId);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
            // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }
        return createFromXmlInner(context, context.getResources(), parser, attrs, context.getTheme());
    } catch (XmlPullParserException e) {
        Log.e(LOGTAG, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGTAG, "parser error", e);
    }
    return null;
}

From source file:de.mrapp.android.util.ThemeUtil.java

/**
 * Obtains the attribute, which corresponds to a specific resource id, from a theme.
 *
 * @param context/*from  ww  w. j a  va  2s. co m*/
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param themeResourceId
 *         The resource id of the theme, the attribute should be obtained from, as an {@link
 *         Integer} value or -1, if the attribute should be obtained from the given context's
 *         theme
 * @param resourceId
 *         The resource id of the attribute, which should be obtained, as an {@link Integer}
 *         value. The resource id must corresponds to a valid theme attribute
 * @return A type array, which holds the attribute, which has been obtained, as an instance of
 * the class {@link TypedArray}
 */
private static TypedArray obtainStyledAttributes(@NonNull final Context context,
        @StyleRes final int themeResourceId, @AttrRes final int resourceId) {
    ensureNotNull(context, "The context may not be null");
    Theme theme = context.getTheme();
    int[] attrs = new int[] { resourceId };

    if (themeResourceId != -1) {
        return theme.obtainStyledAttributes(themeResourceId, attrs);
    } else {
        return theme.obtainStyledAttributes(attrs);
    }
}

From source file:com.google.samples.apps.iosched.util.UIUtils.java

public static Bitmap vectorToBitmap(@NonNull Context context, @DrawableRes int drawableResId) {
    VectorDrawableCompat vector = VectorDrawableCompat.create(context.getResources(), drawableResId,
            context.getTheme());
    final Bitmap bitmap = Bitmap.createBitmap(vector.getIntrinsicWidth(), vector.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);//from  ww  w . ja v  a 2s .c o m
    final Canvas canvas = new Canvas(bitmap);
    vector.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vector.draw(canvas);
    return bitmap;
}

From source file:android.support.graphics.drawable.AnimationUtilsCompat.java

/**
 * Loads an {@link Interpolator} object from a resource
 *
 * @param context Application context used to access resources
 * @param id      The resource id of the animation to load
 * @return The animation object reference by the specified id
 *///from w  w w.j a v  a  2s  .c o m
public static Interpolator loadInterpolator(Context context, int id) throws NotFoundException {
    // From API 21, we added path Interpolator .
    if (Build.VERSION.SDK_INT >= 21) {
        return AnimationUtils.loadInterpolator(context, id);
    }

    XmlResourceParser parser = null;
    try {
        // Special treatment for the interpolator introduced at API 21.
        if (id == AndroidResources.FAST_OUT_LINEAR_IN) {
            return new FastOutLinearInInterpolator();
        } else if (id == AndroidResources.FAST_OUT_SLOW_IN) {
            return new FastOutSlowInInterpolator();
        } else if (id == AndroidResources.LINEAR_OUT_SLOW_IN) {
            return new LinearOutSlowInInterpolator();
        }
        parser = context.getResources().getAnimation(id);
        return createInterpolatorFromXml(context, context.getResources(), context.getTheme(), parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }

}

From source file:eu.faircode.netguard.Util.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static void setTaskColor(Context context) {
    TypedValue tv = new TypedValue();
    context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
    ((Activity) context).setTaskDescription(new ActivityManager.TaskDescription(null, null, tv.data));
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static int getDisabledThemeAttrColor(Context context, @AttrRes int attr) {
    final ColorStateList csl = getThemeAttrColorStateList(context, attr);
    if (csl != null && csl.isStateful()) {
        // If the CSL is stateful, we'll assume it has a disabled state and use it
        return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor());
    } else {// ww  w  . j  av  a  2s .c o m
        // Else, we'll generate the color using disabledAlpha from the theme

        final TypedValue tv = getTypedValue();
        // Now retrieve the disabledAlpha value from the theme
        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true);
        final float disabledAlpha = tv.getFloat();

        return getThemeAttrColor(context, attr, disabledAlpha);
    }
}

From source file:com.beesham.beerac.ui.ScrollAwareFABBehavior.java

public int getToolbarHeight(Context context) {
    final TypedArray styledAttributes = context.getTheme()
            .obtainStyledAttributes(new int[] { R.attr.actionBarSize });
    int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
    styledAttributes.recycle();/*from   w  ww.j  a v a2  s  . c  om*/

    return toolbarHeight;
}

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

private void init(Context context) {
    Resources.Theme theme = context.getTheme();
    theme.resolveAttribute(android.R.attr.windowMinWidthMajor, minWidthMajor, true);
    theme.resolveAttribute(android.R.attr.windowMinWidthMinor, minWidthMinor, true);

    Drawable drawable = ResourcesUtils.getAttrDrawable(context, android.R.attr.windowBackground);
    ViewCompat.setBackground(this, drawable);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        float elevation = ResourcesUtils.getAttrDimension(context, android.R.attr.windowElevation);
        ViewCompat.setElevation(this, elevation);
    }//from ww  w  .  j ava  2  s  .co  m
}

From source file:org.voidsink.anewjkuapp.fragment.AboutFragment.java

private Colors getActivityColor(Context context) {

    TypedArray themeArray = context.getTheme()
            .obtainStyledAttributes(new int[] { R.attr.colorPrimary, R.attr.colorPrimaryDark });
    int colorPrimary = themeArray.getColor(0, ContextCompat.getColor(context, R.color.default_primary));
    int colorPrimaryDark = themeArray.getColor(1, ContextCompat.getColor(context, R.color.default_primaryDark));
    themeArray.recycle();/*  w  ww  . j  av  a  2  s  .c  o m*/

    return new Colors(colorPrimary, colorPrimaryDark);
}