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.daiv.android.twitter.ui.drawer_activities.DrawerActivity.java

public void setNotificationFilled(boolean isFilled) {
    if (isFilled) {
        TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.notification_button });
        int resource = a.getResourceId(0, 0);
        a.recycle();/*from  w  w  w .j  a va2 s . c  om*/
        noti.setIcon(resource);
    } else {
        TypedArray a = context.getTheme()
                .obtainStyledAttributes(new int[] { R.attr.notification_button_empty });
        int resource = a.getResourceId(0, 0);
        a.recycle();
        noti.setIcon(resource);
    }
}

From source file:com.albedinsky.android.ui.widget.ViewPagerWidget.java

/**
 * Called from one of constructors of this view to perform its initialization.
 * <p>/*from  w w  w.  j av a2 s. com*/
 * Initialization is done via parsing of the specified <var>attrs</var> set and obtaining for
 * this view specific data from it that can be used to configure this new view instance. The
 * specified <var>defStyleAttr</var> and <var>defStyleRes</var> are used to obtain default data
 * from the current theme provided by the specified <var>context</var>.
 */
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    this.ensureDecorator();
    mDecorator.processAttributes(context, attrs, defStyleAttr, defStyleRes);

    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Ui_ViewPager, defStyleAttr,
            defStyleRes);
    if (typedArray != null) {
        final int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            final int index = typedArray.getIndex(i);
            if (index == R.styleable.Ui_ViewPager_android_background) {
                int resID = typedArray.getResourceId(index, -1);
                if (resID != -1) {
                    setBackgroundResource(resID);
                } else {
                    setBackgroundColor(typedArray.getColor(0, Color.TRANSPARENT));
                }
            } else if (index == R.styleable.Ui_ViewPager_uiPageMargin) {
                setPageMargin(typedArray.getDimensionPixelSize(index, 0));
            } else if (index == R.styleable.Ui_ViewPager_uiPageMarginDrawable) {
                setPageMarginDrawable(typedArray.getDrawable(index));
            } else if (index == R.styleable.Ui_ViewPager_uiPageSwipingEnabled) {
                mDecorator.updatePrivateFlags(PFLAG_PAGE_SWIPING_ENABLED, typedArray.getBoolean(index, true));
            } else if (index == R.styleable.Ui_ViewPager_uiPageFlingSwipingEnabled) {
                mDecorator.updatePrivateFlags(PFLAG_PAGE_FLING_SWIPING_ENABLED,
                        typedArray.getBoolean(index, false));
            } else if (index == R.styleable.Ui_ViewPager_uiPageFlingSwipingSensitivity) {
                this.mPageFlingSwipingSensitivity = Math.max(0,
                        typedArray.getFloat(index, mPageFlingSwipingSensitivity));
            } else if (index == R.styleable.Ui_ViewPager_uiCurrentPage) {
                this.mCurrentPage = typedArray.getInteger(index, 0);
            } else if (index == R.styleable.Ui_ViewPager_uiOffScreenPageLimit) {
                setOffscreenPageLimit(typedArray.getInt(index, getOffscreenPageLimit()));
            } else if (index == R.styleable.Ui_ViewPager_uiPageScrollDuration) {
                this.mPageScrollDuration = typedArray.getInteger(index, mPageScrollDuration);
            } else if (index == R.styleable.Ui_ViewPager_uiPageScrollRelativeDurationEnabled) {
                mDecorator.updatePrivateFlags(PFLAG_PAGE_SCROLL_RELATIVE_DURATION_ENABLED,
                        typedArray.getBoolean(index, false));
            } else if (index == R.styleable.Ui_ViewPager_uiPullEnabled) {
                setPullEnabled(typedArray.getBoolean(index, false));
            }
        }
        typedArray.recycle();
    }
    // Override default scroller so we can use custom durations for scroll if requested.
    setScroller(new WidgetScroller(context, SCROLLER_INTERPOLATOR));
}

