public YourView(Context context, AttributeSet attrs) { super(context, attrs); int[] set = { android.R.attr.background, // idx 0 android.R.attr.text // idx 1 }; TypedArray a = context.obtainStyledAttributes(attrs, set); Drawable d = a.getDrawable(0); CharSequence t = a.getText(1); Log.d(TAG, "attrs " + d + " " + t); a.recycle(); }
/** * Parse attributes during inflation from a view hierarchy into the * arguments we handle. */ @Override public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { super.onInflate(activity, attrs, savedInstanceState); Log.v(TAG,"onInflate called"); TypedArray a = activity.obtainStyledAttributes(attrs,R.styleable.MyFragment); CharSequence myString = a.getText(R.styleable.MyFragment_my_string); if(myString != null) { Log.v(TAG, "My String Received : " + myString.toString()); } int myInteger = a.getInt(R.styleable.AdFragment_my_integer, -1); if(myInteger != -1) { Log.v(TAG,"My Integer Received :" + myInteger); } a.recycle(); }
private void init(AttributeSet attrs, int defStyleAttr) { if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextMarker, defStyleAttr, 0); for (int i = 0; i < a.getIndexCount(); i++) { int attr = a.getIndex(i); if (attr == R.styleable.TextMarker_carbon_text) { setText(a.getText(attr).toString()); } else if (attr == R.styleable.TextMarker_carbon_textView) { id = a.getResourceId(attr, 0); } } a.recycle(); } }
final CharSequence prefix = typedArray.getText(R.styleable.CounterView_prefix); if (prefix != null) { this.prefix = prefix.toString(); final CharSequence suffix = typedArray.getText(R.styleable.CounterView_suffix); if (suffix != null) { this.suffix = suffix.toString(); final CharSequence text = typedArray.getText(R.styleable.CounterView_android_text); if (text != null) { this.text = text.toString();
try { isAppendMode = a.getBoolean(R.styleable.TagGroup_atg_isAppendMode, false); inputHint = a.getText(R.styleable.TagGroup_atg_inputHint); borderColor = a.getColor(R.styleable.TagGroup_atg_borderColor, default_border_color); textColor = a.getColor(R.styleable.TagGroup_atg_textColor, default_text_color);
@Test public void obtainStyledAttributes_shouldExtractAttributesFromAttributeSet() throws Exception { AttributeSet roboAttributeSet = Robolectric.buildAttributeSet() .addAttribute(R.attr.itemType, "ungulate") .addAttribute(R.attr.scrollBars, "horizontal|vertical") .addAttribute(R.attr.quitKeyCombo, "^q") .addAttribute(R.attr.aspectRatio, "1.5") .addAttribute(R.attr.aspectRatioEnabled, "true") .build(); TypedArray a = context.obtainStyledAttributes(roboAttributeSet, R.styleable.CustomView); assertThat(a.getInt(R.styleable.CustomView_itemType, -1234)).isEqualTo(1 /* ungulate */); assertThat(a.getInt(R.styleable.CustomView_scrollBars, -1234)).isEqualTo(0x300); assertThat(a.getString(R.styleable.CustomView_quitKeyCombo)).isEqualTo("^q"); assertThat(a.getText(R.styleable.CustomView_quitKeyCombo).toString()).isEqualTo("^q"); assertThat(a.getFloat(R.styleable.CustomView_aspectRatio, 1f)).isEqualTo(1.5f); assertThat(a.getBoolean(R.styleable.CustomView_aspectRatioEnabled, false)).isTrue(); TypedArray typedArray = context.obtainStyledAttributes(roboAttributeSet, new int[]{R.attr.quitKeyCombo, R.attr.itemType}); assertThat(typedArray.getString(0)).isEqualTo("^q"); assertThat(typedArray.getInt(1, -1234)).isEqualTo(1 /* ungulate */); } }
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView); CharSequence t = a.getText(R.styleable.MyCustomView_android_text); a.recycle();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextView); CharSequence t = a.getText(R.styleable.TextView_text); int color = a.getColor(R.styleable.TextView_textColor, context.getResources().getColor(android.R.color.darker_gray)); // or other default color a.recycle();
/** * Obtains the title of the toolbar, which is shown, when the tab switcher is shown, from a * specific typed array. * * @param typedArray * The typed array, the title should be obtained from, as an instance of the class * {@link TypedArray}. The typed array may not be null */ private void obtainToolbarTitle(@NonNull final TypedArray typedArray) { setToolbarTitle(typedArray.getText(R.styleable.TabSwitcher_toolbarTitle)); }
private void init(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { LayoutInflater.from(context).inflate(R.layout.widget_navigation_item, this, true); ButterKnife.bind(this, this); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavigationItem, defStyleAttr, defStyleRes); iconNormal = a.getDrawable(R.styleable.NavigationItem_iconNormal); iconChecked = a.getDrawable(R.styleable.NavigationItem_iconChecked); checked = a.getBoolean(R.styleable.NavigationItem_android_checked, false); imgIcon.setImageDrawable(checked ? iconChecked : iconNormal); tvTitle.setTextColor(ResUtils.getThemeAttrColor(context, checked ? R.attr.colorAccent : android.R.attr.textColorPrimary)); iconItem.setBackgroundColor(checked ? ResUtils.getThemeAttrColor(context, R.attr.widgetBackgroundDark) : Color.TRANSPARENT); tvTitle.setText(a.getText(R.styleable.NavigationItem_title)); setBadge(a.getInt(R.styleable.NavigationItem_badge, 0)); a.recycle(); }
public ButtonItem(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwButtonItem); mEnabled = a.getBoolean(R.styleable.SuwButtonItem_android_enabled, true); mText = a.getText(R.styleable.SuwButtonItem_android_text); mTheme = a.getResourceId(R.styleable.SuwButtonItem_android_theme, R.style.SuwButtonItem); a.recycle(); }
private void init(Context context,AttributeSet attrs){ TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.SuperTextView); mText = a.getText(R.styleable.SuperTextView_dynamicText); mDuration = a.getInt(R.styleable.SuperTextView_duration,mDuration); mSelectedColor = a.getColor(R.styleable.SuperTextView_selectedColor,mSelectedColor); mDynamicStyle = DynamicStyle.getFromInt(a.getInt(R.styleable.SuperTextView_dynamicStyle,0)); a.recycle(); }
/** * Obtains the message of the dialog, which is shown by the preference, from a specific typed * array. * * @param typedArray * The typed array, the message should be obtained from, as an instance of the class * {@link TypedArray}. The typed array may not be null */ private void obtainDialogMessage(@NonNull final TypedArray typedArray) { setDialogMessage(typedArray.getText(R.styleable.DialogPreference_android_dialogMessage)); }
/** * Obtains the unit, which is used for textual representation of the preference's value, from a * specific typed array. * * @param typedArray * The typed array, the unit should be obtained from, as an instance of the class {@link * TypedArray}. The typed array may not be null */ private void obtainUnit(@NonNull final TypedArray typedArray) { setUnit(typedArray.getText(R.styleable.AbstractUnitPreference_unit)); }
/** * Obtains the breadcrumb title from a specific typed array. * * @param typedArray * The typed array, the breadcrumb title should be obtained from, as an instance of the * class {@link TypedArray}. The typed array may not be null */ private void obtainBreadCrumbTitle(@NonNull final TypedArray typedArray) { setBreadCrumbTitle( typedArray.getText(R.styleable.NavigationPreference_android_breadCrumbTitle)); }
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { setOnPreferenceChangeListener(this); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListWithEditTextPreference, defStyleAttr, defStyleRes); otherSummary = a.getText(R.styleable.ListWithEditTextPreference_otherSummary); a.recycle(); }
/** * Obtains the symbol, which is used to separate floating point numbers for textual * representation, from a specific typed array. * * @param typedArray * The typed array, the symbol should be obtained from, as an instance of the class * {@link TypedArray}. The typed array may not be null */ private void obtainFloatingPointSeparator(@NonNull final TypedArray typedArray) { setFloatingPointSeparator( typedArray.getText(R.styleable.SeekBarPreference_floatingPointSeparator)); }
private void initAttrs(AttributeSet attrs){ TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs,R.styleable.timeLineView,0, 0); try { mText = a.getText(R.styleable.timeLineView_text); mTextColor = a.getColorStateList(R.styleable.timeLineView_textColor); mTextSize=a.getDimensionPixelSize(R.styleable.timeLineView_textSize,(int)DimUtil.dipToPx(14)); } finally { a.recycle(); } }
public HelperTextInputLayout(Context _context, AttributeSet _attrs) { super(_context, _attrs); final TypedArray a = getContext().obtainStyledAttributes( _attrs, R.styleable.HelperTextInputLayout,0,0); try { mHelperTextColor = a.getColorStateList(R.styleable.HelperTextInputLayout_helperTextColor); mHelperText = a.getText(R.styleable.HelperTextInputLayout_helperText); } finally { a.recycle(); } }
@SuppressLint("RestrictedApi") public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) { super(TintContextWrapper.wrap(context), attrs, defStyleAttr); mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this); mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr); final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.ToggleButton, defStyleAttr, R.style.Widget_Material_ToggleButton); mTextOn = a.getText(R.styleable.ToggleButton_textOn); mTextOff = a.getText(R.styleable.ToggleButton_textOff); syncTextState(); a.recycle(); }