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) 

Source Link

Document

Retrieve styled attribute information in this Context's theme.

Usage

From source file:com.android.settingslib.RestrictedSwitchPreference.java

public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    setWidgetLayoutResource(R.layout.restricted_switch_widget);
    mHelper = new RestrictedPreferenceHelper(context, this, attrs);
    if (attrs != null) {
        final TypedArray attributes = context.obtainStyledAttributes(attrs,
                R.styleable.RestrictedSwitchPreference);
        final TypedValue useAdditionalSummary = attributes
                .peekValue(R.styleable.RestrictedSwitchPreference_useAdditionalSummary);
        if (useAdditionalSummary != null) {
            mUseAdditionalSummary = (useAdditionalSummary.type == TypedValue.TYPE_INT_BOOLEAN
                    && useAdditionalSummary.data != 0);
        }//  w  w  w  . java2 s. com

        final TypedValue restrictedSwitchSummary = attributes
                .peekValue(R.styleable.RestrictedSwitchPreference_restrictedSwitchSummary);
        CharSequence data = null;
        if (restrictedSwitchSummary != null && restrictedSwitchSummary.type == TypedValue.TYPE_STRING) {
            if (restrictedSwitchSummary.resourceId != 0) {
                data = context.getString(restrictedSwitchSummary.resourceId);
            } else {
                data = restrictedSwitchSummary.string;
            }
        }
        mRestrictedSwitchSummary = data == null ? null : data.toString();
    }
    if (mRestrictedSwitchSummary == null) {
        mRestrictedSwitchSummary = context.getString(R.string.disabled_by_admin);
    }
    if (mUseAdditionalSummary) {
        setLayoutResource(R.layout.restricted_switch_preference);
        useAdminDisabledSummary(false);
    }
}

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)));
        }/*ww  w.  j  ava 2 s  .  co  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:android.support.transition.ChangeBounds.java

public ChangeBounds(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, Styleable.CHANGE_BOUNDS);
    boolean resizeClip = TypedArrayUtils.getNamedBoolean(a, (XmlResourceParser) attrs, "resizeClip",
            Styleable.ChangeBounds.RESIZE_CLIP, false);
    a.recycle();/*from   ww  w  .j  av a  2  s. c  o  m*/
    setResizeClip(resizeClip);
}

From source file:com.android.contacts.common.list.ViewPagerTabs.java

public ViewPagerTabs(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setFillViewport(true);/*  w  ww . jav a2 s . c  om*/

    mSidePadding = (int) (getResources().getDisplayMetrics().density * TAB_SIDE_PADDING_IN_DPS);

    final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
    mTextSize = a.getDimensionPixelSize(0, 0);
    mTextStyle = a.getInt(1, 0);
    mTextColor = a.getColorStateList(2);
    mTextAllCaps = a.getBoolean(3, false);

    mTabStrip = new ViewPagerTabStrip(context);
    addView(mTabStrip, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
    a.recycle();

    if (CompatUtils.isLollipopCompatible()) {
        // enable shadow casting from view bounds
        setOutlineProvider(VIEW_BOUNDS_OUTLINE_PROVIDER);
    }
}

From source file:jp.co.ipublishing.esnavi.views.MarqueeView.java

/**
 * (XML??)/*www.java 2 s. c o m*/
 *
 * @param context 
 * @param attrs   XML???
 */
public MarqueeView(Context context, AttributeSet attrs) {
    super(context, attrs);

    initMarqueeView();

    // XML??
    final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MarqueeView);

    final String marqueeText = attributes.getString(R.styleable.MarqueeView_marquee_text);
    if (marqueeText != null) {
        setText(marqueeText);
    }

    final int textSize = attributes.getDimensionPixelOffset(R.styleable.MarqueeView_marquee_textSize, 0);
    if (textSize > 0) {
        setTextSize(textSize);
    }

    final int padding = attributes.getDimensionPixelOffset(R.styleable.MarqueeView_marquee_padding, 0);
    if (padding > 0) {
        setPadding(padding, padding, padding, padding);
    }

    setTextColor(attributes.getColor(R.styleable.MarqueeView_marquee_textColor, 0xFFFFFFFF));
    setBackgroundColor(attributes.getColor(R.styleable.MarqueeView_marquee_background, 0xFF000000));
    setRepeatLimit(attributes.getInteger(R.styleable.MarqueeView_marquee_repeatLimit, 1));
    setTextMoveSpeed(attributes.getInteger(R.styleable.MarqueeView_marquee_textMoveSpeed, 5));

    attributes.recycle();
}

From source file:com.amazonaws.mobile.auth.userpools.SignUpView.java

/**
 * Constructs the SignUp View.//w  w  w  .j a  va2s. c  om
 * @param context The activity context.
 * @param attrs The Attribute Set for the view from which the resources can be accessed.
 * @param defStyleAttr The resource identifier for the default style attribute.
 */
public SignUpView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setOrientation(VERTICAL);

    final int backgroundColor;
    if (isInEditMode()) {
        backgroundColor = Color.DKGRAY;
    } else {
        final TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.SignUpView);
        backgroundColor = styledAttributes.getInt(R.styleable.SignUpView_signUpViewBackgroundColor,
                Color.DKGRAY);
        styledAttributes.recycle();
    }

    this.fontFamily = CognitoUserPoolsSignInProvider.getFontFamily();
    this.typeFace = Typeface.create(this.fontFamily, Typeface.NORMAL);
    this.fullScreenBackgroundColor = CognitoUserPoolsSignInProvider.isBackgroundColorFullScreen();
    this.backgroundColor = CognitoUserPoolsSignInProvider.getBackgroundColor();

    if (this.fullScreenBackgroundColor) {
        this.backgroundDrawable = new BackgroundDrawable(this.backgroundColor);
    } else {
        this.splitBackgroundDrawable = new SplitBackgroundDrawable(0, this.backgroundColor);
    }
}

