Example usage for android.content.res TypedArray getResourceId

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

Introduction

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

Prototype

@AnyRes
public int getResourceId(@StyleableRes int index, int defValue) 

Source Link

Document

Retrieves the resource identifier for the attribute at index.

Usage

From source file:com.amitupadhyay.aboutexample.ui.widget.FabOverlapTextView.java

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FabOverlapTextView);

    float defaultTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, DEFAULT_TEXT_SIZE_SP,
            getResources().getDisplayMetrics());

    setFabOverlapGravity(a.getInt(R.styleable.FabOverlapTextView_fabGravity, Gravity.BOTTOM | Gravity.RIGHT));
    setFabOverlapHeight(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_fabOverlayHeight, 0));
    setFabOverlapWidth(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_fabOverlayWidth, 0));

    if (a.hasValue(R.styleable.FabOverlapTextView_android_textAppearance)) {
        final int textAppearance = a.getResourceId(R.styleable.FabOverlapTextView_android_textAppearance,
                android.R.style.TextAppearance);
        TypedArray atp = getContext().obtainStyledAttributes(textAppearance, R.styleable.FontTextAppearance);
        paint.setColor(atp.getColor(R.styleable.FontTextAppearance_android_textColor, Color.BLACK));
        paint.setTextSize(atp.getDimensionPixelSize(R.styleable.FontTextAppearance_android_textSize,
                (int) defaultTextSize));
        if (atp.hasValue(R.styleable.FontTextAppearance_font)) {
            paint.setTypeface(FontUtil.get(getContext(), atp.getString(R.styleable.FontTextAppearance_font)));
        }//from ww w  . j  av a  2s .  c  o  m
        atp.recycle();
    }

    if (a.hasValue(R.styleable.FabOverlapTextView_font)) {
        setFont(a.getString(R.styleable.FabOverlapTextView_font));
    }

    if (a.hasValue(R.styleable.FabOverlapTextView_android_textColor)) {
        setTextColor(a.getColor(R.styleable.FabOverlapTextView_android_textColor, 0));
    }
    if (a.hasValue(R.styleable.FabOverlapTextView_android_textSize)) {
        setTextSize(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_android_textSize,
                (int) defaultTextSize));
    }

    lineHeightHint = a.getDimensionPixelSize(R.styleable.FabOverlapTextView_lineHeightHint, 0);
    unalignedTopPadding = getPaddingTop();
    unalignedBottomPadding = getPaddingBottom();

    breakStrategy = a.getInt(R.styleable.FabOverlapTextView_android_breakStrategy,
            Layout.BREAK_STRATEGY_BALANCED);

    a.recycle();
}

From source file:com.evilduck.animtest.DraggedPanelLayout.java

public void initAttrs(Context context, AttributeSet attrs) {
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DraggedPanelLayout, 0, 0);

    try {//from ww  w.  ja v a 2 s.  com
        parallaxFactor = a.getFloat(R.styleable.DraggedPanelLayout_parallax_factor, PARALLAX_FACTOR);
        if (parallaxFactor < 0.1 || parallaxFactor > 0.9) {
            parallaxFactor = PARALLAX_FACTOR;
        }

        int defaultHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100,
                getResources().getDisplayMetrics());
        bottomPanelPeekHeight = a.getDimensionPixelSize(R.styleable.DraggedPanelLayout_bottom_panel_height,
                defaultHeight);
        int shadowDrawableId = a.getResourceId(R.styleable.DraggedPanelLayout_shadow_drawable, -1);
        if (shadowDrawableId != -1) {
            shadowDrawable = getResources().getDrawable(shadowDrawableId);
            willDrawShadow = true;
            setWillNotDraw(!willDrawShadow);
        }
    } finally {
        a.recycle();
    }

    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    touchSlop = configuration.getScaledTouchSlop();
}

From source file:bottombar.BottomBar.java

