Example usage for android.util Xml asAttributeSet

List of usage examples for android.util Xml asAttributeSet

Introduction

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

Prototype

public static AttributeSet asAttributeSet(XmlPullParser parser) 

Source Link

Document

Return an AttributeSet interface for use with the given XmlPullParser.

Usage

From source file:com.hippo.anani.AnimationUtils.java

private static Interpolator createInterpolatorFromXml(Context context, XmlPullParser parser)
        throws XmlPullParserException, IOException {

    Interpolator interpolator = null;/*from   ww w. j  ava 2  s .  c om*/

    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();

    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
            && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        AttributeSet attrs = Xml.asAttributeSet(parser);

        String name = parser.getName();

        if (name.equals("linearInterpolator")) {
            interpolator = new LinearInterpolator();
        } else if (name.equals("accelerateInterpolator")) {
            interpolator = new AccelerateInterpolator(context, attrs);
        } else if (name.equals("decelerateInterpolator")) {
            interpolator = new DecelerateInterpolator(context, attrs);
        } else if (name.equals("accelerateDecelerateInterpolator")) {
            interpolator = new AccelerateDecelerateInterpolator();
        } else if (name.equals("cycleInterpolator")) {
            interpolator = new CycleInterpolator(context, attrs);
        } else if (name.equals("anticipateInterpolator")) {
            interpolator = new AnticipateInterpolator(context, attrs);
        } else if (name.equals("overshootInterpolator")) {
            interpolator = new OvershootInterpolator(context, attrs);
        } else if (name.equals("anticipateOvershootInterpolator")) {
            interpolator = new AnticipateOvershootInterpolator(context, attrs);
        } else if (name.equals("bounceInterpolator")) {
            interpolator = new BounceInterpolator();
        } else if (name.equals("pathInterpolator")) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                interpolator = new android.view.animation.PathInterpolator(context, attrs);
            } else {
                interpolator = new PathInterpolator(context, attrs);
            }
        } else {
            throw new RuntimeException("Unknown interpolator name: " + parser.getName());
        }
    }
    return interpolator;
}

From source file:com.givewaygames.transition.TransitionInflater.java

/**
 * Loads a {@link Transition} object from a resource
 *
 * @param resource The resource id of the transition to load
 * @return The loaded Transition object/* w  w  w.  j a  v a2 s .  com*/
 * @throws android.content.res.Resources.NotFoundException when the
 * transition cannot be loaded
 */
public Transition inflateTransition(int resource) {
    XmlResourceParser parser = mContext.getResources().getXml(resource);
    try {
        return createTransitionFromXml(parser, Xml.asAttributeSet(parser), null);
    } catch (XmlPullParserException e) {
        InflateException ex = new InflateException(e.getMessage());
        ex.initCause(e);
        throw ex;
    } catch (IOException e) {
        InflateException ex = new InflateException(parser.getPositionDescription() + ": " + e.getMessage());
        ex.initCause(e);
        throw ex;
    } finally {
        parser.close();
    }
}

From source file:com.undatech.opaque.dialogs.SelectTextElementFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    android.util.Log.e(TAG, "onCreateView called");

    XmlPullParser parser = getResources().getXml(R.layout.textelement);
    AttributeSet attributes = Xml.asAttributeSet(parser);

    // Set title for this dialog
    getDialog().setTitle(title);//  ww w  .j a va2  s. c  o  m

    View v = inflater.inflate(R.layout.select_text, container, false);

    verticalLayout = (LinearLayout) v.findViewById(R.id.verticalLayout);
    ListIterator<String> iter = strings.listIterator();
    while (iter.hasNext()) {
        android.util.Log.e(TAG, "Adding element to dialog");
        String string = iter.next();
        TextView element = new TextView(v.getContext(), attributes);
        element.setText(string);
        element.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25.f);
        element.setPadding(40, 20, 40, 20);
        element.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectTextElementFragment.this.selected = ((TextView) v).getText().toString();
                SelectTextElementFragment.this.dismiss();
            }
        });
        verticalLayout.addView(element);
    }

    return v;
}