From source file:com.dm.material.dashboard.candybar.fragments.ApplyFragment.java

private void getLaunchers() {
    mGetLaunchers = new AsyncTask<Void, Void, Boolean>() {

        List<Icon> launchers;

        @Override//from   w  w w.  ja v a2s .  c o  m
        protected void onPreExecute() {
            super.onPreExecute();
            launchers = new ArrayList<>();
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            while (!isCancelled()) {
                try {
                    Thread.sleep(1);
                    String[] launcherNames = getActivity().getResources()
                            .getStringArray(R.array.launcher_names);
                    TypedArray launcherIcons = getActivity().getResources()
                            .obtainTypedArray(R.array.launcher_icons);
                    String[] launcherPackages1 = getActivity().getResources()
                            .getStringArray(R.array.launcher_packages_1);
                    String[] launcherPackages2 = getActivity().getResources()
                            .getStringArray(R.array.launcher_packages_2);
                    String[] launcherPackages3 = getActivity().getResources()
                            .getStringArray(R.array.launcher_packages_3);

                    List<Icon> installed = new ArrayList<>();
                    List<Icon> supported = new ArrayList<>();

                    for (int i = 0; i < launcherNames.length; i++) {
                        boolean isInstalled = isLauncherInstalled(launcherPackages1[i], launcherPackages2[i],
                                launcherPackages3[i]);

                        int icon = R.drawable.ic_app_default;
                        if (i < launcherIcons.length())
                            icon = launcherIcons.getResourceId(i, icon);

                        String launcherPackage = launcherPackages1[i];
                        if (launcherPackages1[i].equals("com.lge.launcher2")) {
                            boolean lghome3 = isPackageInstalled(launcherPackages2[i]);
                            if (lghome3)
                                launcherPackage = launcherPackages2[i];
                        }

                        Icon launcher = new Icon(launcherNames[i], icon, launcherPackage);
                        if (isInstalled)
                            installed.add(launcher);
                        else
                            supported.add(launcher);
                    }

                    try {
                        Collections.sort(installed, new AlphanumComparator() {
                            @Override
                            public int compare(Object o1, Object o2) {
                                String s1 = ((Icon) o1).getTitle();
                                String s2 = ((Icon) o2).getTitle();
                                return super.compare(s1, s2);
                            }
                        });
                    } catch (Exception ignored) {
                    }

                    try {
                        Collections.sort(supported, new AlphanumComparator() {
                            @Override
                            public int compare(Object o1, Object o2) {
                                String s1 = ((Icon) o1).getTitle();
                                String s2 = ((Icon) o2).getTitle();
                                return super.compare(s1, s2);
                            }
                        });
                    } catch (Exception ignored) {
                    }

                    if (installed.size() > 0) {
                        launchers.add(new Icon(getActivity().getResources().getString(R.string.apply_installed),
                                -1, null));
                    }

                    launchers.addAll(installed);
                    launchers.add(new Icon(getActivity().getResources().getString(R.string.apply_supported), -2,
                            null));
                    launchers.addAll(supported);

                    launcherIcons.recycle();
                    return true;
                } catch (Exception e) {
                    LogUtil.e(Log.getStackTraceString(e));
                    return false;
                }
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (aBoolean) {
                mRecyclerView.setAdapter(new LauncherAdapter(getActivity(), launchers));
                resetSpanSizeLookUp();
            }
            mGetLaunchers = null;
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:com.github.shareme.gwsmaterialuikit.library.material.widget.TabIndicatorView.java

protected void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabPageIndicator, defStyleAttr,
            defStyleRes);/*  ww w .ja va  2  s .  co m*/

    int tabPadding = -1;
    int textAppearance = 0;
    int mode = -1;
    int rippleStyle = 0;
    boolean tabSingleLine = false;
    boolean singleLineDefined = false;

    for (int i = 0, count = a.getIndexCount(); i < count; i++) {
        int attr = a.getIndex(i);
        if (attr == R.styleable.TabPageIndicator_tpi_tabPadding)
            tabPadding = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.TabPageIndicator_tpi_tabRipple)
            rippleStyle = a.getResourceId(attr, 0);
        else if (attr == R.styleable.TabPageIndicator_tpi_indicatorColor)
            mPaint.setColor(a.getColor(attr, 0));
        else if (attr == R.styleable.TabPageIndicator_tpi_indicatorHeight)
            mIndicatorHeight = a.getDimensionPixelSize(attr, 0);
        else if (attr == R.styleable.TabPageIndicator_tpi_indicatorAtTop)
            mIndicatorAtTop = a.getBoolean(attr, true);
        else if (attr == R.styleable.TabPageIndicator_tpi_tabSingleLine) {
            tabSingleLine = a.getBoolean(attr, true);
            singleLineDefined = true;
        } else if (attr == R.styleable.TabPageIndicator_tpi_centerCurrentTab)
            mCenterCurrentTab = a.getBoolean(attr, true);
        else if (attr == R.styleable.TabPageIndicator_android_textAppearance)
            textAppearance = a.getResourceId(attr, 0);
        else if (attr == R.styleable.TabPageIndicator_tpi_mode)
            mode = a.getInteger(attr, 0);
    }

    a.recycle();

    if (mIndicatorHeight < 0)
        mIndicatorHeight = ThemeUtil.dpToPx(context, 2);

    boolean shouldNotify = false;

    if (tabPadding >= 0 && mTabPadding != tabPadding) {
        mTabPadding = tabPadding;
        shouldNotify = true;
    }

    if (singleLineDefined && mTabSingleLine != tabSingleLine) {
        mTabSingleLine = tabSingleLine;
        shouldNotify = true;
    }

    if (mode >= 0 && mMode != mode) {
        mMode = mode;
        mAdapter.setFixedWidth(0, 0);
        shouldNotify = true;
    }

    if (textAppearance != 0 && mTextAppearance != textAppearance) {
        mTextAppearance = textAppearance;
        shouldNotify = true;
    }

    if (rippleStyle != 0 && rippleStyle != mTabRippleStyle) {
        mTabRippleStyle = rippleStyle;
        shouldNotify = true;
    }

    if (shouldNotify)
        mAdapter.notifyItemRangeChanged(0, mAdapter.getItemCount());

    invalidate();
}

From source file:com.daiv.android.twitter.ui.compose.Compose.java

@Override
public void onBackPressed() {
    if (emojiKeyboard.isShowing()) {
        emojiKeyboard.setVisibility(false);

        TypedArray a = getTheme().obtainStyledAttributes(new int[] { R.attr.emoji_button_changing });
        int resource = a.getResourceId(0, 0);
        a.recycle();/*from ww w.j  av a2s . c om*/
        emojiButton.setImageResource(resource);
        return;
    }

    super.onBackPressed();
}

From source file:co.vn.e_alarm.customwiget.SlidingLayer.java

public SlidingLayer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Style/*from w w  w. j  ava  2s  .  c  o  m*/
    final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingLayer);

    // Set the side of the screen
    setStickTo(ta.getInt(R.styleable.SlidingLayer_stickTo, STICK_TO_AUTO));

    // Sets the shadow drawable
    int shadowRes = ta.getResourceId(R.styleable.SlidingLayer_shadowDrawable, -1);
    if (shadowRes != -1) {
        setShadowDrawable(shadowRes);
    }

    // Sets the shadow width
    setShadowWidth((int) ta.getDimension(R.styleable.SlidingLayer_shadowWidth, 0));

    // Sets the ability to close the layer by tapping in any empty space
    closeOnTapEnabled = ta.getBoolean(R.styleable.SlidingLayer_closeOnTapEnabled, true);
    // Sets the ability to open the layout by tapping on any of the exposed closed layer
    openOnTapEnabled = ta.getBoolean(R.styleable.SlidingLayer_openOnTapEnabled, true);

    // How much of the view sticks out when closed
    setOffsetWidth(ta.getDimensionPixelOffset(R.styleable.SlidingLayer_offsetWidth, 0));

    ta.recycle();

    init();
}

