Example usage for android.content Context obtainStyledAttributes

List of usage examples for android.content Context obtainStyledAttributes

Introduction

In this page you can find the example usage for android.content Context obtainStyledAttributes.

Prototype

public final TypedArray obtainStyledAttributes(AttributeSet set, @StyleableRes int[] attrs,
        @AttrRes int defStyleAttr, @StyleRes int defStyleRes) 

Source Link

Document

Retrieve styled attribute information in this Context's theme.

Usage

From source file:android.support.design.widget.AppBarLayout.java

public AppBarLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOrientation(VERTICAL);//from www. ja va 2  s.c o m

    ThemeUtils.checkAppCompatTheme(context);

    if (Build.VERSION.SDK_INT >= 21) {
        // Use the bounds view outline provider so that we cast a shadow, even without a
        // background
        ViewUtilsLollipop.setBoundsViewOutlineProvider(this);

        // If we're running on API 21+, we should reset any state list animator from our
        // default style
        ViewUtilsLollipop.setStateListAnimatorFromAttrs(this, attrs, 0, R.style.Widget_Design_AppBarLayout);
    }

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppBarLayout, 0,
            R.style.Widget_Design_AppBarLayout);
    ViewCompat.setBackground(this, a.getDrawable(R.styleable.AppBarLayout_android_background));
    if (a.hasValue(R.styleable.AppBarLayout_expanded)) {
        setExpanded(a.getBoolean(R.styleable.AppBarLayout_expanded, false), false, false);
    }
    if (Build.VERSION.SDK_INT >= 21 && a.hasValue(R.styleable.AppBarLayout_elevation)) {
        ViewUtilsLollipop.setDefaultAppBarLayoutStateListAnimator(this,
                a.getDimensionPixelSize(R.styleable.AppBarLayout_elevation, 0));
    }
    a.recycle();

    ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            return onWindowInsetChanged(insets);
        }
    });
}

From source file:com.albedinsky.android.support.ui.widget.BaseProgressBar.java

/**
 * Creates a new instance of BaseProgressBar within the given <var>context</var>.
 *
 * @param context      Context in which will be this view presented.
 * @param attrs        Set of Xml attributes used to configure the new instance of this view.
 * @param defStyleAttr An attribute which contains a reference to a default style resource for
 *                     this view within a theme of the given context.
 *///from www. j  av a 2  s  .co m
public BaseProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.mResources = context.getResources();
    this.mUiThreadId = Thread.currentThread().getId();

    onAttachDrawable();
    if (mDrawable == null) {
        throw new IllegalArgumentException("No progress drawable has been attached.");
    }

    /**
     * Process attributes.
     */
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Ui_Widget_ProgressBar,
            defStyleAttr, 0);
    if (typedArray != null) {
        this.processTintValues(context, typedArray);

        final int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            final int index = typedArray.getIndex(i);
            if (index == R.styleable.Ui_Widget_ProgressBar_uiColorProgress) {
                mDrawable.setColor(typedArray.getColor(index, mDrawable.getColor()));
            } else if (index == R.styleable.Ui_Widget_ProgressBar_uiColorsProgress) {
                final int colorsResId = typedArray.getResourceId(index, -1);
                if (colorsResId > 0) {
                    mDrawable.setColors(mResources.getIntArray(colorsResId));
                }
            } else if (index == R.styleable.Ui_Widget_ProgressBar_uiMultiColored) {
                mDrawable.setMultiColored(typedArray.getBoolean(index, mDrawable.isMultiColored()));
            } else if (index == R.styleable.Ui_Widget_ProgressBar_uiColorProgressBackground) {
                mDrawable.setBackgroundColor(typedArray.getInt(index, Color.TRANSPARENT));
            } else if (index == R.styleable.Ui_Widget_ProgressBar_android_thickness) {
                mDrawable.setThickness(typedArray.getDimensionPixelSize(index, 0));
            } else if (index == R.styleable.Ui_Widget_ProgressBar_uiRounded) {
                mDrawable.setRounded(typedArray.getBoolean(index, false));
            } else if (index == R.styleable.Ui_Widget_ProgressBar_uiIndeterminateSpeed) {
                mDrawable.setIndeterminateSpeed(typedArray.getFloat(index, 1));
            }
        }
    }

    this.applyProgressTint();
    this.applyIndeterminateTint();
    this.applyProgressBackgroundTint();
}

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * Create a new, empty popup window capable of displaying items from a ListAdapter.
 * Backgrounds should be set using {@link #setBackgroundDrawable(Drawable)}.
 *
 * @param context Context used for contained views.
 * @param attrs Attributes from inflating parent views used to style the popup.
 * @param defStyleAttr Style attribute to read for default styling of popup content.
 * @param defStyleRes Style resource ID to use for default styling of popup content.
 *///ww w . j av  a 2  s.  c  om
public AbstractXpListPopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    mContext = context;
    mHandler = new Handler(context.getMainLooper());

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListPopupWindow, defStyleAttr,
            defStyleRes);
    mDropDownHorizontalOffset = a
            .getDimensionPixelOffset(R.styleable.ListPopupWindow_android_dropDownHorizontalOffset, 0);
    mDropDownVerticalOffset = a
            .getDimensionPixelOffset(R.styleable.ListPopupWindow_android_dropDownVerticalOffset, 0);
    if (mDropDownVerticalOffset != 0) {
        mDropDownVerticalOffsetSet = true;
    }
    a.recycle();

    int defaultMargin = Util.dpToPxOffset(context, 8);
    final TypedArray b = context.obtainStyledAttributes(attrs, R.styleable.XpListPopupWindow, defStyleAttr,
            defStyleRes);
    if (b.hasValue(R.styleable.XpListPopupWindow_android_layout_margin)) {
        int margin = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_margin,
                defaultMargin);
        mMargins.bottom = margin;
        mMargins.top = margin;
        mMargins.left = margin;
        mMargins.right = margin;
    } else {
        if (API_18 && b.hasValue(R.styleable.XpListPopupWindow_android_layout_marginEnd)) {
            int margin = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_marginEnd, 0);
            if (mLayoutDirection == LayoutDirection.RTL) {
                mMargins.left = margin;
            } else {
                mMargins.right = margin;
            }
        } else {
            mMargins.right = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_marginRight,
                    defaultMargin);
        }
        if (API_18 && b.hasValue(R.styleable.XpListPopupWindow_android_layout_marginStart)) {
            int margin = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_marginStart, 0);
            if (mLayoutDirection == LayoutDirection.RTL) {
                mMargins.right = margin;
            } else {
                mMargins.left = margin;
            }
        } else {
            mMargins.left = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_marginLeft,
                    defaultMargin);
        }
        mMargins.top = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_marginTop,
                defaultMargin);
        mMargins.bottom = b.getDimensionPixelOffset(R.styleable.XpListPopupWindow_android_layout_marginBottom,
                defaultMargin);
    }
    b.recycle();

    mPopup = new AppCompatPopupWindow(context, attrs, defStyleAttr);
    mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

    // Set the default layout direction to match the default locale one
    final Locale locale = mContext.getResources().getConfiguration().locale;
    mLayoutDirection = TextUtilsCompat.getLayoutDirectionFromLocale(locale);

    setAllowScrollingAnchorParent(false);
}

From source file:com.albedinsky.android.ui.widget.BaseProgressBar.java

/**
 * Called from one of constructors of this view to perform its initialization.
 * <p>//from w  ww.ja v  a2  s. co  m
 * Initialization is done via parsing of the specified <var>attrs</var> set and obtaining for
 * this view specific data from it that can be used to configure this new view instance. The
 * specified <var>defStyleAttr</var> and <var>defStyleRes</var> are used to obtain default data
 * from the current theme provided by the specified <var>context</var>.
 */