From source file:me.henrytao.mdcore.core.MdCompat.java

/**
 * A temporary fix for android.content.res.ColorStateList.inflate
 */// w  w  w .ja  v a  2  s. c o m
public static ColorStateList getColorStateList(Context context, int resId)
        throws IOException, XmlPullParserException {
    XmlResourceParser parser = context.getResources().getXml(resId);
    AttributeSet attrs = Xml.asAttributeSet(parser);
    Resources r = context.getResources();

    final int innerDepth = parser.getDepth() + 2;
    int depth;
    int type;

    List<int[]> customStateList = new ArrayList<>();
    List<Integer> customColorList = new ArrayList<>();

    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;
        }
        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int color = 0;
        float alpha = 1.0f;
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            switch (stateResId) {
            case android.R.attr.color:
                int colorAttrId = attrs.getAttributeResourceValue(i, 0);
                if (colorAttrId == 0) {
                    String colorAttrValue = attrs.getAttributeValue(i);
                    colorAttrId = Integer.valueOf(colorAttrValue.replace("?", ""));
                    color = getColorFromAttribute(context, colorAttrId);
                } else {
                    color = ContextCompat.getColor(context, colorAttrId);
                }
                break;
            case android.R.attr.alpha:
                try {
                    alpha = attrs.getAttributeFloatValue(i, 1.0f);
                } catch (Exception e) {
                    String alphaAttrValue = attrs.getAttributeValue(i);
                    alpha = getFloatFromAttribute(context, Integer.valueOf(alphaAttrValue.replace("?", "")));
                }
                break;
            default:
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);
        color = modulateColorAlpha(color, alpha);

        customColorList.add(color);
        customStateList.add(stateSpec);
    }

    int[] colors = new int[customColorList.size()];
    int[][] states = new int[customStateList.size()][];
    int i = 0;
    for (int n = states.length; i < n; i++) {
        colors[i] = customColorList.get(i);
        states[i] = customStateList.get(i);
    }
    return new ColorStateList(states, colors);
}

From source file:android.support.transition.TransitionInflater.java

/**
 * Loads a {@link TransitionManager} object from a resource
 *
 * @param resource The resource id of the transition manager to load
 * @return The loaded TransitionManager object
 * @throws android.content.res.Resources.NotFoundException when the
 *                                                         transition manager cannot be loaded
 *//*from   w  w  w .jav  a 2 s  .c  o  m*/
public TransitionManager inflateTransitionManager(int resource, ViewGroup sceneRoot) {
    XmlResourceParser parser = mContext.getResources().getXml(resource);
    try {
        return createTransitionManagerFromXml(parser, Xml.asAttributeSet(parser), sceneRoot);
    } catch (XmlPullParserException e) {
        InflateException ex = new InflateException(e.getMessage());
        ex.initCause(e);
        throw ex;
    } catch (IOException e) {
        InflateException ex = new InflateException(parser.getPositionDescription() + ": " + e.getMessage());
        ex.initCause(e);
        throw ex;
    } finally {
        parser.close();
    }
}

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

private static Interpolator createInterpolatorFromXml(Context context, Resources res, Theme theme,
        XmlPullParser parser) throws XmlPullParserException, IOException {

    Interpolator interpolator = null;/* w w  w .ja v  a2 s . c o  m*/

    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();

    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
            && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        AttributeSet attrs = Xml.asAttributeSet(parser);

        String name = parser.getName();

        if (name.equals("linearInterpolator")) {
            interpolator = new LinearInterpolator();
        } else if (name.equals("accelerateInterpolator")) {
            interpolator = new AccelerateInterpolator(context, attrs);
        } else if (name.equals("decelerateInterpolator")) {
            interpolator = new DecelerateInterpolator(context, attrs);
        } else if (name.equals("accelerateDecelerateInterpolator")) {
            interpolator = new AccelerateDecelerateInterpolator();
        } else if (name.equals("cycleInterpolator")) {
            interpolator = new CycleInterpolator(context, attrs);
        } else if (name.equals("anticipateInterpolator")) {
            interpolator = new AnticipateInterpolator(context, attrs);
        } else if (name.equals("overshootInterpolator")) {
            interpolator = new OvershootInterpolator(context, attrs);
        } else if (name.equals("anticipateOvershootInterpolator")) {
            interpolator = new AnticipateOvershootInterpolator(context, attrs);
        } else if (name.equals("bounceInterpolator")) {
            interpolator = new BounceInterpolator();
        } else if (name.equals("pathInterpolator")) {
            interpolator = new PathInterpolatorCompat(context, attrs, parser);
        } else {
            throw new RuntimeException("Unknown interpolator name: " + parser.getName());
        }
    }
    return interpolator;
}

