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

The following examples show how to use android.util.AttributeSet#getAttributeValue() . 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: ColorPicker.java    From xDrip with GNU General Public License v3.0 7 votes vote down vote up
public ColorPicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (attrs != null) {
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            if (attrs.getAttributeName(i).equals("defaultValue")) {
                final String this_value = attrs.getAttributeValue(i);

                if (this_value.length() > 0) {
                    if (debug)
                        Log.d(TAG, "Attribute debug: " + i + " " + attrs.getAttributeName(i) + " " + this_value);
                    if (this_value.startsWith("@")) {
                        localDefaultValue = Color.parseColor(getStringResourceByName(this_value));
                    } else {
                        localDefaultValue = Color.parseColor(this_value);
                    }
                    localDefaultValue = Color.parseColor(attrs.getAttributeValue(i));
                    super.defaultColor = localDefaultValue;
                } else {
                    Log.w(TAG, "No default value for colorpicker");
                }
                break;
            }

        }
    }
}
 
Example 2
Source File: Chip.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void validateAttributes(@Nullable AttributeSet attributeSet) {
  if (attributeSet == null) {
    return;
  }
  if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "background") != null) {
    Log.w(TAG, "Do not set the background; Chip manages its own background drawable.");
  }
  if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableLeft") != null) {
    throw new UnsupportedOperationException("Please set left drawable using R.attr#chipIcon.");
  }
  if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableStart") != null) {
    throw new UnsupportedOperationException("Please set start drawable using R.attr#chipIcon.");
  }
  if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableEnd") != null) {
    throw new UnsupportedOperationException("Please set end drawable using R.attr#closeIcon.");
  }
  if (attributeSet.getAttributeValue(NAMESPACE_ANDROID, "drawableRight") != null) {
    throw new UnsupportedOperationException("Please set end drawable using R.attr#closeIcon.");
  }
  if (!attributeSet.getAttributeBooleanValue(NAMESPACE_ANDROID, "singleLine", true)
      || (attributeSet.getAttributeIntValue(NAMESPACE_ANDROID, "lines", 1) != 1)
      || (attributeSet.getAttributeIntValue(NAMESPACE_ANDROID, "minLines", 1) != 1)
      || (attributeSet.getAttributeIntValue(NAMESPACE_ANDROID, "maxLines", 1) != 1)) {
    throw new UnsupportedOperationException("Chip does not support multi-line text");
  }

  if (attributeSet.getAttributeIntValue(
          NAMESPACE_ANDROID, "gravity", (Gravity.CENTER_VERTICAL | Gravity.START))
      != (Gravity.CENTER_VERTICAL | Gravity.START)) {
    Log.w(TAG, "Chip text must be vertically center and start aligned");
  }
}
 
Example 3
Source File: FontPreferenceCompat.java    From memetastic with GNU General Public License v3.0 5 votes vote down vote up
private void loadFonts(Context context, @Nullable AttributeSet attrs) {
    _defaultValue = _fontValues[0];
    if (attrs != null) {
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            String attrValue = attrs.getAttributeValue(i);
            if (attrName.equalsIgnoreCase("defaultValue")) {
                if (attrValue.startsWith("@")) {
                    int resId = Integer.valueOf(attrValue.substring(1));
                    attrValue = getContext().getString(resId);
                }
                _defaultValue = attrValue;
                break;
            }
        }
    }

    for (File file : getAdditionalFonts()) {
        _fontNames = appendToArray(_fontNames, file.getName().replace(".ttf", "").replace(".TTF", ""));
        _fontValues = appendToArray(_fontValues, file.getAbsolutePath());
    }

    Spannable[] fontText = new Spannable[_fontNames.length];
    for (int i = 0; i < _fontNames.length; i++) {
        fontText[i] = new SpannableString(_fontNames[i] + "\n" + _fontValues[i]);
        fontText[i].setSpan(new TypefaceObjectSpan(typeface(getContext(), _fontValues[i], null)), 0, _fontNames[i].length(), 0);
        fontText[i].setSpan(new RelativeSizeSpan(0.7f), _fontNames[i].length() + 1, fontText[i].length(), 0);

    }
    setDefaultValue(_defaultValue);
    setEntries(fontText);
    setEntryValues(_fontValues);
}
 