@SuppressLint("NewApi")
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    this.mUiThreadId = Thread.currentThread().getId();
    // Use software layer that is required for proper drawing work of progress drawables.
    if (ProgressDrawable.REQUIRES_SOFTWARE_LAYER) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    onAttachDrawable();
    if (mDrawable == null) {
        throw new IllegalArgumentException("No progress drawable has been attached.");
    }
    /**
     * Process attributes.
     */
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Ui_ProgressBar,
            defStyleAttr, defStyleRes);
    if (typedArray != null) {
        this.processTintValues(context, typedArray);
        final int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            final int index = typedArray.getIndex(i);
            if (index == R.styleable.Ui_ProgressBar_android_max) {
                setMax(typedArray.getInt(index, getMax()));
            } else if (index == R.styleable.Ui_ProgressBar_android_progress) {
                setProgress(typedArray.getInt(index, mProgress));
            } else if (index == R.styleable.Ui_ProgressBar_uiColorProgress) {
                mDrawable.setColor(typedArray.getColor(index, mDrawable.getColor()));
            } else if (index == R.styleable.Ui_ProgressBar_uiColorsProgress) {
                final int colorsResId = typedArray.getResourceId(index, -1);
                if (colorsResId > 0 && !isInEditMode()) {
                    mDrawable.setColors(context.getResources().getIntArray(colorsResId));
                }
            } else if (index == R.styleable.Ui_ProgressBar_uiMultiColored) {
                mDrawable.setMultiColored(typedArray.getBoolean(index, mDrawable.isMultiColored()));
            } else if (index == R.styleable.Ui_ProgressBar_uiColorProgressBackground) {
                mDrawable.setBackgroundColor(typedArray.getInt(index, Color.TRANSPARENT));
            } else if (index == R.styleable.Ui_ProgressBar_android_thickness) {
                mDrawable.setThickness(typedArray.getDimensionPixelSize(index, 0));
            } else if (index == R.styleable.Ui_ProgressBar_uiRounded) {
                mDrawable.setRounded(!isInEditMode() && typedArray.getBoolean(index, mDrawable.isRounded()));
            } else if (index == R.styleable.Ui_ProgressBar_uiIndeterminateSpeed) {
                mDrawable.setIndeterminateSpeed(typedArray.getFloat(index, 1));
            }
        }
    }
    mDrawable.setInEditMode(isInEditMode());

    this.applyProgressTint();
    this.applyIndeterminateTint();
    this.applyProgressBackgroundTint();
}

From source file:android.support.v7ox.widget.AppCompatSpinner.java

/**
 * Constructs a new spinner with the given context's theme, the supplied
 * attribute set, default styles, popup mode (one of {@link #MODE_DIALOG}
 * or {@link #MODE_DROPDOWN}), and the context against which the popup
 * should be inflated.//from   ww w . j a  va  2s  . c o m
 *
 * @param context      The context against which the view is inflated, which
 *                     provides access to the current theme, resources, etc.
 * @param attrs        The attributes of the XML tag that is inflating the view.
 * @param defStyleAttr An attribute in the current theme that contains a
 *                     reference to a style resource that supplies default
 *                     values for the view. Can be 0 to not look for
 *                     defaults.
 * @param mode         Constant describing how the user will select choices from
 *                     the spinner.
 * @param popupTheme   The theme against which the dialog or dropdown popup
 *                     should be inflated. May be {@code null} to use the
 *                     view theme. If set, this will override any value
 *                     specified by
 *                     {@link R.styleable#Spinner_popupTheme}.
 * @see #MODE_DIALOG
 * @see #MODE_DROPDOWN
 */
public AppCompatSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode,
        Resources.Theme popupTheme) {
    super(context, attrs, defStyleAttr);

    TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.Spinner, defStyleAttr,
            0);

    mDrawableManager = AppCompatDrawableManager.get();
    mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mDrawableManager);

    if (popupTheme != null) {
        mPopupContext = new ContextThemeWrapper(context, popupTheme);
    } else {
        final int popupThemeResId = a.getResourceId(R.styleable.Spinner_popupTheme, 0);
        if (popupThemeResId != 0) {
            mPopupContext = new ContextThemeWrapper(context, popupThemeResId);
        } else {
            // If we're running on a < M device, we'll use the current context and still handle
            // any dropdown popup
            mPopupContext = !IS_AT_LEAST_M ? context : null;
        }
    }

    if (mPopupContext != null) {
        if (mode == MODE_THEME) {
            if (Build.VERSION.SDK_INT >= 11) {
                // If we're running on API v11+ we will try and read android:spinnerMode
                TypedArray aa = null;
                try {
                    aa = context.obtainStyledAttributes(attrs, ATTRS_ANDROID_SPINNERMODE, defStyleAttr, 0);
                    if (aa.hasValue(0)) {
                        mode = aa.getInt(0, MODE_DIALOG);
                    }
                } catch (Exception e) {
                    Log.i(TAG, "Could not read android:spinnerMode", e);
                } finally {
                    if (aa != null) {
                        aa.recycle();
                    }
                }
            } else {
                // Else, we use a default mode of dropdown
                mode = MODE_DROPDOWN;
            }
        }

        if (mode == MODE_DROPDOWN) {
            final DropdownPopup popup = new DropdownPopup(mPopupContext, attrs, defStyleAttr);
            final TintTypedArray pa = TintTypedArray.obtainStyledAttributes(mPopupContext, attrs,
                    R.styleable.Spinner, defStyleAttr, 0);
            mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_android_dropDownWidth,
                    LayoutParams.WRAP_CONTENT);

            popup.setPromptText(a.getString(R.styleable.Spinner_android_prompt));
            pa.recycle();

            mPopup = popup;
            mForwardingListener = new ListPopupWindow.ForwardingListener(this) {
                @Override
                public ListPopupWindow getPopup() {
                    return popup;
                }

                @Override
                public boolean onForwardingStarted() {
                    if (!mPopup.isShowing()) {
                        mPopup.show();
                    }
                    return true;
                }
            };
        }
    }

    final CharSequence[] entries = a.getTextArray(R.styleable.Spinner_android_entries);
    if (entries != null) {
        final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context,
                R.layout.support_simple_spinner_dropdown_item, entries);
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        setAdapter(adapter);
    }

    a.recycle();

    mPopupSet = true;

    // Base constructors can call setAdapter before we initialize mPopup.
    // Finish setting things up if this happened.
    if (mTempAdapter != null) {
        setAdapter(mTempAdapter);
        mTempAdapter = null;
    }

    mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
}

From source file:android.support.v7.widget.AppCompatSpinner.java