private void populateAttributes(Context context, AttributeSet attrs) {
    primaryColor = MiscUtils.getColor(getContext(), R.attr.colorPrimary);
    screenWidth = MiscUtils.getScreenWidth(getContext());
    tenDp = MiscUtils.dpToPixel(getContext(), 10);
    maxFixedItemWidth = MiscUtils.dpToPixel(getContext(), 168);

    TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BottomBar, 0, 0);

    try {//from w  w  w .  j ava 2  s .com
        tabXmlResource = ta.getResourceId(R.styleable.BottomBar_bb_tabXmlResource, 0);
        isTabletMode = ta.getBoolean(R.styleable.BottomBar_bb_tabletMode, false);
        behaviors = ta.getInteger(R.styleable.BottomBar_bb_behavior, BEHAVIOR_NONE);
        inActiveTabAlpha = ta.getFloat(R.styleable.BottomBar_bb_inActiveTabAlpha,
                isShiftingMode() ? DEFAULT_INACTIVE_SHIFTING_TAB_ALPHA : 1);
        activeTabAlpha = ta.getFloat(R.styleable.BottomBar_bb_activeTabAlpha, 1);

        @ColorInt
        int defaultInActiveColor = isShiftingMode() ? Color.WHITE
                : ContextCompat.getColor(context, R.color.bb_inActiveBottomBarItemColor);
        int defaultActiveColor = isShiftingMode() ? Color.WHITE : primaryColor;

        inActiveTabColor = ta.getColor(R.styleable.BottomBar_bb_inActiveTabColor, defaultInActiveColor);
        activeTabColor = ta.getColor(R.styleable.BottomBar_bb_activeTabColor, defaultActiveColor);
        badgeBackgroundColor = ta.getColor(R.styleable.BottomBar_bb_badgeBackgroundColor, Color.RED);
        hideBadgeWhenActive = ta.getBoolean(R.styleable.BottomBar_bb_badgesHideWhenActive, true);
        titleTextAppearance = ta.getResourceId(R.styleable.BottomBar_bb_titleTextAppearance, 0);
        titleTypeFace = getTypeFaceFromAsset(ta.getString(R.styleable.BottomBar_bb_titleTypeFace));
        showShadow = ta.getBoolean(R.styleable.BottomBar_bb_showShadow, true);
    } finally {
        ta.recycle();
    }
}

From source file:com.devbrackets.android.recyclerext.widget.FastScroll.java

/**
 * Retrieves the specified image drawable in a manner that will correctly
 * wrap VectorDrawables on platforms that don't natively support them
 *
 * @param typedArray The TypedArray containing the attributes for the view
 * @param index The index in the {@code typedArray} for the drawable
 *///from   www  . j a  v  a2  s . c  o m
@Nullable
protected Drawable getDrawable(@NonNull TypedArray typedArray, int index) {
    int imageResId = typedArray.getResourceId(index, 0);
    if (imageResId == 0) {
        return null;
    }

    return AppCompatDrawableManager.get().getDrawable(getContext(), imageResId);
}

From source file:com.androzic.Preferences.java

/**
 * Parse the given XML file as a header description, adding each
 * parsed Header into the target list.//from www .j av a 2s  .c o  m
 *
 * @param resid
 *            The XML resource to load and parse.
 * @param target
 *            The list in which the parsed headers should be placed.
 */
