Example usage for android.content.res TypedArray recycle

List of usage examples for android.content.res TypedArray recycle

Introduction

In this page you can find the example usage for android.content.res TypedArray recycle.

Prototype

public void recycle() 

Source Link

Document

Recycles the TypedArray, to be re-used by a later caller.

Usage

From source file:Main.java

/**
 * Tries to pull the Font Path from the View Style as this is the next decendent after being
 * defined in the View's xml./* ww w .j ava2s. c o  m*/
 *
 * @param context     Activity Activity Context
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return null if attribute is not defined or found in the Style
 */
static String pullFontPathFromStyle(Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1)
        return null;
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { attributeId });
    if (typedArray != null) {
        try {
            // First defined attribute
            String fontFromAttribute = typedArray.getString(0);
            if (!TextUtils.isEmpty(fontFromAttribute)) {
                return fontFromAttribute;
            }
        } catch (Exception ignore) {
            // Failed for some reason.
        } finally {
            typedArray.recycle();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the View Style as this is the next decendent after being
 * defined in the View's xml./*from  ww w .  j  a va  2 s .c om*/
 *
 * @param context     Activity Activity Context
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return null if attribute is not defined or found in the Style
 */
static String pullFontPathFromStyle(Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1 || attrs == null)
        return null;
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { attributeId });
    if (typedArray != null) {
        try {
            // First defined attribute
            String fontFromAttribute = typedArray.getString(0);
            if (!TextUtils.isEmpty(fontFromAttribute)) {
                return fontFromAttribute;
            }
        } catch (Exception ignore) {
            // Failed for some reason.
        } finally {
            typedArray.recycle();
        }
    }
    return null;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method used for the floating action button behaviour.
 *//*from   www.j a v  a 2  s. com*/
public static int getToolbarHeight(Context context) {
    final TypedArray styledAttributes = context.getTheme()
            .obtainStyledAttributes(new int[] { R.attr.actionBarSize });
    int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
    styledAttributes.recycle();

    return toolbarHeight;
}

From source file:Main.java

/**
 * Tries to pull the Font Path from the View Style as this is the next decendent after being
 * defined in the View's xml.//from w w w .ja  v  a 2  s  .  c om
 *
 * @param context     Activity Activity Context
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return null if attribute is not defined or found in the Style
 */
static String pullFontPathFromStyle(Context context, AttributeSet attrs, int attributeId) {
    if (attributeId == -1 || attrs == null)
        return null;
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, getAttributeArray(attributeId));
    if (typedArray != null) {
        try {
            // First defined attribute
            String fontFromAttribute = typedArray.getString(0);
            if (!TextUtils.isEmpty(fontFromAttribute)) {
                return fontFromAttribute;
            }
        } catch (Exception ignore) {
            // Failed for some reason.
        } finally {
            typedArray.recycle();
        }
    }
    return null;
}

From source file:ca.marklauman.dominionpicker.CardAdapter.java

/** Retrieve an array of drawable resources from
 *  the xml of the provided {@link Context}.
 *  @param c The {@code Context} to search for the array.
 *  @param resourceId The resource id of an
 *  {@code <array>} containing a list of drawables.
 *  @return The resource ids of all the drawables
 *  in the array, in the order in which they appear
 *  in the xml.                                  */
public static int[] getDrawables(Context c, int resourceId) {
    TypedArray ta = c.getResources().obtainTypedArray(resourceId);
    if (ta == null)
        return null;

    int[] res = new int[ta.length()];
    for (int i = 0; i < ta.length(); i++)
        res[i] = ta.getResourceId(i, -1);

    ta.recycle();
    return res;//from www  .j ava2  s . c o m
}

From source file:bander.notepad.Notepad.java

/**
 * Sets the color of the {@link android.support.v7.widget.Toolbar}
 *
 * @param activity Used similarly to {@link android.content.Context}
 *//*from w  ww.ja  v a2s  . c  o  m*/
