Example usage for android.content Context obtainStyledAttributes

List of usage examples for android.content Context obtainStyledAttributes

Introduction

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

Prototype

public final TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs,
        @AttrRes int defStyleAttr, @StyleRes int defStyleRes) 

Source Link

Document

Retrieve styled attribute information in this Context's theme.

Usage

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

public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(MediaRouterThemeHelper.createThemedContext(context, defStyleAttr), attrs, defStyleAttr);
    context = getContext();//from  w  ww  .  j  a  v a 2s  . co  m

    mRouter = MediaRouter.getInstance(context);
    mCallback = new MediaRouterCallback();

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MediaRouteButton, defStyleAttr, 0);
    setRemoteIndicatorDrawable(a.getDrawable(R.styleable.MediaRouteButton_externalRouteEnabledDrawable));
    mMinWidth = a.getDimensionPixelSize(R.styleable.MediaRouteButton_android_minWidth, 0);
    mMinHeight = a.getDimensionPixelSize(R.styleable.MediaRouteButton_android_minHeight, 0);
    a.recycle();

    setClickable(true);
    setLongClickable(true);
}

From source file:android.support.designox.widget.FloatingActionButton.java

public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    ThemeUtils.checkAppCompatTheme(context);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatingActionButton, defStyleAttr,
            R.style.Widget_Design_FloatingActionButton);
    mBackgroundTint = a.getColorStateList(R.styleable.FloatingActionButton_backgroundTint_ox);
    mBackgroundTintMode = parseTintMode(a.getInt(R.styleable.FloatingActionButton_backgroundTintMode_ox, -1),
            null);/* www  .  j  a  va2  s.  c o m*/
    mRippleColor = a.getColor(R.styleable.FloatingActionButton_rippleColor, 0);
    mSize = a.getInt(R.styleable.FloatingActionButton_fabSize_ox, SIZE_NORMAL);
    mBorderWidth = a.getDimensionPixelSize(R.styleable.FloatingActionButton_borderWidth, 0);
    final float elevation = a.getDimension(R.styleable.FloatingActionButton_elevation, 0f);
    final float pressedTranslationZ = a.getDimension(R.styleable.FloatingActionButton_pressedTranslationZ, 0f);
    mCompatPadding = a.getBoolean(R.styleable.FloatingActionButton_useCompatPadding, false);
    a.recycle();

    mImageHelper = new AppCompatImageHelper(this, AppCompatDrawableManager.get());
    mImageHelper.loadFromAttributes(attrs, defStyleAttr);

    final int maxImageSize = (int) getResources().getDimension(R.dimen.design_fab_image_size);
    mImagePadding = (getSizeDimension() - maxImageSize) / 2;

    getImpl().setBackgroundDrawable(mBackgroundTint, mBackgroundTintMode, mRippleColor, mBorderWidth);
    getImpl().setElevation(elevation);
    getImpl().setPressedTranslationZ(pressedTranslationZ);
    getImpl().updatePadding();
}

From source file:com.albedinsky.android.setting.SettingSelectionDialogPreference.java

/**
 *///from   w w w . j  a  v  a 2 s.co  m
@Override
@SuppressWarnings("ResourceType")
protected void onConfigureDialogOptions(@NonNull SelectionDialog.SelectionOptions options,
        @NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr,
        @StyleRes int defStyleRes) {
    super.onConfigureDialogOptions(options, context, attrs, defStyleAttr, defStyleRes);
    final TypedArray typedArray = context.obtainStyledAttributes(attrs,
            R.styleable.Ui_Settings_SelectionDialogPreference, defStyleAttr, defStyleRes);
    for (int i = 0; i < typedArray.getIndexCount(); i++) {
        final int index = typedArray.getIndex(i);
        if (index == R.styleable.Ui_Settings_SelectionDialogPreference_dialogSelectionMode) {
            options.selectionMode(typedArray.getInt(index, options.selectionMode()));
        } else if (index == R.styleable.Ui_Settings_SelectionDialogPreference_dialogEmptySelectionAllowed) {
            options.emptySelectionAllowed(typedArray.getBoolean(index, options.shouldAllowEmptySelection()));
        }
    }
    typedArray.recycle();
}