Example 4
Source File: SeekBarPreference.java    From SecondScreen with Apache License 2.0 5 votes vote down vote up
private String getAttributeStringValue(AttributeSet attrs, String namespace, String name, String defaultValue) {
	String value = attrs.getAttributeValue(namespace, name);
	if(value == null)
		value = defaultValue;
	
	return value;
}
 
Example 5
Source File: ViewProducer.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
static View createViewFromTag(Context context, String name, AttributeSet attrs) {
    if (name.equals("view")) {
        name = attrs.getAttributeValue(null, "class");
    }

    try {
        mConstructorArgs[0] = context;
        mConstructorArgs[1] = attrs;

        if (-1 == name.indexOf('.')) {
            for (int i = 0; i < sClassPrefixList.length; i++) {
                final View view = createView(context, name, sClassPrefixList[i]);
                if (view != null) {
                    return view;
                }
            }
            return null;
        } else {
            return createView(context, name, null);
        }
    } catch (Exception e) {
        // We do not want to catch these, lets return null and let the actual LayoutInflater
        // try
        return null;
    } finally {
        // Don't retain references on context.
        mConstructorArgs[0] = null;
        mConstructorArgs[1] = null;
    }
}
 
Example 6
Source File: InformationDialogPreference.java    From masterpassword with GNU General Public License v3.0 5 votes vote down vote up
public String getAttributeStringValue(Context context1, AttributeSet attributeset, String namespace, String attributeName, String defaultString) {
    Resources resources = context1.getResources();
    String value = attributeset.getAttributeValue(namespace, attributeName);
    if (value == null) {
        return defaultString;
    } else {
        return stringByName(resources, value);
    }
}
 
Example 7
Source File: MapGeneratorFactory.java    From WhereYouGo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param attributeSet A collection of attributes which includes the desired MapGenerator.
 * @return a new MapGenerator instance.
 */
public static MapGenerator createMapGenerator(AttributeSet attributeSet) {
    String mapGeneratorName = attributeSet.getAttributeValue(null, MAP_GENERATOR_ATTRIBUTE_NAME);
    if (mapGeneratorName == null) {
        return new DatabaseRenderer();
    }

    MapGeneratorInternal mapGeneratorInternal = MapGeneratorInternal.valueOf(mapGeneratorName);
    return MapGeneratorFactory.createMapGenerator(mapGeneratorInternal);
}
 
Example 8
Source File: CustomServerSettingEditTextPreference.java    From AndroidPirateBox with MIT License 5 votes vote down vote up
/**
 * Reads attribute values and initializes members
 * 
 * @param attrs attributes to read from
 */
private void readAttributes(AttributeSet attrs) {
	pawSetting = attrs.getAttributeValue(PIRATEBOX_NAMESPACE, "setting");
	isNumeric = attrs.getAttributeValue(ANDROID_NAMESPACE, "numeric") != null ? true : false;
	try {
		numericDivider = Long.valueOf(attrs.getAttributeValue(PIRATEBOX_NAMESPACE, "numericDivider"));
	}
	catch(Exception e) {
		numericDivider = null;
	}
}
 
Example 9
Source File: WidgetUtils.java    From document-viewer with GNU General Public License v3.0 5 votes vote down vote up
public static Boolean getBooleanAttribute(final Context context, final AttributeSet attrs, final String namespace,
        final String name, final Boolean defValue) {
    final int resId = attrs.getAttributeResourceValue(namespace, name, Integer.MIN_VALUE);
    if (resId != Integer.MIN_VALUE) {
        return context.getResources().getBoolean(resId);
    }
    String str = attrs.getAttributeValue(namespace, name);
    return str != null ? Boolean.valueOf(str) : defValue;
}
 
Example 10
Source File: SkinAttrSupport.java    From ChangeSkin with Apache License 2.0 5 votes vote down vote up
public static List<SkinAttr> getSkinAttrs(AttributeSet attrs, Context context)
{
    List<SkinAttr> skinAttrs = new ArrayList<SkinAttr>();
    SkinAttr skinAttr = null;
    for (int i = 0; i < attrs.getAttributeCount(); i++)
    {
        String attrName = attrs.getAttributeName(i);
        String attrValue = attrs.getAttributeValue(i);

        SkinAttrType attrType = getSupprotAttrType(attrName);
        if (attrType == null) continue;

        if (attrValue.startsWith("@"))
        {
            int id = Integer.parseInt(attrValue.substring(1));
            String entryName = context.getResources().getResourceEntryName(id);

            L.e("entryName = " + entryName);
            if (entryName.startsWith(SkinConfig.ATTR_PREFIX))
            {
                skinAttr = new SkinAttr(attrType, entryName);
                skinAttrs.add(skinAttr);
            }
        }
    }
    return skinAttrs;

}
 
