Java Code Examples for android.util.AttributeSet#getAttributeResourceValue()

The following examples show how to use android.util.AttributeSet#getAttributeResourceValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AutoGridView.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the numColumns based on the attributeset
 */
private void init(AttributeSet attrs) {
    // Read numColumns out of the AttributeSet
    int count = attrs.getAttributeCount();
    if (count > 0) {
        for (int i = 0; i < count; i++) {
            String name = attrs.getAttributeName(i);
            if (name != null && name.equals("numColumns")) {
                // Update columns
                this.numColumnsID = attrs.getAttributeResourceValue(i, 1);
                updateColumns();
                break;
            }
        }
    }
    Log.d(TAG, "numColumns set to: " + numColumns);
}
 
Example 2
Source File: NumberPickerPreference.java    From ForceDoze with GNU General Public License v3.0 6 votes vote down vote up
private void init(Context context, AttributeSet attrs) {
    setOnPreferenceClickListener(this);

    if (attrs != null) {
        int title = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "title", -1);
        if (title != -1) {
            mTitle = context.getString(title);
        }
        mBindSummary = attrs.getAttributeBooleanValue(null, "bindSummary", DEFAULT_BIND_SUMMARY);
        mMin = attrs.getAttributeIntValue(null, "min", DEFAULT_MIN);
        mMax = attrs.getAttributeIntValue(null, "max", DEFAULT_MAX);
        mStep = attrs.getAttributeIntValue(null, "step", DEFAULT_STEP);
        mCurrentValue = mMin;
    }

    if (mTitle == null) {
        mTitle = "Choose delay (minutes)";
    }
    if (mMax < mMin) {
        throw new AssertionError("max value must be > min value");
    }
    if (mStep <= 0) {
        throw new AssertionError("step value must be > 0");
    }

}
 
Example 3
Source File: Card.java    From MoeGallery with GNU General Public License v3.0 6 votes vote down vote up
protected void setAttributes(AttributeSet attrs){
	
	setBackgroundResource(R.drawable.background_button_rectangle);
	//Set background Color
	// Color by resource
	int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,"background",-1);
	if(bacgroundColor != -1){
		setBackgroundColor(getResources().getColor(bacgroundColor));
	}else{
		// Color by hexadecimal
		String background = attrs.getAttributeValue(ANDROIDXML,"background");
		if(background != null)
			setBackgroundColor(Color.parseColor(background));
		else
			setBackgroundColor(this.backgroundColor);
	}
}
 
Example 4
Source File: ViewSkinHelper.java    From ThemeDemo with Apache License 2.0 6 votes vote down vote up
/**
 * 设置背景色
 * Set background Color
 */
public void init(View view, AttributeSet attrs) {

    mView = view;
    if (attrs == null) {
        mEnable = false;
        return;
    }
    mLightBackgroundRes = attrs.getAttributeResourceValue(ANDROIDXML, "background", -1);
    mDarkBackgroundRes = attrs.getAttributeResourceValue(MATERIALDESIGNXML, "nightBackground", -1);
    if (mLightBackgroundRes == -1) {
        mLightBackgroundColor = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
    }
    if (mDarkBackgroundRes == -1) {
        mDarkBackgroundColor = attrs.getAttributeIntValue(MATERIALDESIGNXML, "nightBackground", -1);
    }

}
 
Example 5
Source File: CalligraphyUtils.java    From Calligraphy with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to pull the Custom Attribute directly from the TextView.
 *
 * @param context     Activity Context
 * @param attrs       View Attributes
 * @param attributeId if -1 returns null.
 * @return null if attribute is not defined or added to View
 */
static String pullFontPathFromView(Context context, AttributeSet attrs, int[] attributeId) {
    if (attributeId == null || attrs == null)
        return null;

    final String attributeName;
    try {
        attributeName = context.getResources().getResourceEntryName(attributeId[0]);
    } catch (Resources.NotFoundException e) {
        // invalid attribute ID
        return null;
    }

    final int stringResourceId = attrs.getAttributeResourceValue(null, attributeName, -1);
    return stringResourceId > 0
            ? context.getString(stringResourceId)
            : attrs.getAttributeValue(null, attributeName);
}
 
Example 6
Source File: ListViewSkinHelper.java    From ThemeDemo with Apache License 2.0 6 votes vote down vote up
@Override
public void init(View view, AttributeSet attrs) {
    super.init(view, attrs);

    if (attrs == null) {
        mEnable = false;
        return;
    }


    mDividerRes = attrs.getAttributeResourceValue(ANDROIDXML, "divider", -1);
    if (mDividerRes == -1) {
        mDivider = attrs.getAttributeIntValue(ANDROIDXML, "divider", -1);
    }


    mNightLVDividerRes = attrs.getAttributeResourceValue(MATERIALDESIGNXML, "nightLVDivider", -1);
    if (mNightLVDividerRes != -1) {
        mNightLVDivider = attrs.getAttributeIntValue(MATERIALDESIGNXML, "nightLVDivider", -1);
    }

}
 