/**
 * Constructs a new spinner with the given context's theme, the supplied
 * attribute set, default styles, popup mode (one of {@link #MODE_DIALOG}
 * or {@link #MODE_DROPDOWN}), and the context against which the popup
 * should be inflated./*from   www  .ja v  a 2s  .  c o m*/
 *
 * @param context      The context against which the view is inflated, which
 *                     provides access to the current theme, resources, etc.
 * @param attrs        The attributes of the XML tag that is inflating the view.
 * @param defStyleAttr An attribute in the current theme that contains a
 *                     reference to a style resource that supplies default
 *                     values for the view. Can be 0 to not look for
 *                     defaults.
 * @param mode         Constant describing how the user will select choices from
 *                     the spinner.
 * @param popupTheme   The theme against which the dialog or dropdown popup
 *                     should be inflated. May be {@code null} to use the
 *                     view theme. If set, this will override any value
 *                     specified by
 *                     {@link R.styleable#Spinner_popupTheme}.
 * @see #MODE_DIALOG
 * @see #MODE_DROPDOWN
 */
public AppCompatSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode,
        Resources.Theme popupTheme) {
    super(context, attrs, defStyleAttr);

    TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.Spinner, defStyleAttr,
            0);

    mDrawableManager = AppCompatDrawableManager.get();
    mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mDrawableManager);

    if (popupTheme != null) {
        mPopupContext = new ContextThemeWrapper(context, popupTheme);
    } else {
        final int popupThemeResId = a.getResourceId(R.styleable.Spinner_popupTheme, 0);
        if (popupThemeResId != 0) {
            mPopupContext = new ContextThemeWrapper(context, popupThemeResId);
        } else {
            // If we're running on a < M device, we'll use the current context and still handle
            // any dropdown popup
            mPopupContext = !IS_AT_LEAST_M ? context : null;
        }
    }

    if (mPopupContext != null) {
        if (mode == MODE_THEME) {
            if (Build.VERSION.SDK_INT >= 11) {
                // If we're running on API v11+ we will try and read android:spinnerMode
                TypedArray aa = null;
                try {
                    aa = context.obtainStyledAttributes(attrs, ATTRS_ANDROID_SPINNERMODE, defStyleAttr, 0);
                    if (aa.hasValue(0)) {
                        mode = aa.getInt(0, MODE_DIALOG);
                    }
                } catch (Exception e) {
                    Log.i(TAG, "Could not read android:spinnerMode", e);
                } finally {
                    if (aa != null) {
                        aa.recycle();
                    }
                }
            } else {
                // Else, we use a default mode of dropdown
                mode = MODE_DROPDOWN;
            }
        }

        if (mode == MODE_DROPDOWN) {
            final DropdownPopup popup = new DropdownPopup(mPopupContext, attrs, defStyleAttr);
            final TintTypedArray pa = TintTypedArray.obtainStyledAttributes(mPopupContext, attrs,
                    R.styleable.Spinner, defStyleAttr, 0);
            mDropDownWidth = pa.getLayoutDimension(R.styleable.Spinner_android_dropDownWidth,
                    LayoutParams.WRAP_CONTENT);
            popup.setBackgroundDrawable(pa.getDrawable(R.styleable.Spinner_android_popupBackground));
            popup.setPromptText(a.getString(R.styleable.Spinner_android_prompt));
            pa.recycle();

            mPopup = popup;
            mForwardingListener = new ForwardingListener(this) {
                @Override
                public ShowableListMenu getPopup() {
                    return popup;
                }

                @Override
                public boolean onForwardingStarted() {
                    if (!mPopup.isShowing()) {
                        mPopup.show();
                    }
                    return true;
                }
            };
        }
    }

    final CharSequence[] entries = a.getTextArray(R.styleable.Spinner_android_entries);
    if (entries != null) {
        final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(context,
                android.R.layout.simple_spinner_item, entries);
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        setAdapter(adapter);
    }

    a.recycle();

    mPopupSet = true;

    // Base constructors can call setAdapter before we initialize mPopup.
    // Finish setting things up if this happened.
    if (mTempAdapter != null) {
        setAdapter(mTempAdapter);
        mTempAdapter = null;
    }

    mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
}

From source file:cnedu.ustcjd.widget.MultiSlider.java