From source file:android.app.FragmentState.java

private static Transition loadTransition(Context context, TypedArray typedArray, Transition currentValue,
        Transition defaultValue, int id) {
    if (currentValue != defaultValue) {
        return currentValue;
    }//www  .ja  v  a 2s .  c  o m
    int transitionId = typedArray.getResourceId(id, 0);
    Transition transition = defaultValue;
    if (transitionId != 0 && transitionId != com.android.internal.R.transition.no_transition) {
        TransitionInflater inflater = TransitionInflater.from(context);
        transition = inflater.inflateTransition(transitionId);
        if (transition instanceof TransitionSet && ((TransitionSet) transition).getTransitionCount() == 0) {
            transition = null;
        }
    }
    return transition;
}

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

/**
 * Init ListView/*from   w w w.j  a  va2 s.  c o  m*/
 *
 * @param attrs AttributeSet
 */
private 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);
        swipeFrontView = styled.getResourceId(R.styleable.SwipeListView_swipeFrontView, 0);
        swipeBackView = styled.getResourceId(R.styleable.SwipeListView_swipeBackView, 0);
    }

    if (swipeFrontView == 0 || swipeBackView == 0) {
        throw new RuntimeException("Missed attribute swipeFrontView or swipeBackView");
    }

    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    touchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    touchListener = new SwipeListViewTouchListener(this, swipeFrontView, swipeBackView);
    if (swipeAnimationTime > 0) {
        touchListener.setAnimationTime(swipeAnimationTime);
    }
    touchListener.setRightOffset(swipeOffsetRight);
    touchListener.setLeftOffset(swipeOffsetLeft);
    touchListener.setSwipeActionLeft(swipeActionLeft);
    touchListener.setSwipeActionRight(swipeActionRight);
    touchListener.setSwipeMode(swipeMode);
    touchListener.setSwipeClosesAllItemsWhenListMoves(swipeCloseAllItemsWhenMoveList);
    touchListener.setSwipeOpenOnLongPress(swipeOpenOnLongPress);
    setOnTouchListener(touchListener);
    setOnScrollListener(touchListener.makeScrollListener());
}