Example 11
Source File: FileBrowser.java    From AndroidWebServ with Apache License 2.0 5 votes vote down vote up
public FileBrowser(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);
    initFileBrowser(context);

    folderResId = attrs.getAttributeResourceValue(namespace, "folder_def", 0);
    folderParentResId = attrs
            .getAttributeResourceValue(namespace, "folder_parent", folderResId);
    fileResId = attrs.getAttributeResourceValue(namespace, "file_def", 0);
    String value = attrs.getAttributeValue(namespace, "display");
    if (value != null) {
        if (value.equals("file")) {
            display = DISPLAY_FILE;
        } else if (value.equals("folder")) {
            display = DISPLAY_FOLDER;
        }
    }
    isBackResp = attrs.getAttributeBooleanValue(namespace, "back_resp", false);
    isSort = attrs.getAttributeBooleanValue(namespace, "sort", true);

    // 动态扩展名属性
    int index = 1;
    while (true) {
        String extName = attrs.getAttributeValue(namespace, "ext_name" + index);
        int fileImageResId = attrs.getAttributeResourceValue(namespace, "ext_image" + index, 0);
        // 如果读取不到extName或fileImage属性时跳出循环
        if ("".equals(extName) || extName == null || fileImageResId == 0) {
            break;
        }
        extResIdMap.put(extName, fileImageResId);
        index++;
    }
}
 
Example 12
Source File: ELABORATE_FEED_REPORT.java    From stynico with MIT License 5 votes vote down vote up
public ELABORATE_FEED_REPORT(Context context, AttributeSet attrs) {
    super(context, attrs);
    setDialogLayoutResource(R.layout.preference_seekbar);

    for (int i = 0; i < attrs.getAttributeCount(); i++) {
        String attr = attrs.getAttributeName(i);
        if (attr.equalsIgnoreCase("pref_kind")) {
            prefKind = attrs.getAttributeValue(i);
            break;
        }
    }
}
 
Example 13
Source File: PackageParser.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
private static String parsePackageName(XmlPullParser parser,
        AttributeSet attrs, int flags, String[] outError)
        throws IOException, XmlPullParserException {

    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG
            && type != XmlPullParser.END_DOCUMENT) {
        ;
    }

    if (type != XmlPullParser.START_TAG) {
        outError[0] = "No start tag found";
        return null;
    }
    if (DEBUG_PARSER)
        Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
    if (!parser.getName().equals("manifest")) {
        outError[0] = "No <manifest> tag";
        return null;
    }
    String pkgName = attrs.getAttributeValue(null, "package");
    if (pkgName == null || pkgName.length() == 0) {
        outError[0] = "<manifest> does not specify package";
        return null;
    }
    String nameError = validateName(pkgName, true);
    if (nameError != null && !"android".equals(pkgName)) {
        outError[0] = "<manifest> specifies bad package name \""
            + pkgName + "\": " + nameError;
        return null;
    }

    return pkgName.intern();
}
 
Example 14
Source File: DynamicApkParser.java    From Android-plugin-support with MIT License 5 votes vote down vote up
private static String parsePackageNames(XmlPullParser parser, AttributeSet attrs)
        throws IOException, XmlPullParserException, DynamicApkParserException {
    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG
            && type != XmlPullParser.END_DOCUMENT) {
    }

    if (type != XmlPullParser.START_TAG) {
        throw new DynamicApkParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
                "No start tag found");
    }
    if (!parser.getName().equals("manifest")) {
        throw new DynamicApkParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
                "No <manifest> tag");
    }

    final String packageName = attrs.getAttributeValue(null, "package");
    if (!"android".equals(packageName)) {
        final String error = validateName(packageName, true, true);
        if (error != null) {
            throw new DynamicApkParserException(INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME,
                    "Invalid manifest package: " + error);
        }
    }

    return packageName.intern();
}
 