Example 7
Source File: AddItemView.java    From Moring-Alarm with Apache License 2.0 5 votes vote down vote up
public AddItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext=context;
    Title=attrs.getAttributeValue(NAMESPACE,"aivtitle");
    Desc=attrs.getAttributeValue(NAMESPACE, "aivdesc");
    src=attrs.getAttributeResourceValue(NAMESPACE,"aivsrc",R.mipmap.icon_trangel);
    initView();
}
 
Example 8
Source File: PictureView.java    From UI-Motion with Apache License 2.0 5 votes vote down vote up
public PictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    if (attrs != null) {
        final String namespace = "http://schemas.android.com/apk/res/android";
        final String attribute = "src";
        mResId = attrs.getAttributeResourceValue(namespace, attribute, 0);
    }
}
 
Example 9
Source File: Switch.java    From MaterialDesignLibrary with Apache License 2.0 5 votes vote down vote up
protected void setAttributes(AttributeSet attrs) {

        setBackgroundResource(R.drawable.background_transparent);

        // Set size of view
        setMinimumHeight(Utils.dpToPx(48, getResources()));
        setMinimumWidth(Utils.dpToPx(80, getResources()));

        // Set background Color
        // Color by resource
        int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
                "background", -1);
        if (bacgroundColor != -1) {
			setBackgroundColor(getResources().getColor(bacgroundColor));
		} else {
			// Color by hexadecimal
			int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
			if (background != -1)
				setBackgroundColor(background);
		}

		check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "check",
				false);
		eventCheck = check;
		ball = new Ball(getContext());
		RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
				getResources()), Utils.dpToPx(20, getResources()));
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		ball.setLayoutParams(params);
		addView(ball);

	}
 
Example 10
Source File: GifViewUtils.java    From sketch with Apache License 2.0 5 votes vote down vote up
private static int getResourceId(ImageView view, AttributeSet attrs, final boolean isSrc) {
	final int resId = attrs.getAttributeResourceValue(ANDROID_NS, isSrc ? "src" : "background", 0);
	if (resId > 0) {
		final String resourceTypeName = view.getResources().getResourceTypeName(resId);
		if (SUPPORTED_RESOURCE_TYPE_NAMES.contains(resourceTypeName) && !setResource(view, isSrc, resId)) {
			return resId;
		}
	}
	return 0;
}
 
Example 11
Source File: WidgetUtils.java    From document-viewer with GNU General Public License v3.0 5 votes vote down vote up
public static String getStringAttribute(final Context context, final AttributeSet attrs, final String namespace,
        final String name, final String defValue) {
    final int resId = attrs.getAttributeResourceValue(namespace, name, Integer.MIN_VALUE);
    if (resId != Integer.MIN_VALUE) {
        return context.getResources().getString(resId);
    }
    return LengthUtils.safeString(attrs.getAttributeValue(namespace, name), defValue);
}
 
Example 12
Source File: Slider.java    From DMAudioStreamer with Apache License 2.0 5 votes vote down vote up
protected void setAttributes(AttributeSet attrs) {

        setBackgroundResource(R.drawable.background_transparent);

        // Set size of view
        setMinimumHeight(dpToPx(48, getResources()));
        setMinimumWidth(dpToPx(80, getResources()));

        // Set background Color
        // Color by resource
        int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML, "background", -1);
        if (bacgroundColor != -1) {
            setBackgroundColor(getResources().getColor(bacgroundColor));
        } else {
            // Color by hexadecimal
            int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
            if (background != -1)
                setBackgroundColor(background);
        }

        min = attrs.getAttributeIntValue(MATERIALDESIGNXML, "min", 0);
        max = attrs.getAttributeIntValue(MATERIALDESIGNXML, "max", 0);
        value = attrs.getAttributeIntValue(MATERIALDESIGNXML, "value", min);

        ball = new Ball(getContext());
        LayoutParams params = new LayoutParams(dpToPx(15, getResources()), dpToPx(15, getResources()));
        params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        ball.setLayoutParams(params);
        addView(ball);

    }
 