public void loadHeadersFromResource(int resid, List<Header> target) {
    Androzic application = Androzic.getApplication();
    XmlResourceParser parser = null;
    try {
        Resources resources = getResources();
        parser = resources.getXml(resid);
        AttributeSet attrs = Xml.asAttributeSet(parser);

        int type;
        //noinspection StatementWithEmptyBody
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
            // Parse next until start tag is found
        }

        String nodeName = parser.getName();
        if (!"preference-headers".equals(nodeName)) {
            throw new RuntimeException("XML document must start with <preference-headers> tag; found" + nodeName
                    + " at " + parser.getPositionDescription());
        }

        Bundle curBundle = null;

        final int outerDepth = parser.getDepth();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }

            nodeName = parser.getName();
            if ("header".equals(nodeName)) {
                Header header = new Header();

                TypedArray sa = resources.obtainAttributes(attrs, R.styleable.PreferenceHeader);
                header.id = sa.getResourceId(R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED);
                TypedValue tv = sa.peekValue(R.styleable.PreferenceHeader_title);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.titleRes = tv.resourceId;
                    } else {
                        header.title = tv.string;
                    }
                }
                tv = sa.peekValue(R.styleable.PreferenceHeader_summary);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.summaryRes = tv.resourceId;
                    } else {
                        header.summary = tv.string;
                    }
                }
                tv = sa.peekValue(R.styleable.PreferenceHeader_breadCrumbTitle);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.breadCrumbTitleRes = tv.resourceId;
                    } else {
                        header.breadCrumbTitle = tv.string;
                    }
                }
                tv = sa.peekValue(R.styleable.PreferenceHeader_breadCrumbShortTitle);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.breadCrumbShortTitleRes = tv.resourceId;
                    } else {
                        header.breadCrumbShortTitle = tv.string;
                    }
                }
                header.iconRes = sa.getResourceId(R.styleable.PreferenceHeader_icon, 0);
                header.fragment = sa.getString(R.styleable.PreferenceHeader_fragment);
                header.help = sa.getResourceId(R.styleable.PreferenceHeader_help, 0);
                sa.recycle();

                if (curBundle == null) {
                    curBundle = new Bundle();
                }

                final int innerDepth = parser.getDepth();
                while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                        && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
                    if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                        continue;
                    }

                    String innerNodeName = parser.getName();
                    if (innerNodeName.equals("extra")) {
                        resources.parseBundleExtra(innerNodeName, attrs, curBundle);
                        XmlUtils.skipCurrentTag(parser);

                    } else if (innerNodeName.equals("intent")) {
                        header.intent = Intent.parseIntent(resources, parser, attrs);

                    } else {
                        XmlUtils.skipCurrentTag(parser);
                    }
                }

                if (curBundle.size() > 0) {
                    header.fragmentArguments = curBundle;
                    curBundle = null;
                }

                if (header.id == R.id.pref_plugins && application.getPluginsPreferences().size() == 0)
                    continue;

                target.add(header);
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }

    } catch (XmlPullParserException | IOException e) {
        throw new RuntimeException("Error parsing headers", e);
    } finally {
        if (parser != null)
            parser.close();
    }

}

From source file:com.hippo.preference.DialogPreference.java

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DialogPreference, defStyleAttr,
            defStyleRes);//w  w w.  java 2  s .c  o m
    mDialogTitle = a.getString(R.styleable.DialogPreference_dialogTitle);
    if (mDialogTitle == null) {
        // Fallback on the regular title of the preference
        // (the one that is seen in the list)
        mDialogTitle = getTitle();
    }
    mDialogIcon = a.getDrawable(R.styleable.DialogPreference_dialogIcon);
    mPositiveButtonText = a.getString(R.styleable.DialogPreference_positiveButtonText);
    mNegativeButtonText = a.getString(R.styleable.DialogPreference_negativeButtonText);
    mDialogLayoutResId = a.getResourceId(R.styleable.DialogPreference_dialogLayout, mDialogLayoutResId);
    a.recycle();
}

From source file:com.chuhan.privatecalc.fragment.os.FragmentActivity.java

