Example usage for android.util AttributeSet getAttributeResourceValue

List of usage examples for android.util AttributeSet getAttributeResourceValue

Introduction

In this page you can find the example usage for android.util AttributeSet getAttributeResourceValue.

Prototype

public int getAttributeResourceValue(String namespace, String attribute, int defaultValue);

Source Link

Document

Return the value of 'attribute' as a resource identifier.

Usage

From source file:Main.java

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);
    }/*from w w  w.j a  v a2 s  .co  m*/
    String str = attrs.getAttributeValue(namespace, name);
    return str != null ? Boolean.valueOf(str) : defValue;
}

From source file:Main.java

public static int getIntAttribute(final Context context, final AttributeSet attrs, final String namespace,
        final String name, final int defValue) {
    final int resId = attrs.getAttributeResourceValue(namespace, name, Integer.MIN_VALUE);
    if (resId != Integer.MIN_VALUE) {
        final String string = context.getResources().getString(resId);
        try {//from  www . ja va2s .  co  m
            return Integer.parseInt(string);
        } catch (final NumberFormatException e) {
        }
    }
    return attrs.getAttributeIntValue(namespace, name, defValue);
}

From source file:Main.java

@Nullable
public static String getAttribute(@NonNull Context context, @NonNull AttributeSet attrs, @NonNull String name,
        @Nullable String defaultValue) {
    int resId = attrs.getAttributeResourceValue(ISORON_NAMESPACE, name, 0);
    if (resId != 0)
        return context.getResources().getString(resId);

    String value = attrs.getAttributeValue(ISORON_NAMESPACE, name);
    if (value != null)
        return value;
    else//ww w  .ja  va 2  s .  c o  m
        return defaultValue;
}

From source file:Main.java

/**
 * Tries to pull the Custom Attribute directly from the TextView.
 *
 * @param context     Activity Context/* w w w .  java 2s. c  o  m*/
 * @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 == -1)
        return null;

    final String attributeName;
    try {
        attributeName = context.getResources().getResourceEntryName(attributeId);
    } 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);
}

From source file:Main.java

/**
 * Tries to pull the Custom Attribute directly from the TextView.
 *
 * @param context     Activity Context// w  w  w  . j a  va  2  s .c o  m
 * @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 == -1 || attrs == null)
        return null;

    final String attributeName;
    try {
        attributeName = context.getResources().getResourceEntryName(attributeId);
    } 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);
}

From source file:Main.java

/**
 * Tries to pull the Custom Attribute directly from the TextView.
 *
 * @param context     Activity Context/*from   w w w.j ava  2  s . co  m*/
 * @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);
}

From source file:com.anysoftkeyboard.addons.AddOnsFactory.java

@Nullable
protected static CharSequence getTextFromResourceOrText(Context context, AttributeSet attrs,
        String attributeName) {//from   w  w  w. j  av  a 2 s .c o m
    final int stringResId = attrs.getAttributeResourceValue(null, attributeName, AddOn.INVALID_RES_ID);
    if (stringResId != AddOn.INVALID_RES_ID) {
        return context.getResources().getText(stringResId);
    } else {
        return attrs.getAttributeValue(null, attributeName);
    }
}

From source file:com.anysoftkeyboard.theme.KeyboardThemeFactory.java

@Override
protected KeyboardTheme createConcreteAddOn(Context askContext, Context context, String prefId, int nameResId,
        String description, boolean isHidden, int sortIndex, AttributeSet attrs) {
    final int keyboardThemeResId = attrs.getAttributeResourceValue(null, XML_KEYBOARD_THEME_RES_ID_ATTRIBUTE,
            0);/*from   w  w  w .  ja va  2s .c om*/
    final int popupKeyboardThemeResId = attrs.getAttributeResourceValue(null,
            XML_POPUP_KEYBOARD_THEME_RES_ID_ATTRIBUTE, 0);
    final int iconsThemeResId = attrs.getAttributeResourceValue(null, XML_KEYBOARD_ICONS_THEME_RES_ID_ATTRIBUTE,
            0);
    final int popupKeyboardIconThemeResId = attrs.getAttributeResourceValue(null,
            XML_POPUP_KEYBOARD_ICONS_THEME_RES_ID_ATTRIBUTE, 0);

    if (keyboardThemeResId == -1) {
        String detailMessage = String.format(Locale.US,
                "Missing details for creating Keyboard theme! prefId %s, keyboardThemeResId: %d", prefId,
                keyboardThemeResId);

        throw new RuntimeException(detailMessage);
    }
    return new KeyboardTheme(askContext, context, prefId, nameResId, keyboardThemeResId,
            popupKeyboardThemeResId, iconsThemeResId, popupKeyboardIconThemeResId, isHidden, description,
            sortIndex);
}

From source file:com.anysoftkeyboard.keyboards.KeyboardFactory.java

