Example usage for android.content.res TypedArray hasValue

List of usage examples for android.content.res TypedArray hasValue

Introduction

In this page you can find the example usage for android.content.res TypedArray hasValue.

Prototype

public boolean hasValue(@StyleableRes int index) 

Source Link

Document

Determines whether there is an attribute at index.

Usage

From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java

/**
 * Fill in this object based on the contents of an XML "selector" element.
 *//*from   w w  w .  jav a2 s.com*/
private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
        @NonNull AttributeSet attrs, @Nullable Resources.Theme theme)
        throws XmlPullParserException, IOException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    int defaultColor = DEFAULT_COLOR;

    int[][] stateSpecList = new int[20][];
    int[] colorList = new int[stateSpecList.length];
    int listSize = 0;

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem);
        final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA);

        float alphaMod = 1.0f;
        if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod);
        } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod);
        }

        a.recycle();

        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha
                    && stateResId != R.attr.alpha) {
                // Unrecognized attribute, add to state set
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);

        // Apply alpha modulation. If we couldn't resolve the color or
        // alpha yet, the default values leave us enough information to
        // modulate again during applyTheme().
        final int color = modulateColorAlpha(baseColor, alphaMod);
        if (listSize == 0 || stateSpec.length == 0) {
            defaultColor = color;
        }

        colorList = GrowingArrayUtils.append(colorList, listSize, color);
        stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec);
        listSize++;
    }

    int[] colors = new int[listSize];
    int[][] stateSpecs = new int[listSize][];
    System.arraycopy(colorList, 0, colors, 0, listSize);
    System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize);

    return new ColorStateList(stateSpecs, colors);
}

From source file:com.bilibili.magicasakura.utils.ThemeUtils.java

public static boolean hasThemeAttr(Context context, @AttrRes int attr) {
    TEMP_ARRAY[0] = attr;/*from ww  w.j a  v  a 2s.co  m*/
    TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY);
    try {
        return a.hasValue(0);
    } finally {
        a.recycle();
    }
}

From source file:com.appeaser.sublimepickerlibrary.utilities.SUtils.java

public static void initializeResources(Context context) {
    TypedArray a = context.obtainStyledAttributes(new int[] { R.attr.colorAccent, R.attr.colorControlHighlight,
            R.attr.colorControlActivated, R.attr.colorButtonNormal, android.R.attr.textColorPrimary,
            android.R.attr.textColorPrimaryInverse, R.attr.colorPrimary, R.attr.colorPrimaryDark,
            android.R.attr.textColorSecondary, android.R.attr.colorBackground,
            android.R.attr.textColorSecondaryInverse });

    if (a.hasValue(0))
        COLOR_ACCENT = a.getColor(0, Color.TRANSPARENT);

    if (a.hasValue(1))
        COLOR_CONTROL_HIGHLIGHT = a.getColor(1, Color.TRANSPARENT);

    if (a.hasValue(2))
        COLOR_CONTROL_ACTIVATED = a.getColor(2, Color.TRANSPARENT);

    if (a.hasValue(3))
        COLOR_BUTTON_NORMAL = a.getColor(3, Color.TRANSPARENT);

    if (a.hasValue(4))
        COLOR_TEXT_PRIMARY = a.getColor(4, Color.TRANSPARENT);

    if (a.hasValue(5))
        COLOR_TEXT_PRIMARY_INVERSE = a.getColor(5, Color.TRANSPARENT);

    if (a.hasValue(6))
        COLOR_PRIMARY = a.getColor(6, Color.TRANSPARENT);

    if (a.hasValue(7))
        COLOR_PRIMARY_DARK = a.getColor(7, Color.TRANSPARENT);

    if (a.hasValue(8))
        COLOR_TEXT_SECONDARY = a.getColor(8, Color.TRANSPARENT);

    if (a.hasValue(9))
        COLOR_BACKGROUND = a.getColor(9, Color.TRANSPARENT);

    if (a.hasValue(10))
        COLOR_TEXT_SECONDARY_INVERSE = a.getColor(10, Color.TRANSPARENT);

    a.recycle();/*  www  .ja  v a 2s.  c  om*/

    CORNER_RADIUS = context.getResources().getDimensionPixelSize(R.dimen.control_corner_material);

    if (Config.DEBUG) {
        Log.i(TAG, "COLOR_ACCENT: " + Integer.toHexString(COLOR_ACCENT));
        Log.i(TAG, "COLOR_CONTROL_HIGHLIGHT: " + Integer.toHexString(COLOR_CONTROL_HIGHLIGHT));
        Log.i(TAG, "COLOR_CONTROL_ACTIVATED: " + Integer.toHexString(COLOR_CONTROL_ACTIVATED));
        Log.i(TAG, "COLOR_BUTTON_NORMAL: " + Integer.toHexString(COLOR_BUTTON_NORMAL));
        Log.i(TAG, "COLOR_TEXT_PRIMARY: " + Integer.toHexString(COLOR_TEXT_PRIMARY));
        Log.i(TAG, "COLOR_TEXT_PRIMARY_INVERSE: " + Integer.toHexString(COLOR_TEXT_PRIMARY_INVERSE));
        Log.i(TAG, "COLOR_PRIMARY: " + Integer.toHexString(COLOR_PRIMARY));
        Log.i(TAG, "COLOR_PRIMARY_DARK: " + Integer.toHexString(COLOR_PRIMARY_DARK));
        Log.i(TAG, "COLOR_TEXT_SECONDARY: " + Integer.toHexString(COLOR_TEXT_SECONDARY));
        Log.i(TAG, "COLOR_BACKGROUND: " + Integer.toHexString(COLOR_BACKGROUND));
        Log.i(TAG, "COLOR_TEXT_SECONDARY_INVERSE: " + Integer.toHexString(COLOR_TEXT_SECONDARY_INVERSE));
    }
}