From source file:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java

public ResolverDrawerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ResolverDrawerLayout, defStyleAttr,
            0);/* ww w . j  av  a2s . c om*/
    mMaxWidth = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_android_maxWidth, -1);
    mMaxCollapsedHeight = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_maxCollapsedHeight, 0);
    mMaxCollapsedHeightSmall = a.getDimensionPixelSize(R.styleable.ResolverDrawerLayout_maxCollapsedHeightSmall,
            mMaxCollapsedHeight);
    a.recycle();

    mScroller = new OverScroller(context,
            AnimationUtils.loadInterpolator(context, android.R.interpolator.decelerate_quint));
    mVelocityTracker = VelocityTracker.obtain();

    final ViewConfiguration vc = ViewConfiguration.get(context);
    mTouchSlop = vc.getScaledTouchSlop();
    mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
}

From source file:androidx.mediarouter.app.MediaRouteButton.java

public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(MediaRouterThemeHelper.createThemedButtonContext(context), attrs, defStyleAttr);
    context = getContext();/*from ww w .  j a va  2  s  .  c om*/

    mRouter = MediaRouter.getInstance(context);
    mCallback = new MediaRouterCallback();

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MediaRouteButton, defStyleAttr, 0);
    mButtonTint = a.getColorStateList(R.styleable.MediaRouteButton_mediaRouteButtonTint);
    mMinWidth = a.getDimensionPixelSize(R.styleable.MediaRouteButton_android_minWidth, 0);
    mMinHeight = a.getDimensionPixelSize(R.styleable.MediaRouteButton_android_minHeight, 0);
    int remoteIndicatorResId = a.getResourceId(R.styleable.MediaRouteButton_externalRouteEnabledDrawable, 0);
    a.recycle();

    if (remoteIndicatorResId != 0) {
        Drawable.ConstantState remoteIndicatorState = sRemoteIndicatorCache.get(remoteIndicatorResId);
        if (remoteIndicatorState != null) {
            setRemoteIndicatorDrawable(remoteIndicatorState.newDrawable());
        } else {
            mRemoteIndicatorLoader = new RemoteIndicatorLoader(remoteIndicatorResId);
            mRemoteIndicatorLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    }

    updateContentDescription();
    setClickable(true);
}

From source file:com.android.inputmethod.keyboard.EmojiPalettesView.java

public EmojiPalettesView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
    final TypedArray keyboardViewAttr = context.obtainStyledAttributes(attrs, R.styleable.KeyboardView,
            defStyle, R.style.KeyboardView);
    mKeyBackgroundId = keyboardViewAttr.getResourceId(R.styleable.KeyboardView_keyBackground, 0);
    mEmojiFunctionalKeyBackgroundId = keyboardViewAttr
            .getResourceId(R.styleable.KeyboardView_keyBackgroundEmojiFunctional, 0);
    keyboardViewAttr.recycle();/* w w w.  j  av  a2s  .c  om*/
    final TypedArray emojiPalettesViewAttr = context.obtainStyledAttributes(attrs,
            R.styleable.EmojiPalettesView, defStyle, R.style.EmojiPalettesView);
    mTabLabelColor = emojiPalettesViewAttr.getColorStateList(R.styleable.EmojiPalettesView_emojiTabLabelColor);
    emojiPalettesViewAttr.recycle();
    final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(context, null /* editorInfo */);
    final Resources res = context.getResources();
    final EmojiLayoutParams emojiLp = new EmojiLayoutParams(res);
    builder.setSubtype(SubtypeSwitcher.getInstance().getEmojiSubtype());
    builder.setKeyboardGeometry(ResourceUtils.getDefaultKeyboardWidth(res), emojiLp.mEmojiKeyboardHeight);
    builder.setOptions(false, false, false /* lanuageSwitchKeyEnabled */);
    mLayoutSet = builder.build();
    mEmojiCategory = new EmojiCategory(PreferenceManager.getDefaultSharedPreferences(context),
            context.getResources(), builder.build());
    mDeleteKeyOnTouchListener = new DeleteKeyOnTouchListener(context);
}