Example 15
Source File: SeekBarPreference.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
public SeekBarPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (attrs != null) {
        mMinimum = attrs.getAttributeIntValue(null, "minimum", 0);
        mMaximum = attrs.getAttributeIntValue(null, "maximum", 100);
        mInterval = attrs.getAttributeIntValue(null, "interval", 1);
        mDefaultValue = mMinimum;
        mMonitorBoxEnabled = attrs.getAttributeBooleanValue(null, "monitorBoxEnabled", false);
        mMonitorBoxUnit = attrs.getAttributeValue(null, "monitorBoxUnit");
    }

    mHandler = new Handler();
}
 
Example 16
Source File: SkinAppCompatViewInflater.java    From ReadMark with Apache License 2.0 5 votes vote down vote up
private View createViewFromTag(Context context, String name, AttributeSet attrs) {
    if (name.equals("view")) {
        name = attrs.getAttributeValue(null, "class");
    }

    try {
        mConstructorArgs[0] = context;
        mConstructorArgs[1] = attrs;

        if (-1 == name.indexOf('.')) {
            for (int i = 0; i < sClassPrefixList.length; i++) {
                final View view = createView(context, name, sClassPrefixList[i]);
                if (view != null) {
                    return view;
                }
            }
            return null;
        } else {
            return createView(context, name, null);
        }
    } catch (Exception e) {
        // We do not want to catch these, lets return null and let the actual LayoutInflater
        // try
        return null;
    } finally {
        // Don't retain references on context.
        mConstructorArgs[0] = null;
        mConstructorArgs[1] = null;
    }
}
 
Example 17
Source File: TimePickerPreference.java    From ploggy with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param context
 * @param attrs
 */
public TimePickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        defaultValue = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "defaultValue");
        initialize();
}
 
Example 18
Source File: CheckBox.java    From MaterialDesignLibrary with Apache License 2.0 4 votes vote down vote up
protected void setAttributes(AttributeSet attrs) {

		setBackgroundResource(R.drawable.background_checkbox);

		// Set size of view
		setMinimumHeight(Utils.dpToPx(48, getResources()));
		setMinimumWidth(Utils.dpToPx(48, 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
			// Color by hexadecimal
			int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
			if (background != -1)
				setBackgroundColor(background);
		}

		final boolean check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
				"check", false);
			post(new Runnable() {

				@Override
				public void run() {
					setChecked(check);
					setPressed(false);
					changeBackgroundColor(getResources().getColor(
							android.R.color.transparent));
				}
			});

		checkView = new Check(getContext());
        checkView.setId(View.generateViewId());
		RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
				getResources()), Utils.dpToPx(20, getResources()));
		params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		checkView.setLayoutParams(params);
		addView(checkView);

        // Adding text view to checkbox
        int textResource = attrs.getAttributeResourceValue(ANDROIDXML, "text", -1);
        String text = null;

        if(textResource != -1) {
            text = getResources().getString(textResource);
        } else {
            text = attrs.getAttributeValue(ANDROIDXML, "text");
        }

        if(text != null) {
            params.removeRule(RelativeLayout.CENTER_IN_PARENT);
            params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
                    TextView textView = new TextView(getContext());
            RelativeLayout.LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            textViewLayoutParams.addRule(RelativeLayout.RIGHT_OF, checkView.getId());
            textViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            textViewLayoutParams.setMargins(10, 0, 0, 0);
            textView.setLayoutParams(textViewLayoutParams);
            textView.setText(text);

            addView(textView);
        }
	}
 