public static void setToolbarColor(final ActionBarActivity activity) {
    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(activity);

    /**
     * The lighter toolbar color {@link com.devin.notepad.R.array.color500}
     */
    TypedArray m500ta = activity.getResources().obtainTypedArray(R.array.color500);
    final int[] mToolbarColors = new int[m500ta.length()];
    for (int i = 0; i < m500ta.length(); i++) {
        mToolbarColors[i] = m500ta.getColor(i, 0);
    }
    m500ta.recycle();

    /**
     * The darker status bar color. {@link com.devin.notepad.R.array.color700}
     */
    TypedArray m700ta = activity.getResources().obtainTypedArray(R.array.color700);
    int[] statusBarColors = new int[m700ta.length()];
    for (int i = 0; i < m700ta.length(); i++) {
        statusBarColors[i] = m700ta.getColor(i, 0);
    }
    m700ta.recycle();

    final int abc = mSettings.getInt("actionBarColor", 0);

    Toolbar mToolbar = (Toolbar) activity.findViewById(R.id.toolbar);

    if (mToolbar != null)
        mToolbar.setBackgroundColor(mToolbarColors[abc]);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        activity.getWindow().setStatusBarColor(statusBarColors[abc]);
    }
}

From source file:android.support.v7.internal.widget.ViewUtils.java

/**
 * Allows us to emulate the {@code android:theme} attribute for devices before L.
 *//*  www . j  av a 2s.  c om*/
public static Context themifyContext(Context context, AttributeSet attrs, boolean useAndroidTheme,
        boolean useAppTheme) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.View, 0, 0);
    int themeId = 0;
    if (useAndroidTheme) {
        // First try reading android:theme if enabled
        themeId = a.getResourceId(R.styleable.View_android_theme, 0);
    }
    if (useAppTheme && themeId == 0) {
        // ...if that didn't work, try reading app:theme (for legacy reasons) if enabled
        themeId = a.getResourceId(R.styleable.View_theme, 0);

        if (themeId != 0) {
            Log.i(TAG, "app:theme is now deprecated. Please move to using android:theme instead.");
        }
    }
    a.recycle();

    if (themeId != 0 && (!(context instanceof ContextThemeWrapper)
            || ((ContextThemeWrapper) context).getThemeResId() != themeId)) {
        // If the context isn't a ContextThemeWrapperCompat, or it is but does not have
        // the same theme as we need, wrap it in a new wrapper
        context = new ContextThemeWrapper(context, themeId);
    }
    return context;
}

From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java

/**
 * Fill in this object based on the contents of an XML "selector" element.
 *//*  www. j  ava2s  .  co m*/
private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
        @NonNull AttributeSet attrs, @Nullable Resources.Theme theme)
        throws XmlPullParserException, IOException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    int defaultColor = DEFAULT_COLOR;

    int[][] stateSpecList = new int[20][];
    int[] colorList = new int[stateSpecList.length];
    int listSize = 0;

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem);
        final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA);

        float alphaMod = 1.0f;
        if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod);
        } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod);
        }

        a.recycle();

        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha
                    && stateResId != R.attr.alpha) {
                // Unrecognized attribute, add to state set
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);

        // Apply alpha modulation. If we couldn't resolve the color or
        // alpha yet, the default values leave us enough information to
        // modulate again during applyTheme().
        final int color = modulateColorAlpha(baseColor, alphaMod);
        if (listSize == 0 || stateSpec.length == 0) {
            defaultColor = color;
        }

        colorList = GrowingArrayUtils.append(colorList, listSize, color);
        stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec);
        listSize++;
    }

    int[] colors = new int[listSize];
    int[][] stateSpecs = new int[listSize][];
    System.arraycopy(colorList, 0, colors, 0, listSize);
    System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize);

    return new ColorStateList(stateSpecs, colors);
}

From source file:info.papdt.blacklight.support.Utility.java

public static int getColorPrimary(Context context) {
    try {//from   w w w  . j  av  a  2s.  c o  m
        TypedArray array = context.obtainStyledAttributes(R.styleable.Theme);
        int ret = array.getColor(R.styleable.Theme_colorPrimary, 0);
        array.recycle();
        return ret;
    } catch (NotFoundException e) {
        return 0;
    }
}

From source file:info.papdt.blacklight.support.Utility.java

public static int getColorPrimaryDark(Context context) {
    try {//w  w  w  . j  a va2 s  .  c  om
        TypedArray array = context.obtainStyledAttributes(R.styleable.Theme);
        int ret = array.getColor(R.styleable.Theme_colorPrimaryDark, 0);
        array.recycle();
        return ret;
    } catch (NotFoundException e) {
        return 0;
    }
}