From source file:co.lujun.popmenulayout.PopMenuLayout.java

private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
    TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.PopMenuLayout, defStyleAttr, 0);
    mConfigJson = attributes.getString(R.styleable.PopMenuLayout_config_json);
    mLevel2MenuAnimStyle = attributes.getResourceId(R.styleable.PopMenuLayout_level2_menu_anim_style,
            mLevel2MenuAnimStyle);/*from  w w w .j a  v  a2  s . c o  m*/
    mLevel1MenuItemHeight = attributes.getDimension(R.styleable.PopMenuLayout_level1_menu_item_height,
            Util.dp2px(context, mLevel1MenuItemHeight));
    mChildMenuItemHeight = attributes.getDimension(R.styleable.PopMenuLayout_child_menu_item_height,
            Util.dp2px(context, mChildMenuItemHeight));
    isWithLevel1MenuWidth = attributes.getBoolean(R.styleable.PopMenuLayout_cmenu_w_follow_level1_menu,
            isWithLevel1MenuWidth);
    mMenuDividerDp = attributes.getDimension(R.styleable.PopMenuLayout_menu_divider_width,
            Util.dp2px(context, mMenuDividerDp));
    mMenuTextPaddingLeft = attributes.getDimension(R.styleable.PopMenuLayout_menu_left_padding,
            Util.dp2px(context, mMenuTextPaddingLeft));
    mMenuTextPaddingRight = attributes.getDimension(R.styleable.PopMenuLayout_menu_right_padding,
            Util.dp2px(context, mMenuTextPaddingRight));
    mMenuTextPaddingTop = attributes.getDimension(R.styleable.PopMenuLayout_menu_top_padding,
            Util.dp2px(context, mMenuTextPaddingTop));
    mMenuTextPaddingBottom = attributes.getDimension(R.styleable.PopMenuLayout_menu_bottom_padding,
            Util.dp2px(context, mMenuTextPaddingBottom));
    mDividerColor = attributes.getColor(R.styleable.PopMenuLayout_menu_divider_color, mDividerColor);
    mExpandableIcon = attributes.getResourceId(R.styleable.PopMenuLayout_menu_expandable_icon, mExpandableIcon);
    mMenuTextColor = attributes.getColor(R.styleable.PopMenuLayout_menu_text_color, mMenuTextColor);
    mHorizontalMenuBackgroundRes = attributes.getResourceId(R.styleable.PopMenuLayout_horizontal_menu_bg,
            mHorizontalMenuBackgroundRes);
    mVerticalMenuBackgroundRes = attributes.getResourceId(R.styleable.PopMenuLayout_vertical_menu_bg,
            mVerticalMenuBackgroundRes);
    mMenuTextSize = attributes.getDimension(R.styleable.PopMenuLayout_menu_text_size,
            Util.sp2px(context, mMenuTextSize));
    mMaxMenuItemCount = attributes.getInteger(R.styleable.PopMenuLayout_child_menu_max_count,
            mMaxMenuItemCount);
    mLevel1MenuLayoutBgColor = attributes.getColor(R.styleable.PopMenuLayout_level1_menu_layout_bg_color,
            mLevel1MenuLayoutBgColor);
    mChildMenuLayoutBgColor = attributes.getColor(R.styleable.PopMenuLayout_cmenu_layout_bg_color,
            mChildMenuLayoutBgColor);
    mDividerMarginLeft = attributes.getDimension(R.styleable.PopMenuLayout_divider_margin_left,
            Util.dp2px(context, mDividerMarginLeft));
    mDividerMarginRight = attributes.getDimension(R.styleable.PopMenuLayout_divider_margin_right,
            Util.dp2px(context, mDividerMarginRight));
    mDividerMarginTop = attributes.getDimension(R.styleable.PopMenuLayout_divider_margin_top,
            Util.dp2px(context, mDividerMarginTop));
    mDividerMarginBottom = attributes.getDimension(R.styleable.PopMenuLayout_divider_margin_bottom,
            Util.dp2px(context, mDividerMarginBottom));
    attributes.recycle();
}