From source file:android.support.v7.internal.view.SupportMenuInflater.java

/**
 * Inflate a menu hierarchy from the specified XML resource. Throws
 * {@link InflateException} if there is an error.
 *
 * @param menuRes Resource ID for an XML layout resource to load (e.g.,
 *            <code>R.menu.main_activity</code>)
 * @param menu The Menu to inflate into. The items and submenus will be
 *            added to this Menu./*from w ww . j  a v a2  s .co  m*/
 */
@Override
public void inflate(int menuRes, Menu menu) {
    // If we're not dealing with a SupportMenu instance, let super handle
    if (!(menu instanceof SupportMenu)) {
        super.inflate(menuRes, menu);
        return;
    }

    XmlResourceParser parser = null;
    try {
        parser = mContext.getResources().getLayout(menuRes);
        AttributeSet attrs = Xml.asAttributeSet(parser);

        parseMenu(parser, attrs, menu);
    } catch (XmlPullParserException e) {
        throw new InflateException("Error inflating menu XML", e);
    } catch (IOException e) {
        throw new InflateException("Error inflating menu XML", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}

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.
 *///  ww  w .  j  a v  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:com.actionbarsherlock.view.MenuInflater.java

/**
 * Inflate a menu hierarchy from the specified XML resource. Throws
 * {@link InflateException} if there is an error.
 *
 * @param menuRes Resource ID for an XML layout resource to load (e.g.,
 *            <code>R.menu.main_activity</code>)
 * @param menu The Menu to inflate into. The items and submenus will be
 *            added to this Menu.// w  ww.  j ava  2s.c  o  m
 */
public void inflate(int menuRes, Menu menu) {
    XmlResourceParser parser = null;
    try {
        parser = mContext.getResources().getLayout(menuRes);
        AttributeSet attrs = Xml.asAttributeSet(parser);

        parseMenu(parser, attrs, menu);
    } catch (XmlPullParserException e) {
        throw new InflateException("Error inflating menu XML", e);
    } catch (IOException e) {
        throw new InflateException("Error inflating menu XML", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}

From source file:lewa.support.v7.internal.view.SupportMenuInflater.java

/**
 * Inflate a menu hierarchy from the specified XML resource. Throws
 * {@link InflateException} if there is an error.
 *
 * @param menuRes Resource ID for an XML layout resource to load (e.g.,
 *            <code>R.menu.main_activity</code>)
 * @param menu The Menu to inflate into. The items and submenus will be
 *            added to this Menu.//from  w  ww  . ja v a2  s. com
 */
@Override
public void inflate(int menuRes, Menu menu) {
    // If we're not dealing with a SupportMenu instance, let super handle
    if (!(menu instanceof SupportMenu)) {
        super.inflate(menuRes, menu);
        return;
    }
    XmlResourceParser parser = null;
    try {
        parser = mContext.getResources().getLayout(menuRes);
        AttributeSet attrs = Xml.asAttributeSet(parser);

        parseMenu(parser, attrs, menu);
    } catch (XmlPullParserException e) {
        throw new InflateException("Error inflating menu XML", e);
    } catch (IOException e) {
        throw new InflateException("Error inflating menu XML", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}