public MyCustomView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0); String str = a.getString(R.styleable.MyCustomView_my_custom_attribute); //do something with str a.recycle(); }
private void init(AttributeSet attrs) { TypedArray a=getContext().obtainStyledAttributes( attrs, R.styleable.MyCustomView); //Use a Log.i("test",a.getString( R.styleable.MyCustomView_android_text)); Log.i("test",""+a.getColor( R.styleable.MyCustomView_android_textColor, Color.BLACK)); Log.i("test",a.getString( R.styleable.MyCustomView_extraInformation)); //Don't forget this a.recycle(); }
TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs); // get resource ID by index imgs.getResourceId(i, -1) // or set you ImageView's resource to the id mImgView1.setImageResource(imgs.getResourceId(i, -1)); // recycle the array imgs.recycle();
public MyCustomElement(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomElement, 0, 0); try { distanceExample = ta.getDimension(R.styleable.MyCustomElement_distanceExample, 100.0f); } finally { ta.recycle(); } // ... }
public ImageAdapter(Context c) { mContext = c; TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1); mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0); a.recycle(); }
public DividerItemDecoration(Context context, AttributeSet attrs) { final TypedArray a = context .obtainStyledAttributes(attrs, new int[]{android.R.attr.listDivider}); mDivider = a.getDrawable(0); a.recycle(); }
public AHBottomNavigationBehavior(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AHBottomNavigationBehavior_Params); mTabLayoutId = a.getResourceId(R.styleable.AHBottomNavigationBehavior_Params_tabLayoutId, View.NO_ID); a.recycle(); }
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UrlPreference, defStyleAttr, defStyleRes); mUrl = a.getString(R.styleable.UrlPreference_url); a.recycle(); }
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {R.attr.homeIcon}); int attributeResourceId = a.getResourceId(0, 0); Drawable drawable = getResources().getDrawable(attributeResourceId); a.recycle();
public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); final TypedArray a = c.obtainStyledAttributes(attrs, ATTRS); a.recycle(); }
/** * Get the styleId from attributes. * @param context * @param attrs * @param defStyleAttr * @param defStyleRes * @return The styleId. */ public static int getStyleId(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){ TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ThemableView, defStyleAttr, defStyleRes); int styleId = a.getResourceId(R.styleable.ThemableView_v_styleId, 0); a.recycle(); return styleId; }
public CustomKeyboard(Context context, AttributeSet set) { super(context, set); TypedArray a = context.obtainStyledAttributes(set,R.styleable.custom_keyboard); CharSequence s = a.getString(R.styleable.custom_keyboard_alternative_key_label); if (s != null) { this.setAlternativeKeyLabel(s.toString()); } a.recycle(); }
TypedArray ids = getResources().obtainTypedArray(R.array.random_imgs); // Get resource id by its index ids.getResourceId(index, -1); // be sure to call TypedArray.recycle() when done with the array ids.recycle();
public class CustomImageButton extends ImageButton { private String customAttr; public CustomImageButton( Context context ) { this( context, null ); } public CustomImageButton( Context context, AttributeSet attrs ) { this( context, attrs, R.attr.customImageButtonStyle ); } public CustomImageButton( Context context, AttributeSet attrs, int defStyle ) { super( context, attrs, defStyle ); final TypedArray array = context.obtainStyledAttributes( attrs, R.styleable.CustomImageButton, defStyle, R.style.Widget_ImageButton_Custom ); // see below this.customAttr = array.getString( R.styleable.CustomImageButton_customAttr, "" ); array.recycle(); } }
public LiftOff(Context context, AttributeSet attrs) { super(context, attrs); final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LiftOff); initialElevation = ta.getDimension(R.styleable.LiftOff_initialElevation, 0f); finalElevation = ta.getDimension(R.styleable.LiftOff_finalElevation, 0f); ta.recycle(); }
public void setTextAppearanceForTextColor(int resId, boolean isForced) { boolean isTextColorForced = isForced || mTextColorId == 0; TypedArray appearance = mView.getContext().obtainStyledAttributes(resId, R.styleable.TextAppearance); if (appearance.hasValue(R.styleable.TextAppearance_android_textColor) && isTextColorForced) { setTextColor(appearance.getResourceId(R.styleable.TextAppearance_android_textColor, 0)); } appearance.recycle(); }
package your.package.widget; public class TypefacedTextView extends TextView { public TypefacedTextView(Context context, AttributeSet attrs) { super(context, attrs); //Typeface.createFromAsset doesn't work in the layout editor. Skipping... if (isInEditMode()) { return; } TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView); String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface); styledAttrs.recycle(); if (fontName != null) { Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName); setTypeface(typeface); } } }
final int resolveResIdAttr(@AttrRes int attrResId, int defResId) { mAttrs[0] = attrResId; TypedArray a = mTheme.obtainStyledAttributes(mAttrs); try { return a.getResourceId(0, defResId); } finally { a.recycle(); } }
// The attributes you want retrieved int[] attrs = {android.R.attr.textColor, android.R.attr.background, android.R.attr.text}; // Parse MyCustomStyle, using Context.obtainStyledAttributes() TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs); // Fetch the text from your style like this. String text = ta.getString(2); // Fetching the colors defined in your style int textColor = ta.getColor(0, Color.BLACK); int backgroundColor = ta.getColor(1, Color.BLACK); // Do some logging to see if we have retrieved correct values Log.i("Retrieved text:", text); Log.i("Retrieved textColor as hex:", Integer.toHexString(textColor)); Log.i("Retrieved background as hex:", Integer.toHexString(backgroundColor)); // OH, and don't forget to recycle the TypedArray ta.recycle()