From source file:com.alimuzaffar.lib.widgets.PinEntryEditText.java

private void init(Context context, AttributeSet attrs) {
    float multi = context.getResources().getDisplayMetrics().density;
    mLineStroke = multi * mLineStroke;/*from w  ww  .j a v a  2s. c  om*/
    mLineStrokeSelected = multi * mLineStrokeSelected;
    mSpace = multi * mSpace; //convert to pixels for our density
    mTextBottomPadding = multi * mTextBottomPadding; //convert to pixels for our density

    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PinEntryEditText, 0, 0);
    try {
        TypedValue outValue = new TypedValue();
        ta.getValue(R.styleable.PinEntryEditText_pinAnimationType, outValue);
        mAnimatedType = outValue.data;
        mMask = ta.getString(R.styleable.PinEntryEditText_pinCharacterMask);
        mLineStroke = ta.getDimension(R.styleable.PinEntryEditText_pinLineStroke, mLineStroke);
        mLineStrokeSelected = ta.getDimension(R.styleable.PinEntryEditText_pinLineStrokeSelected,
                mLineStrokeSelected);
        mSpace = ta.getDimension(R.styleable.PinEntryEditText_pinCharacterSpacing, mSpace);
        mTextBottomPadding = ta.getDimension(R.styleable.PinEntryEditText_pinTextBottomPadding,
                mTextBottomPadding);
        mIsDigitSquare = ta.getBoolean(R.styleable.PinEntryEditText_pinBackgroundIsSquare, mIsDigitSquare);
        mPinBackground = ta.getDrawable(R.styleable.PinEntryEditText_pinBackgroundDrawable);
        ColorStateList colors = ta.getColorStateList(R.styleable.PinEntryEditText_pinLineColors);
        if (colors != null) {
            mColorStates = colors;
        }
    } finally {
        ta.recycle();
    }

    mCharPaint = new Paint(getPaint());
    mLastCharPaint = new Paint(getPaint());
    mLinesPaint = new Paint(getPaint());
    mLinesPaint.setStrokeWidth(mLineStroke);

    TypedValue outValue = new TypedValue();
    context.getTheme().resolveAttribute(R.attr.colorControlActivated, outValue, true);
    int colorSelected = outValue.data;
    mColors[0] = colorSelected;

    int colorFocused = isInEditMode() ? Color.GRAY : ContextCompat.getColor(context, R.color.pin_normal);
    mColors[1] = colorFocused;

    int colorUnfocused = isInEditMode() ? Color.GRAY : ContextCompat.getColor(context, R.color.pin_normal);
    mColors[2] = colorUnfocused;

    setBackgroundResource(0);

    mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", 4);
    mNumChars = mMaxLength;

    //Disable copy paste
    super.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {
        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }
    });
    // When tapped, move cursor to end of text.
    super.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            setSelection(getText().length());
            if (mClickListener != null) {
                mClickListener.onClick(v);
            }
        }
    });

    super.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            setSelection(getText().length());
            return true;
        }
    });

    //If input type is password and no mask is set, use a default mask
    if ((getInputType() & InputType.TYPE_TEXT_VARIATION_PASSWORD) == InputType.TYPE_TEXT_VARIATION_PASSWORD
            && TextUtils.isEmpty(mMask)) {
        mMask = "\u25CF";
    } else if ((getInputType()
            & InputType.TYPE_NUMBER_VARIATION_PASSWORD) == InputType.TYPE_NUMBER_VARIATION_PASSWORD
            && TextUtils.isEmpty(mMask)) {
        mMask = "\u25CF";
    }

    if (!TextUtils.isEmpty(mMask)) {
        mMaskChars = getMaskChars();
    }

    //Height of the characters, used if there is a background drawable
    getPaint().getTextBounds("|", 0, 1, mTextHeight);
}