From source file:com.github.rubensousa.floatingtoolbar.FloatingToolbar.java

public FloatingToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatingToolbar, 0, 0);

    TypedValue outValue = new TypedValue();

    // Set colorAccent as default color
    if (getBackground() == null) {
        getContext().getTheme().resolveAttribute(R.attr.colorAccent, outValue, true);
        setBackgroundResource(outValue.resourceId);
    }//from   w w w  . j  a  va2 s .c  o  m

    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);

    mMorphListeners = new ArrayList<>();
    mScrollListener = new FloatingScrollListener(this);
    mShowToast = a.getBoolean(R.styleable.FloatingToolbar_floatingToastOnLongClick, true);
    mHandleFabClick = a.getBoolean(R.styleable.FloatingToolbar_floatingHandleFabClick, true);
    mItemBackground = a.getResourceId(R.styleable.FloatingToolbar_floatingItemBackground, outValue.resourceId);
    mMenuRes = a.getResourceId(R.styleable.FloatingToolbar_floatingMenu, 0);

    int customView = a.getResourceId(R.styleable.FloatingToolbar_floatingCustomView, 0);

    if (customView != 0) {
        mCustomView = LayoutInflater.from(context).inflate(customView, this, true);
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        mAnimator = new FloatingAnimatorImpl(this);
    } else {
        mAnimator = new FloatingAnimatorLollipopImpl(this);
    }

    if (mCustomView != null) {
        mAnimator.setContentView(mCustomView);
    }

    // Set elevation to 6dp
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setElevation(dpToPixels(context, 6));
    }

    if (mMenuRes != 0 && customView == 0) {
        mMenuLayout = new LinearLayoutCompat(context, attrs, defStyleAttr);

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        mMenuLayout.setId(genViewId());
        addView(mMenuLayout, layoutParams);
        addMenuItems();
        mAnimator.setContentView(mMenuLayout);
    }

    if (!isInEditMode()) {
        setVisibility(View.INVISIBLE);
    }

    a.recycle();

    setOrientation(HORIZONTAL);
}