From source file:cn.njmeter.njmeter.widget.spinner.NiceSpinner.java

private void init(Context context, AttributeSet attrs) {
    Resources resources = getResources();
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NiceSpinner);
    int defaultPadding = resources.getDimensionPixelSize(R.dimen.one_and_a_half_grid_unit);

    setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
    setPadding(resources.getDimensionPixelSize(R.dimen.three_grid_unit), defaultPadding, defaultPadding,
            defaultPadding);//from w w  w. j  a v a2s. c  o m
    setClickable(true);

    backgroundSelector = typedArray.getResourceId(R.styleable.NiceSpinner_backgroundSelector,
            R.drawable.selector);
    setBackgroundResource(backgroundSelector);
    textColor = typedArray.getColor(R.styleable.NiceSpinner_textTint, getDefaultTextColor(context));
    setTextColor(textColor);

    listView = new ListView(context);
    // Set the spinner's id into the listview to make it pretend to be the right parent in
    // onItemClick
    listView.setId(getId());
    listView.setDivider(new ColorDrawable(Color.GRAY));
    listView.setDividerHeight(1);
    listView.setItemsCanFocus(true);
    //hide vertical and horizontal scrollbars
    listView.setVerticalScrollBarEnabled(false);
    listView.setHorizontalScrollBarEnabled(false);
    listView.setOverScrollMode(OVER_SCROLL_NEVER);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedIndex = position;

            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(parent, view, position, id);
            }

            if (onItemSelectedListener != null) {
                onItemSelectedListener.onItemSelected(parent, view, position, id);
            }

            adapter.setSelectedIndex(position);
            setTextInternal(adapter.getItemInDataset(position).toString());
            dismissDropDown();
        }
    });

    popupWindow = new PopupWindow(context);
    popupWindow.setContentView(listView);
    popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        popupWindow.setElevation(DEFAULT_ELEVATION);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.spinner_drawable));
    } else {
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.drop_down_shadow));
    }

    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            if (!isArrowHidden) {
                animateArrow(false);
            }
        }
    });

    isArrowHidden = typedArray.getBoolean(R.styleable.NiceSpinner_hideArrow, false);
    arrowDrawableTint = typedArray.getColor(R.styleable.NiceSpinner_arrowTint, Integer.MAX_VALUE);
    arrowDrawableResId = typedArray.getResourceId(R.styleable.NiceSpinner_arrowDrawable, R.drawable.arrow);
    dropDownListPaddingBottom = typedArray
            .getDimensionPixelSize(R.styleable.NiceSpinner_dropDownListPaddingBottom, 0);
    typedArray.recycle();

    measureDisplayHeight();
}

From source file:com.adithyaupadhya.uimodule.roundcornerprogressbar.BaseRoundCornerProgressBar.java

private void setupStyleable(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerProgress);

    radius = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcRadius,
            dp2px(DEFAULT_PROGRESS_RADIUS));
    padding = (int) typedArray.getDimension(R.styleable.RoundCornerProgress_rcBackgroundPadding,
            dp2px(DEFAULT_BACKGROUND_PADDING));

    isReverse = typedArray.getBoolean(R.styleable.RoundCornerProgress_rcReverse, false);

    max = typedArray.getFloat(R.styleable.RoundCornerProgress_rcMax, DEFAULT_MAX_PROGRESS);
    progress = typedArray.getFloat(R.styleable.RoundCornerProgress_rcProgress, DEFAULT_PROGRESS);
    secondaryProgress = typedArray.getFloat(R.styleable.RoundCornerProgress_rcSecondaryProgress,
            DEFAULT_SECONDARY_PROGRESS);

    int colorBackgroundDefault = ContextCompat.getColor(context,
            R.color.round_corner_progress_bar_background_default);
    colorBackground = typedArray.getColor(R.styleable.RoundCornerProgress_rcBackgroundColor,
            colorBackgroundDefault);//  w  w w . j  a va2 s  .co m
    int colorProgressDefault = ContextCompat.getColor(context,
            R.color.round_corner_progress_bar_progress_default);
    colorProgress = typedArray.getColor(R.styleable.RoundCornerProgress_rcProgressColor, colorProgressDefault);
    int colorSecondaryProgressDefault = ContextCompat.getColor(context,
            R.color.round_corner_progress_bar_secondary_progress_default);
    colorSecondaryProgress = typedArray.getColor(R.styleable.RoundCornerProgress_rcSecondaryProgressColor,
            colorSecondaryProgressDefault);
    typedArray.recycle();

    initStyleable(context, attrs);
}

From source file:cn.njmeter.njmeter.widget.spinner.NiceSpinner.java

private int getDefaultTextColor(Context context) {
    TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
    TypedArray typedArray = context.obtainStyledAttributes(typedValue.data,
            new int[] { android.R.attr.textColorPrimary });
    int defaultTextColor = typedArray.getColor(0, Color.BLACK);
    typedArray.recycle();//from  w w w . j ava2 s  .c  om
    return defaultTextColor;
}

From source file:com.quarterfull.newsAndroid.NewsReaderDetailFragment.java

@Override
public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState) {
    super.onInflate(context, attrs, savedInstanceState);
    TypedArray a = context.obtainStyledAttributes(attrs,
            new int[] { R.attr.markasreadDrawable, R.attr.starredDrawable, R.attr.colorAccent });
    markAsReadDrawable = a.getDrawable(0);
    starredDrawable = a.getDrawable(1);/*from  w  w  w .jav  a 2s  .  co  m*/
    accentColor = a.getColor(2, ContextCompat.getColor(context, R.color.owncloudBlueLight));
    a.recycle();
}