/**
 * Add support for inflating the &lt;fragment> tag.
 *//*from w  w w.  j a v a  2s.  c  om*/
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    if (!"fragment".equals(name)) {
        return super.onCreateView(name, context, attrs);
    }

    String fname = attrs.getAttributeValue(null, "class");
    TypedArray a = context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
    if (fname == null) {
        fname = a.getString(FragmentTag.Fragment_name);
    }
    int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
    String tag = a.getString(FragmentTag.Fragment_tag);
    a.recycle();

    View parent = null; // NOTE: no way to get parent pre-Honeycomb.
    int containerId = parent != null ? parent.getId() : 0;
    if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
        throw new IllegalArgumentException(attrs.getPositionDescription()
                + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
    }

    // If we restored from a previous state, we may already have
    // instantiated this fragment from the state and should use
    // that instance instead of making a new one.
    Fragment fragment = id != View.NO_ID ? mFragments.findFragmentById(id) : null;
    if (fragment == null && tag != null) {
        fragment = mFragments.findFragmentByTag(tag);
    }
    if (fragment == null && containerId != View.NO_ID) {
        fragment = mFragments.findFragmentById(containerId);
    }

    if (FragmentManagerImpl.DEBUG)
        Log.v(TAG,
                "onCreateView: id=0x" + Integer.toHexString(id) + " fname=" + fname + " existing=" + fragment);
    if (fragment == null) {
        fragment = Fragment.instantiate(this, fname);
        fragment.mFromLayout = true;
        fragment.mFragmentId = id != 0 ? id : containerId;
        fragment.mContainerId = containerId;
        fragment.mTag = tag;
        fragment.mInLayout = true;
        fragment.mFragmentManager = mFragments;
        fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
        mFragments.addFragment(fragment, true);

    } else if (fragment.mInLayout) {
        // A fragment already exists and it is not one we restored from
        // previous state.
        throw new IllegalArgumentException(attrs.getPositionDescription() + ": Duplicate id 0x"
                + Integer.toHexString(id) + ", tag " + tag + ", or parent id 0x"
                + Integer.toHexString(containerId) + " with another fragment for " + fname);
    } else {
        // This fragment was retained from a previous instance; get it
        // going now.
        fragment.mInLayout = true;
        // If this fragment is newly instantiated (either right now, or
        // from last saved state), then give it the attributes to
        // initialize itself.
        if (!fragment.mRetaining) {
            fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
        }
        mFragments.moveToState(fragment);
    }

    if (fragment.mView == null) {
        throw new IllegalStateException("Fragment " + fname + " did not create a view.");
    }
    if (id != 0) {
        fragment.mView.setId(id);
    }
    if (fragment.mView.getTag() == null) {
        fragment.mView.setTag(tag);
    }
    return fragment.mView;
}

From source file:android.support.v7.app.AlertController.java

public AlertController(Context context, AppCompatDialog di, Window window) {
    mContext = context;//from w w w.  ja va 2 s.  co  m
    mDialog = di;
    mWindow = window;
    mHandler = new ButtonHandler(di);

    final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog, R.attr.alertDialogStyle,
            0);

    mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);
    mButtonPanelSideLayout = a.getResourceId(R.styleable.AlertDialog_buttonPanelSideLayout, 0);

    mListLayout = a.getResourceId(R.styleable.AlertDialog_listLayout, 0);
    mMultiChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_multiChoiceItemLayout, 0);
    mSingleChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0);
    mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);

    a.recycle();

    /* We use a custom title so never request a window title */
    di.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}

From source file:android.support.v7ox.app.AlertController.java

public AlertController(Context context, AppCompatDialog di, Window window) {
    mContext = context;/*from  ww  w.  ja  v a 2 s . c o  m*/
    mDialog = di;
    mWindow = window;
    mHandler = new ButtonHandler(di);

    final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog,
            R.attr.alertDialogStyle_ox, 0);

    mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);
    mButtonPanelSideLayout = a.getResourceId(R.styleable.AlertDialog_buttonPanelSideLayout, 0);

    mListLayout = a.getResourceId(R.styleable.AlertDialog_listLayout, 0);
    mMultiChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_multiChoiceItemLayout, 0);
    mSingleChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0);
    mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);

    a.recycle();
}