public MultiSlider(Context context, AttributeSet attrs, int defStyle, int styleRes) {
    super(context, attrs, defStyle);
    if ((Build.VERSION.SDK_INT >= 21) && getBackground() == null) {
        setBackgroundResource(R.drawable.control_background_multi_material);
    }/*w  w  w.  j av a 2  s .co m*/

    mUiThreadId = Thread.currentThread().getId();

    a = context.obtainStyledAttributes(attrs, R.styleable.MultiSlider, defStyle, styleRes);
    mNoInvalidate = true;
    int numThumbs = a.getInt(R.styleable.MultiSlider_thumbNumber, 2);
    initMultiSlider(numThumbs);

    Drawable trackDrawable = a.getDrawable(R.styleable.MultiSlider_android_track);
    if (trackDrawable == null) {
        trackDrawable = ContextCompat.getDrawable(getContext(), R.drawable.multislider_track_material);
    }

    setTrackDrawable(getTintedDrawable(trackDrawable, a.getColor(R.styleable.MultiSlider_trackColor, 0)));

    //TODO
    //        mMinWidth = a.getDimensionPixelSize(R.styleable.MultiSlider_minWidth, mMinWidth);
    //        mMaxWidth = a.getDimensionPixelSize(R.styleable.MultiSlider_maxWidth, mMaxWidth);
    //        mMinHeight = a.getDimensionPixelSize(R.styleable.MultiSlider_minHeight, mMinHeight);
    //        mMaxHeight = a.getDimensionPixelSize(R.styleable.MultiSlider_maxHeight, mMaxHeight);

    setStep(a.getInt(R.styleable.MultiSlider_scaleStep, mStep));
    setStepsThumbsApart(a.getInt(R.styleable.MultiSlider_stepsThumbsApart, mStepsThumbsApart));
    setDrawThumbsApart(a.getBoolean(R.styleable.MultiSlider_drawThumbsApart, mDrawThumbsApart));
    setMax(a.getInt(R.styleable.MultiSlider_scaleMax, mScaleMax), true);
    setMin(a.getInt(R.styleable.MultiSlider_scaleMin, mScaleMin), true);

    mMirrorForRtl = a.getBoolean(R.styleable.MultiSlider_mirrorForRTL, mMirrorForRtl);

    // --> now place thumbs

    defThumbDrawable = a.getDrawable(R.styleable.MultiSlider_android_thumb);

    if (defThumbDrawable == null) {
        if (Build.VERSION.SDK_INT >= 21) {
            defThumbDrawable = ContextCompat.getDrawable(getContext(),
                    R.drawable.multislider_thumb_material_anim);
        } else {
            defThumbDrawable = ContextCompat.getDrawable(getContext(), R.drawable.multislider_thumb_material);
        }
    }

    defRangeDrawable = a.getDrawable(R.styleable.MultiSlider_range);
    if (defRangeDrawable == null) {
        defRangeDrawable = ContextCompat.getDrawable(getContext(), R.drawable.multislider_range_material);
    }

    Drawable range1Drawable = a.getDrawable(R.styleable.MultiSlider_range1);
    Drawable range2Drawable = a.getDrawable(R.styleable.MultiSlider_range2);

    defRangeColor = a.getColor(R.styleable.MultiSlider_rangeColor, 0);
    defThumbColor = a.getColor(R.styleable.MultiSlider_thumbColor, 0);
    setThumbDrawables(defThumbDrawable, defRangeDrawable, range1Drawable, range2Drawable); //
    // will
    // guess thumbOffset if
    // thumb != null...
    // ...but allow layout to override this

    int thumbOffset = a.getDimensionPixelOffset(R.styleable.MultiSlider_android_thumbOffset,
            defThumbDrawable.getIntrinsicWidth() / 2);
    setThumbOffset(thumbOffset);

    repositionThumbs();

    mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    mNoInvalidate = false;
    a.recycle();
}

From source file:com.appunite.list.GridView.java

public GridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridView, defStyle, 0);

    int hSpacing = a.getDimensionPixelOffset(R.styleable.GridView_horizontalSpacing, 0);
    setHorizontalSpacing(hSpacing);/*w w  w . j  a va2 s .com*/

    int vSpacing = a.getDimensionPixelOffset(R.styleable.GridView_verticalSpacing, 0);
    setVerticalSpacing(vSpacing);

    int index = a.getInt(R.styleable.GridView_stretchMode, STRETCH_COLUMN_WIDTH);
    if (index >= 0) {
        setStretchMode(index);
    }

    int columnWidth = a.getDimensionPixelOffset(R.styleable.GridView_columnWidth, -1);
    if (columnWidth > 0) {
        setColumnWidth(columnWidth);
    }

    int numColumns = a.getInt(R.styleable.GridView_numColumns, 1);
    setNumColumns(numColumns);

    index = a.getInt(R.styleable.GridView_android_gravity, -1);
    if (index >= 0) {
        setGravity(index);
    }

    a.recycle();
}

From source file:com.androtex.viewpagerindicator.TitlePageIndicator.java