From source file:com.google.samples.apps.iosched.ui.widget.NavDrawerItemView.java

public NavDrawerItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(HORIZONTAL);/*  w ww .  java 2 s .c om*/
    LayoutInflater.from(context).inflate(R.layout.navdrawer_item_view, this, true);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavDrawerItemView);
    if (a.hasValue(R.styleable.NavDrawerItemView_iconTints)) {
        mIconTints = a.getColorStateList(R.styleable.NavDrawerItemView_iconTints);
    }
}

From source file:io.plaidapp.ui.widget.CutoutTextView.java

public CutoutTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

    final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CutoutTextView, 0, 0);
    if (a.hasValue(R.styleable.CutoutTextView_android_fontFamily)) {
        try {/*  w ww.  j a  v  a  2 s  . co  m*/
            Typeface font = ResourcesCompat.getFont(getContext(),
                    a.getResourceId(R.styleable.CutoutTextView_android_fontFamily, 0));
            if (font != null) {
                textPaint.setTypeface(font);
            }
        } catch (Resources.NotFoundException nfe) {
        }
    }
    if (a.hasValue(R.styleable.CutoutTextView_foregroundColor)) {
        foregroundColor = a.getColor(R.styleable.CutoutTextView_foregroundColor, foregroundColor);
    }
    if (a.hasValue(R.styleable.CutoutTextView_android_text)) {
        text = a.getString(R.styleable.CutoutTextView_android_text);
    }
    maxTextSize = context.getResources().getDimensionPixelSize(R.dimen.display_4_text_size);
    a.recycle();
}

From source file:com.stepstone.stepper.internal.ColorableProgressBar.java

public ColorableProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mProgressColor = ContextCompat.getColor(context, R.color.ms_selectedColor);
    mProgressBackgroundColor = ContextCompat.getColor(context, R.color.ms_unselectedColor);
    super.setProgressDrawable(ContextCompat.getDrawable(context, R.drawable.ms_colorable_progress_bar));

    if (attrs != null) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ColorableProgressBar,
                defStyleAttr, 0);/*w  ww  .jav a  2s.c o  m*/

        if (a.hasValue(R.styleable.ColorableProgressBar_ms_progressPrimaryColor)) {
            mProgressColor = a.getColor(R.styleable.ColorableProgressBar_ms_progressPrimaryColor,
                    mProgressColor);
        }
        if (a.hasValue(R.styleable.ColorableProgressBar_ms_progressBackgroundColor)) {
            mProgressBackgroundColor = a.getColor(R.styleable.ColorableProgressBar_ms_progressBackgroundColor,
                    mProgressBackgroundColor);
        }

        a.recycle();
    }
    updateProgressDrawable();
}

From source file:android.support.v7.app.ActionBarActivityDelegate.java