From source file:br.liveo.searchliveo.SearchLiveo.java

private void initAttribute(Context context, AttributeSet attributeSet, int defStyleAttr) {
    TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.search_liveo, defStyleAttr, 0);
    if (attr != null) {
        try {//from w  ww.j a v  a  2 s  .  c  o  m

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_hint)) {
                hint(attr.getString(R.styleable.search_liveo_search_liveo_hint));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_text_color)) {
                mEdtSearch.setTextColor(attr.getColor(R.styleable.search_liveo_search_liveo_text_color, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_hint_color)) {
                mEdtSearch
                        .setHintTextColor(attr.getColor(R.styleable.search_liveo_search_liveo_hint_color, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_color_icon)) {
                setColorIcon(attr.getColor(R.styleable.search_liveo_search_liveo_color_icon, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_color_arrow)) {
                setColorIconArrow(attr.getColor(R.styleable.search_liveo_search_liveo_color_arrow, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_color_voice)) {
                setColorIconVoice(attr.getColor(R.styleable.search_liveo_search_liveo_color_voice, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_color_close)) {
                setColorIconClose(attr.getColor(R.styleable.search_liveo_search_liveo_color_close, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_background)) {
                mViewSearch.setBackgroundColor(
                        attr.getColor(R.styleable.search_liveo_search_liveo_background, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_status_bar_show_color)) {
                setStatusBarShowColor(
                        attr.getColor(R.styleable.search_liveo_search_liveo_status_bar_show_color, -1));
            }

            if (attr.hasValue(R.styleable.search_liveo_search_liveo_status_bar_hide_color)) {
                setStatusBarHideColor(
                        attr.getColor(R.styleable.search_liveo_search_liveo_status_bar_hide_color, -1));
            }
        } finally {
            attr.recycle();
        }
    }
}

From source file:br.liveo.searchliveo.SearchCardLiveo.java

private void initAttribute(Context context, AttributeSet attributeSet, int defStyleAttr) {
    TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.search_card_liveo, defStyleAttr,
            0);/*from  www.  ja va  2s  .c  o m*/
    if (attr != null) {
        try {

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_hint)) {
                hint(attr.getString(R.styleable.search_card_liveo_search_card_liveo_hint));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_text_color)) {
                mEdtSearch.setTextColor(
                        attr.getColor(R.styleable.search_card_liveo_search_card_liveo_text_color, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_hint_color)) {
                mEdtSearch.setHintTextColor(
                        attr.getColor(R.styleable.search_card_liveo_search_card_liveo_hint_color, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_color_icon)) {
                setColorIcon(attr.getColor(R.styleable.search_card_liveo_search_card_liveo_color_icon, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_color_arrow)) {
                setColorIconArrow(
                        attr.getColor(R.styleable.search_card_liveo_search_card_liveo_color_arrow, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_color_voice)) {
                setColorIconVoice(
                        attr.getColor(R.styleable.search_card_liveo_search_card_liveo_color_voice, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_color_close)) {
                setColorIconClose(
                        attr.getColor(R.styleable.search_card_liveo_search_card_liveo_color_close, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_background)) {
                mCardSearch.setBackgroundColor(
                        attr.getColor(R.styleable.search_card_liveo_search_card_liveo_background, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_status_bar_show_color)) {
                setStatusBarShowColor(attr
                        .getColor(R.styleable.search_card_liveo_search_card_liveo_status_bar_show_color, -1));
            }

            if (attr.hasValue(R.styleable.search_card_liveo_search_card_liveo_status_bar_hide_color)) {
                setStatusBarHideColor(attr
                        .getColor(R.styleable.search_card_liveo_search_card_liveo_status_bar_hide_color, -1));
            }
        } finally {
            attr.recycle();
        }
    }
}