public TitlePageIndicator(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    //Load defaults from resources
    final Resources res = getResources();
    final int defaultFooterColor = res.getColor(R.color.default_title_indicator_footer_color);
    final float defaultFooterLineHeight = res.getDimension(R.dimen.default_title_indicator_footer_line_height);
    final int defaultFooterIndicatorStyle = res
            .getInteger(R.integer.default_title_indicator_footer_indicator_style);
    final float defaultFooterIndicatorHeight = res
            .getDimension(R.dimen.default_title_indicator_footer_indicator_height);
    final float defaultFooterIndicatorUnderlinePadding = res
            .getDimension(R.dimen.default_title_indicator_footer_indicator_underline_padding);
    final float defaultFooterPadding = res.getDimension(R.dimen.default_title_indicator_footer_padding);
    @SuppressWarnings("unused")
    final int defaultSelectedColor = res.getColor(R.color.default_title_indicator_selected_color);
    final boolean defaultSelectedBold = res.getBoolean(R.bool.default_title_indicator_selected_bold);
    final int defaultTextColor = res.getColor(R.color.default_title_indicator_text_color);
    final float defaultTextSize = res.getDimension(R.dimen.default_title_indicator_text_size);
    final float defaultTitlePadding = res.getDimension(R.dimen.default_title_indicator_title_padding);
    final float defaultClipPadding = res.getDimension(R.dimen.default_title_indicator_clip_padding);
    final float defaultTopPadding = res.getDimension(R.dimen.default_title_indicator_top_padding);

    //Retrieve styles attributes
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitlePageIndicator, defStyle,
            R.style.Widget_TitlePageIndicator);

    //Retrieve the colors to be used for this view and apply them.
    mFooterLineHeight = a.getDimension(R.styleable.TitlePageIndicator_footerLineHeight,
            defaultFooterLineHeight);//  w w  w  .j ava  2 s  . c o m
    mFooterIndicatorStyle = IndicatorStyle.fromValue(
            a.getInteger(R.styleable.TitlePageIndicator_footerIndicatorStyle, defaultFooterIndicatorStyle));
    mFooterIndicatorHeight = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorHeight,
            defaultFooterIndicatorHeight);
    mFooterIndicatorUnderlinePadding = a.getDimension(
            R.styleable.TitlePageIndicator_footerIndicatorUnderlinePadding,
            defaultFooterIndicatorUnderlinePadding);
    mFooterPadding = a.getDimension(R.styleable.TitlePageIndicator_footerPadding, defaultFooterPadding);
    mTopPadding = a.getDimension(R.styleable.TitlePageIndicator_topPadding, defaultTopPadding);
    mTitlePadding = a.getDimension(R.styleable.TitlePageIndicator_titlePadding, defaultTitlePadding);
    mClipPadding = a.getDimension(R.styleable.TitlePageIndicator_clipPadding, defaultClipPadding);
    mColorSelected = 0; //a.getColor(R.styleable.TitlePageIndicator_selectedColor, defaultSelectedColor);
    mColorText = a.getColor(R.styleable.TitlePageIndicator_textColor, defaultTextColor);
    mBoldText = a.getBoolean(R.styleable.TitlePageIndicator_selectedBold, defaultSelectedBold);

    final float textSize = a.getDimension(R.styleable.TitlePageIndicator_textSize, defaultTextSize);
    final int footerColor = a.getColor(R.styleable.TitlePageIndicator_footerColor, defaultFooterColor);
    mPaintText = new Paint();
    mPaintText.setTextSize(textSize);
    mPaintText.setAntiAlias(true);
    mPaintFooterLine = new Paint();
    mPaintFooterLine.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaintFooterLine.setStrokeWidth(mFooterLineHeight);
    mPaintFooterLine.setColor(footerColor);
    mPaintFooterIndicator = new Paint();
    mPaintFooterIndicator.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaintFooterIndicator.setColor(footerColor);

    a.recycle();
}