From source file:com.fortysevendeg.android.swipelistview.RefreshSwipeListView.java

/**
 * Init ListView//w ww .j  a v a 2 s  .  c om
 *
 * @param attrs AttributeSet
 */
@Override
protected void init(AttributeSet attrs) {

    int swipeMode = SWIPE_MODE_BOTH;
    boolean swipeOpenOnLongPress = true;
    boolean swipeCloseAllItemsWhenMoveList = true;
    long swipeAnimationTime = 0;
    float swipeOffsetLeft = 0;
    float swipeOffsetRight = 0;

    int swipeActionLeft = SWIPE_ACTION_REVEAL;
    int swipeActionRight = SWIPE_ACTION_REVEAL;

    if (attrs != null) {
        TypedArray styled = getContext().obtainStyledAttributes(attrs, R.styleable.SwipeListView);
        swipeMode = styled.getInt(R.styleable.SwipeListView_swipeMode, SWIPE_MODE_BOTH);
        swipeActionLeft = styled.getInt(R.styleable.SwipeListView_swipeActionLeft, SWIPE_ACTION_REVEAL);
        swipeActionRight = styled.getInt(R.styleable.SwipeListView_swipeActionRight, SWIPE_ACTION_REVEAL);
        swipeOffsetLeft = styled.getDimension(R.styleable.SwipeListView_swipeOffsetLeft, 0);
        swipeOffsetRight = styled.getDimension(R.styleable.SwipeListView_swipeOffsetRight, 0);
        swipeOpenOnLongPress = styled.getBoolean(R.styleable.SwipeListView_swipeOpenOnLongPress, true);
        swipeAnimationTime = styled.getInteger(R.styleable.SwipeListView_swipeAnimationTime, 0);
        swipeCloseAllItemsWhenMoveList = styled
                .getBoolean(R.styleable.SwipeListView_swipeCloseAllItemsWhenMoveList, true);
        mSwipeFrontView = styled.getResourceId(R.styleable.SwipeListView_swipeFrontView, 0);
        mSwipeBackView = styled.getResourceId(R.styleable.SwipeListView_swipeBackView, 0);
    }

    if (mSwipeFrontView == 0 || mSwipeBackView == 0) {
        mSwipeFrontView = getContext().getResources().getIdentifier(SWIPE_DEFAULT_FRONT_VIEW, "id",
                getContext().getPackageName());
        mSwipeBackView = getContext().getResources().getIdentifier(SWIPE_DEFAULT_BACK_VIEW, "id",
                getContext().getPackageName());

        if (mSwipeFrontView == 0 || mSwipeBackView == 0) {
            throw new RuntimeException(String.format(
                    "You forgot the attributes swipeFrontView or swipeBackView. You can add this attributes or use '%s' and '%s' identifiers",
                    SWIPE_DEFAULT_FRONT_VIEW, SWIPE_DEFAULT_BACK_VIEW));
        }
    }

    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    mTouchListener = new SwipeRefreshListViewTouchListener(this, mSwipeFrontView, mSwipeBackView);
    if (swipeAnimationTime > 0) {
        mTouchListener.setAnimationTime(swipeAnimationTime);
    }
    mTouchListener.setRightOffset(swipeOffsetRight);
    mTouchListener.setLeftOffset(swipeOffsetLeft);
    mTouchListener.setSwipeActionLeft(swipeActionLeft);
    mTouchListener.setSwipeActionRight(swipeActionRight);
    mTouchListener.setSwipeMode(swipeMode);
    mTouchListener.setSwipeClosesAllItemsWhenListMoves(swipeCloseAllItemsWhenMoveList);
    mTouchListener.setSwipeOpenOnLongPress(swipeOpenOnLongPress);
    setOnTouchListener(mTouchListener);
    setOnScrollListener(mTouchListener.makeScrollListener());
}