Example 13
Source File: Switch.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
protected void setAttributes(AttributeSet attrs) {

        setBackgroundResource(R.drawable.background_transparent);

        // Set size of view
        setMinimumHeight(Utils.dpToPx(48, getResources()));
        setMinimumWidth(Utils.dpToPx(80, getResources()));

        // Set background Color
        // Color by resource
        int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
                "background", -1);
        if (bacgroundColor != -1) {
			setBackgroundColor(getResources().getColor(bacgroundColor));
		} else {
			// Color by hexadecimal
			int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
			if (background != -1)
				setBackgroundColor(background);
		}

		check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "check",
				false);
		eventCheck = check;
		ball = new Ball(getContext());
		RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
				getResources()), Utils.dpToPx(20, getResources()));
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		ball.setLayoutParams(params);
		addView(ball);

	}
 
Example 14
Source File: RippleView.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
protected void setRippleAttributes(AttributeSet attrs) {
	/**
	 * 初始化按压时涟漪的颜色
	 * Set Ripple Color
	 * Color by resource
	 */
	int color = attrs.getAttributeResourceValue(MATERIALDESIGNXML,"rippleColor",-1);
	if(color != -1){
		rippleColor = getResources().getColor(color);
		settedRippleColor = true;
	}else{
		// Color by hexadecimal
		int rColor = attrs.getAttributeIntValue(MATERIALDESIGNXML, "rippleColor", -1);// 16进制的颜色
		if(rColor != -1 && !isInEditMode()) {
			rippleColor = rColor;
			settedRippleColor = true;
		}
	}
	
	/**
	 * 初始化涟漪扩展的速度 
	 * init Ripple speed
	 */
	rippleSpeed = attrs.getAttributeFloatValue(MATERIALDESIGNXML, "rippleSpeed", rippleSpeed);
	
	/**
	 * 设定涟漪的响应时间
	 */
	clickAfterRipple = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "clickAfterRipple", clickAfterRipple);
}
 
Example 15
Source File: ButtonFloat.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
protected void onInitAttributes(AttributeSet attrs) {
	super.onInitAttributes(attrs);
	// 设置按钮中的图标
	int iconResource = attrs.getAttributeResourceValue(MATERIALDESIGNXML,"iconDrawable",-1);
	if (iconResource != -1) {
		iconDrawable = getResources().getDrawable(iconResource);
	}

	// animation
	boolean animate = attrs.getAttributeBooleanValue(MATERIALDESIGNXML, "animate", false);
	if (animate) {
		playAnimation();
	}
	
	if (iconDrawable != null) {
		icon.setBackgroundDrawable(iconDrawable);
	}
	// 设置按钮中图标的大小
	String size = attrs.getAttributeValue(MATERIALDESIGNXML, "iconSize");
	if (size != null) {
		iconSize = (int) Utils.dipOrDpToFloat(size);
	}
	setIconParams();
	addView(icon);
}
 
Example 16
Source File: ColorPickerPreference.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
private void init(final Context context, final AttributeSet attrs) {
    if (AppContext.DEBUG) {
        Log.d(ColorPickerPreference.TAG, "init");
    }
    this.mDensity = getContext().getResources().getDisplayMetrics().density;
    setOnPreferenceClickListener(this);
    if (attrs != null) {
        final String defaultValue = attrs.getAttributeValue(
                ColorPickerPreference.ANDROID_NS, "defaultValue");
        if (defaultValue.startsWith("#")) {
            try {
                this.mDefaultValue = ColorPickerPreference
                        .convertToColorInt(defaultValue);
            } catch (final NumberFormatException e) {
                this.mDefaultValue = ColorPickerPreference
                        .convertToColorInt("#FF000000");
            }
        } else {
            final int resourceId = attrs.getAttributeResourceValue(
                    ColorPickerPreference.ANDROID_NS, "defaultValue", 0);
            if (resourceId != 0) {
                this.mDefaultValue = context.getResources().getColor(
                        resourceId);
            }
        }
        this.mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null,
                "alphaSlider", false);
    }
    this.mValue = this.mDefaultValue;
}
 
Example 17
Source File: DraggableViewPager.java    From android-draggable-viewpager with Apache License 2.0 4 votes vote down vote up
private void setBackground(AttributeSet attrs) {
    final String xmlns = "http://schemas.android.com/apk/res/android";
    xmlRes = attrs.getAttributeResourceValue(xmlns, "background", -1);
}
 