void onCreate(Bundle savedInstanceState) {
    TypedArray a = mActivity.obtainStyledAttributes(R.styleable.ActionBarWindow);

    if (!a.hasValue(R.styleable.ActionBarWindow_windowActionBar)) {
        a.recycle();// www.j av a2 s. c o m
        throw new IllegalStateException(
                "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
    }

    mHasActionBar = a.getBoolean(R.styleable.ActionBarWindow_windowActionBar, false);
    mOverlayActionBar = a.getBoolean(R.styleable.ActionBarWindow_windowActionBarOverlay, false);
    a.recycle();
}

From source file:de.vanita5.twittnuker.util.ThemeUtils.java

public static Context getActionBarContext(final Context context) {
    final TypedArray a = context.obtainStyledAttributes(
            new int[] { android.R.attr.actionBarTheme, android.R.attr.actionBarWidgetTheme });
    final int resId;
    try {// w  w w  . j a  va2s .  c o m
        resId = a.hasValue(0) ? a.getResourceId(0, 0) : a.getResourceId(1, 0);
    } finally {
        a.recycle();
    }
    if (resId == 0)
        return context;
    return new ContextThemeWrapper(context, resId);
}

From source file:org.opensilk.common.ui.widget.TintImageButton.java

public TintImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    if (attrs == null)
        return;//from   w w w.ja  v  a 2s .  c o m

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintImageButton, defStyleAttr, 0);

    if (a == null)
        return;

    if (a.hasValue(R.styleable.TintImageButton_tibTint)) {
        final int color = a.getColor(R.styleable.TintImageButton_tibTint, 0);
        if (color != 0) {
            // XXX From AOSP see TintManager

            // First, lets see if the cache already contains the color filter
            PorterDuffColorFilter filter = COLOR_FILTER_CACHE.get(color, TINT_MODE);

            if (filter == null) {
                // Cache miss, so create a color filter and add it to the cache
                filter = new PorterDuffColorFilter(color, TINT_MODE);
                COLOR_FILTER_CACHE.put(color, TINT_MODE, filter);
            }

            // Finally set the color filter
            getDrawable().setColorFilter(filter);
        }
    }

    a.recycle();
}

From source file:cat.ereza.customactivityoncrash.activity.DefaultErrorReportActivity.java

@SuppressLint("PrivateResource")
@Override// w w w .j a v  a 2s.  com
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //This is needed to avoid a crash if the developer has not specified
    //an app-level theme that extends Theme.AppCompat
    TypedArray a = obtainStyledAttributes(R.styleable.AppCompatTheme);
    if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
        setTheme(R.style.Theme_AppCompat_Light_DarkActionBar);
    }
    a.recycle();

    setContentView(R.layout.customactivityoncrash_default_error_activity);

    //Close/restart button logic:
    //If a class if set, use restart.
    //Else, use close and just finish the app.
    //It is recommended that you follow this logic if implementing a custom error activity.
    Button restartButton = (Button) findViewById(R.id.customactivityoncrash_error_activity_restart_button);

    final CaocConfig config = CustomActivityOnCrash.getConfigFromIntent(getIntent());

    if (config.isShowRestartButton() && config.getRestartActivityClass() != null) {
        restartButton.setText(R.string.customactivityoncrash_error_activity_restart_app);
        restartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomActivityOnCrash.restartApplication(DefaultErrorReportActivity.this, config);
            }
        });
    } else {
        restartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomActivityOnCrash.closeApplication(DefaultErrorReportActivity.this, config);
            }
        });
    }

    Button moreInfoButton = (Button) findViewById(R.id.customactivityoncrash_error_activity_more_info_button);

    if (config.isShowErrorDetails()) {
        moreInfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //We retrieve all the error data and show it

                AlertDialog dialog = new AlertDialog.Builder(DefaultErrorReportActivity.this)
                        .setTitle(R.string.customactivityoncrash_error_activity_error_details_title)
                        .setMessage(CustomActivityOnCrash
                                .getAllErrorDetailsFromIntent(DefaultErrorReportActivity.this, getIntent()))
                        .setPositiveButton(R.string.customactivityoncrash_error_activity_error_details_close,
                                null)
                        .setNeutralButton(R.string.customactivityoncrash_error_activity_error_details_copy,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        copyErrorToClipboard();
                                        Toast.makeText(DefaultErrorReportActivity.this,
                                                R.string.customactivityoncrash_error_activity_error_details_copied,
                                                Toast.LENGTH_SHORT).show();
                                    }
                                })
                        .show();
                TextView textView = (TextView) dialog.findViewById(android.R.id.message);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
                        .getDimension(R.dimen.customactivityoncrash_error_activity_error_details_text_size));
            }
        });
    } else {
        moreInfoButton.setVisibility(View.GONE);
    }

    Integer defaultErrorActivityDrawableId = config.getErrorDrawable();
    ImageView errorImageView = ((ImageView) findViewById(R.id.customactivityoncrash_error_activity_image));

    if (defaultErrorActivityDrawableId != null) {
        errorImageView.setImageDrawable(
                ResourcesCompat.getDrawable(getResources(), defaultErrorActivityDrawableId, getTheme()));
    }
}