Example 19
Source File: CheckBox.java    From MoeGallery with GNU General Public License v3.0 4 votes vote down vote up
protected void setAttributes(AttributeSet attrs) {

		setBackgroundResource(R.drawable.background_checkbox);

		// Set size of view
		setMinimumHeight(Utils.dpToPx(48, getResources()));
		setMinimumWidth(Utils.dpToPx(48, 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
			// Color by hexadecimal
			int background = attrs.getAttributeIntValue(ANDROIDXML, "background", -1);
			if (background != -1)
				setBackgroundColor(background);
		}

		final boolean check = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,
				"check", false);
			post(new Runnable() {

				@Override
				public void run() {
					setChecked(check);
					setPressed(false);
					changeBackgroundColor(getResources().getColor(
							android.R.color.transparent));
				}
			});

		checkView = new Check(getContext());
        checkView.setId(View.generateViewId());
		RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
				getResources()), Utils.dpToPx(20, getResources()));
		params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		checkView.setLayoutParams(params);
		addView(checkView);

        // Adding text view to checkbox
        int textResource = attrs.getAttributeResourceValue(ANDROIDXML, "text", -1);
        String text = null;

        if(textResource != -1) {
            text = getResources().getString(textResource);
        } else {
            text = attrs.getAttributeValue(ANDROIDXML, "text");
        }

        if(text != null) {
            params.removeRule(RelativeLayout.CENTER_IN_PARENT);
            params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
                    TextView textView = new TextView(getContext());
            RelativeLayout.LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            textViewLayoutParams.addRule(RelativeLayout.RIGHT_OF, checkView.getId());
            textViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            textViewLayoutParams.setMargins(10, 0, 0, 0);
            textView.setLayoutParams(textViewLayoutParams);
            textView.setText(text);

            addView(textView);
        }
	}
 
Example 20
Source File: FragmentActivity.java    From V.FlyoutTest with MIT License 4 votes vote down vote up
/**
 * Add support for inflating the &lt;fragment> tag.
 */
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    if (!"fragment".equals(name)) {
        return super.onCreateView(name, context, attrs);
    }
    
    String fname = attrs.getAttributeValue(null, "class");
    TypedArray a =  context.obtainStyledAttributes(attrs, FragmentTag.Fragment);
    if (fname == null) {
        fname = a.getString(FragmentTag.Fragment_name);
    }
    int id = a.getResourceId(FragmentTag.Fragment_id, View.NO_ID);
    String tag = a.getString(FragmentTag.Fragment_tag);
    a.recycle();

    if (!Fragment.isSupportFragmentClass(this, fname)) {
        // Invalid support lib fragment; let the device's framework handle it.
        // This will allow android.app.Fragments to do the right thing.
        return super.onCreateView(name, context, attrs);
    }
    
    View parent = null; // NOTE: no way to get parent pre-Honeycomb.
    int containerId = parent != null ? parent.getId() : 0;
    if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
        throw new IllegalArgumentException(attrs.getPositionDescription()
                + ": Must specify unique android:id, android:tag, or have a parent with an id for " + fname);
    }

    // If we restored from a previous state, we may already have
    // instantiated this fragment from the state and should use
    // that instance instead of making a new one.
    Fragment fragment = id != View.NO_ID ? mFragments.findFragmentById(id) : null;
    if (fragment == null && tag != null) {
        fragment = mFragments.findFragmentByTag(tag);
    }
    if (fragment == null && containerId != View.NO_ID) {
        fragment = mFragments.findFragmentById(containerId);
    }

    if (FragmentManagerImpl.DEBUG) Log.v(TAG, "onCreateView: id=0x"
            + Integer.toHexString(id) + " fname=" + fname
            + " existing=" + fragment);
    if (fragment == null) {
        fragment = Fragment.instantiate(this, fname);
        fragment.mFromLayout = true;
        fragment.mFragmentId = id != 0 ? id : containerId;
        fragment.mContainerId = containerId;
        fragment.mTag = tag;
        fragment.mInLayout = true;
        fragment.mFragmentManager = mFragments;
        fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
        mFragments.addFragment(fragment, true);

    } else if (fragment.mInLayout) {
        // A fragment already exists and it is not one we restored from
        // previous state.
        throw new IllegalArgumentException(attrs.getPositionDescription()
                + ": Duplicate id 0x" + Integer.toHexString(id)
                + ", tag " + tag + ", or parent id 0x" + Integer.toHexString(containerId)
                + " with another fragment for " + fname);
    } else {
        // This fragment was retained from a previous instance; get it
        // going now.
        fragment.mInLayout = true;
        // If this fragment is newly instantiated (either right now, or
        // from last saved state), then give it the attributes to
        // initialize itself.
        if (!fragment.mRetaining) {
            fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
        }
        mFragments.moveToState(fragment);
    }

    if (fragment.mView == null) {
        throw new IllegalStateException("Fragment " + fname
                + " did not create a view.");
    }
    if (id != 0) {
        fragment.mView.setId(id);
    }
    if (fragment.mView.getTag() == null) {
        fragment.mView.setTag(tag);
    }
    return fragment.mView;
}