@Override
protected KeyboardAddOnAndBuilder createConcreteAddOn(Context askContext, Context context, String prefId,
        int nameId, String description, boolean isHidden, int sortIndex, AttributeSet attrs) {
    final int layoutResId = attrs.getAttributeResourceValue(null, XML_LAYOUT_RES_ID_ATTRIBUTE,
            AddOn.INVALID_RES_ID);//from  w  w  w . j a  v  a 2 s .  c o  m
    final int landscapeLayoutResId = attrs.getAttributeResourceValue(null,
            XML_LANDSCAPE_LAYOUT_RES_ID_ATTRIBUTE, AddOn.INVALID_RES_ID);
    final int iconResId = attrs.getAttributeResourceValue(null, XML_ICON_RES_ID_ATTRIBUTE,
            R.drawable.sym_keyboard_notification_icon);
    final String defaultDictionary = attrs.getAttributeValue(null, XML_DICTIONARY_NAME_ATTRIBUTE);
    final String additionalIsLetterExceptions = attrs.getAttributeValue(null,
            XML_ADDITIONAL_IS_LETTER_EXCEPTIONS_ATTRIBUTE);
    String sentenceSeparators = attrs.getAttributeValue(null, XML_SENTENCE_SEPARATOR_CHARACTERS_ATTRIBUTE);
    if (TextUtils.isEmpty(sentenceSeparators))
        sentenceSeparators = DEFAULT_SENTENCE_SEPARATORS;
    final int physicalTranslationResId = attrs.getAttributeResourceValue(null,
            XML_PHYSICAL_TRANSLATION_RES_ID_ATTRIBUTE, AddOn.INVALID_RES_ID);
    // A keyboard is enabled by default if it is the first one (index==1)
    final boolean keyboardDefault = attrs.getAttributeBooleanValue(null, XML_DEFAULT_ATTRIBUTE, sortIndex == 1);

    // asserting
    if ((prefId == null) || (nameId == AddOn.INVALID_RES_ID) || (layoutResId == AddOn.INVALID_RES_ID)) {
        Logger.e(TAG, "External Keyboard does not include all mandatory details! Will not create keyboard.");
        return null;
    } else {
        if (BuildConfig.DEBUG) {
            Logger.d(TAG,
                    "External keyboard details: prefId:" + prefId + " nameId:" + nameId + " resId:"
                            + layoutResId + " landscapeResId:" + landscapeLayoutResId + " iconResId:"
                            + iconResId + " defaultDictionary:" + defaultDictionary);
        }
        return new KeyboardAddOnAndBuilder(askContext, context, prefId, nameId, layoutResId,
                landscapeLayoutResId, defaultDictionary, iconResId, physicalTranslationResId,
                additionalIsLetterExceptions, sentenceSeparators, description, isHidden, sortIndex,
                keyboardDefault);
    }
}

From source file:com.yek.keyboard.keyboardextensions.KeyboardExtensionFactory.java

@Override
protected KeyboardExtension createConcreteAddOn(Context askContext, Context context, String prefId,
        int nameResId, String description, boolean isHidden, int sortIndex, AttributeSet attrs) {
    int keyboardResId = attrs.getAttributeResourceValue(null, XML_EXT_KEYBOARD_RES_ID_ATTRIBUTE,
            AddOn.INVALID_RES_ID);//  w w  w  . j a va2 s  . c o  m
    if (keyboardResId == AddOn.INVALID_RES_ID)
        keyboardResId = attrs.getAttributeIntValue(null, XML_EXT_KEYBOARD_RES_ID_ATTRIBUTE,
                AddOn.INVALID_RES_ID);
    @KeyboardExtension.KeyboardExtensionType
    int extensionType = attrs.getAttributeResourceValue(null, XML_EXT_KEYBOARD_TYPE_ATTRIBUTE,
            AddOn.INVALID_RES_ID);
    if (extensionType != AddOn.INVALID_RES_ID) {
        extensionType = KeyboardExtension.ensureValidType(context.getResources().getInteger(extensionType));
    } else {
        extensionType = attrs.getAttributeIntValue(null, XML_EXT_KEYBOARD_TYPE_ATTRIBUTE, AddOn.INVALID_RES_ID);
    }
    Logger.d(TAG, "Parsing Extension Keyboard! prefId %s, keyboardResId %d, type %d", prefId, keyboardResId,
            extensionType);

    if (extensionType == AddOn.INVALID_RES_ID) {
        throw new RuntimeException(String.format(Locale.US,
                "Missing details for creating Extension Keyboard! prefId %s\nkeyboardResId: %d, type: %d",
                prefId, keyboardResId, extensionType));
    } else {
        return new KeyboardExtension(askContext, context, prefId, nameResId, keyboardResId, extensionType,
                description, isHidden, sortIndex);
    }
}