Refine search
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(); }
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(); }
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 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(); }
// 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()
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); } } }
public PreferenceHelp(Context context, AttributeSet attrs) { super(context, attrs, R.attr.preferenceHelpStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PreferenceHelp); try { mLayoutResId = a.getResourceId(R.styleable.PreferenceHelp_dialogLayout, 0); mTitle = a.getString(R.styleable.PreferenceHelp_dialogTitle); } finally { a.recycle(); } }
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActivityPreference, defStyleAttr, defStyleRes); String clazzName = a.getString(R.styleable.ActivityPreference_activity); if (null != clazzName) { try { mActivityClazz = Class.forName(clazzName); } catch (ClassNotFoundException e) { Log.e(TAG, "Can't find class " + clazzName, e); } } a.recycle(); }
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DialogPreference, defStyleAttr, defStyleRes); mDialogTitle = a.getString(R.styleable.DialogPreference_dialogTitle); if (mDialogTitle == null) { // Fallback on the regular title of the preference // (the one that is seen in the list) mDialogTitle = getTitle(); } mDialogIcon = a.getDrawable(R.styleable.DialogPreference_dialogIcon); mPositiveButtonText = a.getString(R.styleable.DialogPreference_positiveButtonText); mNegativeButtonText = a.getString(R.styleable.DialogPreference_negativeButtonText); mDialogLayoutResId = a.getResourceId(R.styleable.DialogPreference_dialogLayout, mDialogLayoutResId); a.recycle(); }
public static void applyFont(TextView v, AttributeSet attrs, int defStyleAttr, int defStyleRes){ TypedArray a = v.getContext().obtainStyledAttributes(attrs, new int[]{R.attr.tv_fontFamily}, defStyleAttr, defStyleRes); String fontFamily = a.getString(0); if(fontFamily != null){ Typeface typeface = TypefaceUtil.load(v.getContext(), fontFamily, 0); v.setTypeface(typeface); } a.recycle(); }
/** * Tries to pull the Font Path from the View Style as this is the next decendent after being * defined in the View's xml. * * @param context Activity Activity Context * @param attrs View Attributes * @param attributeId if -1 returns null. * @return null if attribute is not defined or found in the Style */ static String pullFontPathFromStyle(Context context, AttributeSet attrs, int[] attributeId) { if (attributeId == null || attrs == null) return null; final TypedArray typedArray = context.obtainStyledAttributes(attrs, attributeId); if (typedArray != null) { try { // First defined attribute String fontFromAttribute = typedArray.getString(0); if (!TextUtils.isEmpty(fontFromAttribute)) { return fontFromAttribute; } } catch (Exception ignore) { // Failed for some reason. } finally { typedArray.recycle(); } } return null; }
public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MessagePreference, defStyleAttr, defStyleRes); String message = a.getString(R.styleable.MessagePreference_dialogMessage); if (a.getBoolean(R.styleable.MessagePreference_dialogMessageHtml, false)) { mDialogMessage = Html.fromHtml(message); } else { mDialogMessage = message; } mDialogMessageLinkify = a.getBoolean(R.styleable.MessagePreference_dialogMessageLinkify, false); a.recycle(); }
/** * Last but not least, try to pull the Font Path from the Theme, which is defined. * * @param context Activity Context * @param styleAttrId Theme style id * @param attributeId if -1 returns null. * @return null if no theme or attribute defined. */ static String pullFontPathFromTheme(Context context, int styleAttrId, int[] attributeId) { if (styleAttrId == -1 || attributeId == null) return null; final Resources.Theme theme = context.getTheme(); final TypedValue value = new TypedValue(); theme.resolveAttribute(styleAttrId, value, true); final TypedArray typedArray = theme.obtainStyledAttributes(value.resourceId, attributeId); try { String font = typedArray.getString(0); return font; } catch (Exception ignore) { // Failed for some reason. return null; } finally { typedArray.recycle(); } }
public ItemCardView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); mContext = context; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ItemCardView); cardName.setText(a.getString(R.styleable.ItemCardView_card_name)); cardImage.setImageDrawable(a.getDrawable(R.styleable.ItemCardView_card_image)); a.recycle(); }
public String resolveStringAttr(@AttrRes int attrResId, @StringRes int defResId) { mAttrs[0] = attrResId; TypedArray a = mTheme.obtainStyledAttributes(mAttrs); try { String result = a.getString(0); if (result == null) { result = resolveStringRes(defResId); } return result; } finally { a.recycle(); } }
public PatternPathMotion(@NonNull Context context, @NonNull AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PatternPathMotion); try { String pathData = a.getString(R.styleable.PatternPathMotion_patternPathData); if (pathData == null) { throw new RuntimeException("pathData must be supplied for patternPathMotion"); } Path pattern = PathParser.createPathFromPathData(pathData); setPatternPath(pattern); } finally { a.recycle(); } }
public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { setWidgetLayoutResource(R.layout.preference_widget_fixed_switch); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwitchPreference, defStyleAttr, defStyleRes); setSummaryOn(a.getString(R.styleable.SwitchPreference_summaryOn)); setSummaryOff(a.getString(R.styleable.SwitchPreference_summaryOff)); setSwitchTextOn(a.getString(R.styleable.SwitchPreference_switchTextOn)); setSwitchTextOff(a.getString(R.styleable.SwitchPreference_switchTextOff)); setDisableDependentsState(a.getBoolean(R.styleable.SwitchPreference_disableDependentsState, false)); a.recycle(); }