From source file:android.support.designox.widget.TabLayout.java

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

    ThemeUtils.checkAppCompatTheme(context);

    // Disable the Scroll Bar
    setHorizontalScrollBarEnabled(false);

    // Add the TabStrip
    mTabStrip = new SlidingTabStrip(context);
    super.addView(mTabStrip, 0,
            new HorizontalScrollView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabLayout, defStyleAttr,
            R.style.Widget_Design_TabLayout);

    mTabStrip.setSelectedIndicatorHeight(a.getDimensionPixelSize(R.styleable.TabLayout_tabIndicatorHeight, 0));
    mTabStrip.setSelectedIndicatorColor(a.getColor(R.styleable.TabLayout_tabIndicatorColor, 0));

    mTabPaddingStart = mTabPaddingTop = mTabPaddingEnd = mTabPaddingBottom = a
            .getDimensionPixelSize(R.styleable.TabLayout_tabPadding, 0);
    mTabPaddingStart = a.getDimensionPixelSize(R.styleable.TabLayout_tabPaddingStart, mTabPaddingStart);
    mTabPaddingTop = a.getDimensionPixelSize(R.styleable.TabLayout_tabPaddingTop, mTabPaddingTop);
    mTabPaddingEnd = a.getDimensionPixelSize(R.styleable.TabLayout_tabPaddingEnd, mTabPaddingEnd);
    mTabPaddingBottom = a.getDimensionPixelSize(R.styleable.TabLayout_tabPaddingBottom, mTabPaddingBottom);

    mTabTextAppearance = a.getResourceId(R.styleable.TabLayout_tabTextAppearance,
            R.style.TextAppearance_Design_Tab);

    // Text colors/sizes come from the text appearance first
    final TypedArray ta = context.obtainStyledAttributes(mTabTextAppearance, R.styleable.TextAppearance);
    try {/*w  w w . jav  a 2 s. c o m*/
        mTabTextSize = ta.getDimensionPixelSize(R.styleable.TextAppearance_android_textSize, 0);
        mTabTextColors = ta.getColorStateList(R.styleable.TextAppearance_android_textColor);
    } finally {
        ta.recycle();
    }

    if (a.hasValue(R.styleable.TabLayout_tabTextColor)) {
        // If we have an explicit text color set, use it instead
        mTabTextColors = a.getColorStateList(R.styleable.TabLayout_tabTextColor);
    }

    if (a.hasValue(R.styleable.TabLayout_tabSelectedTextColor)) {
        // We have an explicit selected text color set, so we need to make merge it with the
        // current colors. This is exposed so that developers can use theme attributes to set
        // this (theme attrs in ColorStateLists are Lollipop+)
        final int selected = a.getColor(R.styleable.TabLayout_tabSelectedTextColor, 0);
        mTabTextColors = createColorStateList(mTabTextColors.getDefaultColor(), selected);
    }

    mRequestedTabMinWidth = a.getDimensionPixelSize(R.styleable.TabLayout_tabMinWidth, INVALID_WIDTH);
    mRequestedTabMaxWidth = a.getDimensionPixelSize(R.styleable.TabLayout_tabMaxWidth, INVALID_WIDTH);
    mTabBackgroundResId = a.getResourceId(R.styleable.TabLayout_tabBackground, 0);
    mContentInsetStart = a.getDimensionPixelSize(R.styleable.TabLayout_tabContentStart, 0);
    mMode = a.getInt(R.styleable.TabLayout_tabMode, MODE_FIXED);
    mTabGravity = a.getInt(R.styleable.TabLayout_tabGravity, GRAVITY_FILL);
    a.recycle();

    // TODO add attr for these
    final Resources res = getResources();
    mTabTextMultiLineSize = res.getDimensionPixelSize(R.dimen.design_tab_text_size_2line);
    mScrollableTabMinWidth = res.getDimensionPixelSize(R.dimen.design_tab_scrollable_min_width);

    // Now apply the tab mode and gravity
    applyModeAndGravity();
}