Example 18
Source File: Slider.java    From meiShi with Apache License 2.0 4 votes vote down vote up
protected void setAttributes(AttributeSet attrs) {

        setBackgroundResource(R.drawable.background_transparent);

        // Set size of view
        setMinimumHeight(Utils.dpToPx(48, getResources()));
        setMinimumWidth(Utils.dpToPx(80, getResources()));

        // Set background Color
        // Color by resource
        int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,
                "background", -1);
        if (bacgroundColor != -1) {
            setBackgroundColor(getResources().getColor(bacgroundColor));
        } else {
            // Color by hexadecimal
            int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
            if (background != -1)
                setBackgroundColor(background);
        }

        showNumberIndicator = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
                "showNumberIndicator", false);
        min = attrs.getAttributeIntValue(MATERIALDESIGNXML, "min", 0);
        max = attrs.getAttributeIntValue(MATERIALDESIGNXML, "max", 0);
        value = attrs.getAttributeIntValue(MATERIALDESIGNXML, "value", min);

        ball = new Ball(getContext());
        RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
                getResources()), Utils.dpToPx(20, getResources()));
        params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        ball.setLayoutParams(params);
        addView(ball);

        // Set if slider content number indicator
        // TODO
        if (showNumberIndicator) {
            numberIndicator = new NumberIndicator(getContext());
        }

    }
 
Example 19
Source File: ButtonRectangle.java    From XERUNG with Apache License 2.0 4 votes vote down vote up
@Override
protected void onInitAttributes(AttributeSet attrs) {
	super.onInitAttributes(attrs);
	if (isInEditMode()) {
		// 为了在编译器中预览时不报空指针,在这里产生一个textView对象。实际中不会产生的。
		textButton = new TextView(getContext());
	}
	String text = null;
	/**
	 * 设置按钮上的文字内容
	 */
	int textResource = attrs.getAttributeResourceValue(ANDROIDXML,"text",-1);
	if(textResource != -1){
		text = getResources().getString(textResource);
	}else{
		//如果没有文字资源,也就是@String/xx,那么就设置文字
		text = attrs.getAttributeValue(ANDROIDXML,"text");
	}
	
	/**
	 * 当文字不为空的时候,TextView设置文字,否则不设置文字
	 */
	if(text != null){
		textButton.setText(text);
	}
	
	/**
	 * 设置textSize
	 */
	String textSize = attrs.getAttributeValue(ANDROIDXML,"textSize");
	if (text != null && textSize != null) {
		textSize = textSize.substring(0, textSize.length() - 2);//12sp->12
		textButton.setTextSize(Float.parseFloat(textSize));
	}
	
	/**
	 * 设置textColor
	 */
	int textColor = attrs.getAttributeResourceValue(ANDROIDXML,"textColor",-1);
	if(text != null && textColor != -1){
		textButton.setTextColor(getResources().getColor(textColor));
	}
	else if(text != null ){
		// 16进制的color
		String color = attrs.getAttributeValue(ANDROIDXML,"textColor");
		if(color != null && !isInEditMode()) {
			textButton.setTextColor(Color.parseColor(color));
		}else {
			textButton.setTextColor(defaultTextColor);
		}
	}
	textButton.setTypeface(null, Typeface.BOLD);
	//textButton.setPadding(5, 5, 5, 5);
	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
			LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
	params.setMargins(Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(5, getResources()));
	textButton.setLayoutParams(params);
	addView(textButton);
	
}
 
Example 20
Source File: AnimatorInflater.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static StateListAnimator createStateListAnimatorFromXml(Context context,
        XmlPullParser parser, AttributeSet attributeSet)
        throws IOException, XmlPullParserException {
    int type;
    StateListAnimator stateListAnimator = new StateListAnimator();

    while (true) {
        type = parser.next();
        switch (type) {
            case XmlPullParser.END_DOCUMENT:
            case XmlPullParser.END_TAG:
                return stateListAnimator;

            case XmlPullParser.START_TAG:
                // parse item
                Animator animator = null;
                if ("item".equals(parser.getName())) {
                    int attributeCount = parser.getAttributeCount();
                    int[] states = new int[attributeCount];
                    int stateIndex = 0;
                    for (int i = 0; i < attributeCount; i++) {
                        int attrName = attributeSet.getAttributeNameResource(i);
                        if (attrName == R.attr.animation) {
                            final int animId = attributeSet.getAttributeResourceValue(i, 0);
                            animator = loadAnimator(context, animId);
                        } else {
                            states[stateIndex++] =
                                    attributeSet.getAttributeBooleanValue(i, false) ?
                                            attrName : -attrName;
                        }
                    }
                    if (animator == null) {
                        animator = createAnimatorFromXml(context.getResources(),
                                context.getTheme(), parser, 1f);
                    }

                    if (animator == null) {
                        throw new Resources.NotFoundException(
                                "animation state item must have a valid animation");
                    }
                    stateListAnimator
                            .addState(StateSet.trimStateSet(states, stateIndex), animator);
                }
                break;
        }
    }
}