From source file:com.github.antoniodisanto92.swipeselector.SwipeSelector.java

private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SwipeSelector, defStyleAttr,
            defStyleRes);/*from  w w  w  .  j a v a2  s  .  c  o m*/

    int indicatorSize;
    int indicatorMargin;
    int indicatorInActiveColor;
    int indicatorActiveColor;
    int leftButtonResource;
    int rightButtonResource;

    String customFontPath;
    int titleTextAppearance;
    int descriptionTextAppearance;
    int descriptionGravity;

    try {
        indicatorSize = (int) ta.getDimension(R.styleable.SwipeSelector_swipe_indicatorSize,
                PixelUtils.dpToPixel(context, DEFAULT_INDICATOR_SIZE));
        indicatorMargin = (int) ta.getDimension(R.styleable.SwipeSelector_swipe_indicatorMargin,
                PixelUtils.dpToPixel(context, DEFAULT_INDICATOR_MARGIN));
        indicatorInActiveColor = ta.getColor(R.styleable.SwipeSelector_swipe_indicatorInActiveColor,
                ContextCompat.getColor(context, R.color.swipeselector_color_indicator_inactive));
        indicatorActiveColor = ta.getColor(R.styleable.SwipeSelector_swipe_indicatorActiveColor,
                ContextCompat.getColor(context, R.color.swipeselector_color_indicator_active));

        leftButtonResource = ta.getResourceId(R.styleable.SwipeSelector_swipe_leftButtonResource,
                R.drawable.ic_action_navigation_chevron_left);
        rightButtonResource = ta.getResourceId(R.styleable.SwipeSelector_swipe_rightButtonResource,
                R.drawable.ic_action_navigation_chevron_right);

        customFontPath = ta.getString(R.styleable.SwipeSelector_swipe_customFontPath);
        titleTextAppearance = ta.getResourceId(R.styleable.SwipeSelector_swipe_titleTextAppearance, -1);
        descriptionTextAppearance = ta.getResourceId(R.styleable.SwipeSelector_swipe_descriptionTextAppearance,
                -1);
        descriptionGravity = ta.getInteger(R.styleable.SwipeSelector_swipe_descriptionGravity, -1);
    } finally {
        ta.recycle();
    }

    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.swipeselector_layout, this);

    ViewPager pager = (ViewPager) findViewById(R.id.swipeselector_layout_swipePager);
    ViewGroup indicatorContainer = (ViewGroup) findViewById(R.id.swipeselector_layout_circleContainer);
    ImageView leftButton = (ImageView) findViewById(R.id.swipeselector_layout_leftButton);
    ImageView rightButton = (ImageView) findViewById(R.id.swipeselector_layout_rightButton);

    mAdapter = new SwipeAdapter.Builder().viewPager(pager).indicatorContainer(indicatorContainer)
            .indicatorSize(indicatorSize).indicatorMargin(indicatorMargin)
            .inActiveIndicatorColor(indicatorInActiveColor).activeIndicatorColor(indicatorActiveColor)
            .leftButtonResource(leftButtonResource).rightButtonResource(rightButtonResource)
            .leftButton(leftButton).rightButton(rightButton).customFontPath(customFontPath)
            .titleTextAppearance(titleTextAppearance).descriptionTextAppearance(descriptionTextAppearance)
            .descriptionGravity(descriptionGravity).build();
    pager.setAdapter(mAdapter);
}