Example usage for android.content.res TypedArray getString

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

Introduction

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

Prototype

@Nullable
public String getString(@StyleableRes int index) 

Source Link

Document

Retrieves the string value for the attribute at index.

Usage

From source file:com.chuhan.privatecalc.fragment.os.FragmentActivity.java

/**
 * Add support for inflating the <fragment> tag.
 */// w ww  .j a va 2  s .  co m
@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();

    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;
}

From source file:it.sephiroth.android.library.bottomnavigation.BottomNavigation.java

private void initialize(final Context context, final AttributeSet attrs, final int defStyleAttr,
        final int defStyleRes) {
    typeface = new SoftReference<>(Typeface.DEFAULT);

    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BottomNavigation, defStyleAttr,
            defStyleRes);//www . j a  v a  2s. c  om
    final int menuResId = array.getResourceId(R.styleable.BottomNavigation_bbn_entries, 0);
    pendingMenu = MenuParser.inflateMenu(context, menuResId);
    badgeProvider = parseBadgeProvider(this, context,
            array.getString(R.styleable.BottomNavigation_bbn_badgeProvider));
    array.recycle();

    backgroundColorAnimation = getResources().getInteger(R.integer.bbn_background_animation_duration);
    defaultSelectedIndex = 0;

    defaultHeight = getResources().getDimensionPixelSize(R.dimen.bbn_bottom_navigation_height);
    defaultWidth = getResources().getDimensionPixelSize(R.dimen.bbn_bottom_navigation_width);
    shadowHeight = getResources().getDimensionPixelOffset(R.dimen.bbn_top_shadow_height);

    // check if the bottom navigation is translucent
    if (!isInEditMode()) {
        final Activity activity = MiscUtils.getActivity(context);
        if (null != activity) {
            final SystemBarTintManager systembarTint = new SystemBarTintManager(activity);
            if (MiscUtils.hasTranslucentNavigation(activity) && systembarTint.getConfig().isNavigationAtBottom()
                    && systembarTint.getConfig().hasNavigtionBar()) {
                bottomInset = systembarTint.getConfig().getNavigationBarHeight();
            } else {
                bottomInset = 0;
            }
            topInset = systembarTint.getConfig().getStatusBarHeight();
        }
    }

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
    backgroundOverlay = new View(getContext());
    backgroundOverlay.setLayoutParams(params);
    addView(backgroundOverlay);
}

From source file:com.tencent.tws.assistant.support.v4.app.TwsFragmentActivity.java

/**
 * Add support for inflating the &lt;fragment> tag.
 *//*from w ww  .  j  av a 2s. c om*/
@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;
}

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

private Typeface readFontFamilyTypeface(int resId) {
    final TypedArray a = mView.getContext().obtainStyledAttributes(resId,
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ? new int[] { android.R.attr.fontFamily }
                    : new int[0]);
    try {/*from   w w  w  .  j  ava  2  s .  c o  m*/
        final String family = a.getString(0);
        if (family != null) {
            return Typeface.create(family, Typeface.NORMAL);
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to read font family typeface: " + resId);
    } finally {
        a.recycle();
    }
    return null;
}

From source file:com.manuelpeinado.numericpageindicator.NumericPageIndicator.java

@SuppressWarnings("deprecation")
public NumericPageIndicator(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (isInEditMode()) {
        return;//from w  w w  .  j  a v  a2s.  c om
    }

    // Load defaults from resources
    final Resources res = getResources();
    final int defaultTextColor = res.getColor(R.color.default_page_number_indicator_text_color);
    final int defaultPageNumberTextColor = res
            .getColor(R.color.default_page_number_indicator_page_number_text_color);
    final boolean defaultPageNumberTextBold = res
            .getBoolean(R.bool.default_page_number_indicator_page_number_text_bold);
    final int defaultButtonPressedColor = res
            .getColor(R.color.default_page_number_indicator_pressed_button_color);
    final float defaultTopPadding = res.getDimension(R.dimen.default_page_number_indicator_top_padding);
    final float defaultBottomPadding = res.getDimension(R.dimen.default_page_number_indicator_bottom_padding);
    final float defaultTextSize = res.getDimension(R.dimen.default_page_number_indicator_text_size);
    final boolean defaultShowChangePageButtons = res
            .getBoolean(R.bool.default_page_number_indicator_show_change_page_buttons);
    final boolean defaultShowStartEndButtons = res
            .getBoolean(R.bool.default_page_number_indicator_show_start_end_buttons);
    final boolean defaultShowImagesForPageControls = res
            .getBoolean(R.bool.default_page_number_indicator_show_images_for_page_controls);

    // Retrieve styles attributes
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumericPageIndicator, defStyle, 0);

    mTextTemplate = a.getString(R.styleable.NumericPageIndicator_textTemplate);
    if (mTextTemplate == null) {
        mTextTemplate = res.getString(R.string.default_page_number_indicator_text_template);
        ;
    }
    parseTextTemplate();

    mTextStartButton = a.getString(R.styleable.NumericPageIndicator_startButtonText);
    if (mTextStartButton == null) {
        mTextStartButton = res.getString(R.string.default_page_number_indicator_start_button_text);
    }
    mTextEndButton = a.getString(R.styleable.NumericPageIndicator_endButtonText);
    if (mTextEndButton == null) {
        mTextEndButton = res.getString(R.string.default_page_number_indicator_end_button_text);
    }
    mTextPreviousButton = a.getString(R.styleable.NumericPageIndicator_previousButtonText);
    if (mTextPreviousButton == null) {
        mTextPreviousButton = res.getString(R.string.default_page_number_indicator_previous_button_text);
    }
    mTextNextButton = a.getString(R.styleable.NumericPageIndicator_nextButtonText);
    if (mTextNextButton == null) {
        mTextNextButton = res.getString(R.string.default_page_number_indicator_next_button_text);
    }

    // these will be null if they are unused
    mStartButtonImage = getBitmapFromDrawable(a.getDrawable(R.styleable.NumericPageIndicator_startButtonImage));
    mEndButtonImage = getBitmapFromDrawable(a.getDrawable(R.styleable.NumericPageIndicator_endButtonImage));
    mPreviousButtonImage = getBitmapFromDrawable(
            a.getDrawable(R.styleable.NumericPageIndicator_previousButtonImage));
    mNextButtonImage = getBitmapFromDrawable(a.getDrawable(R.styleable.NumericPageIndicator_nextButtonImage));

    mColorText = a.getColor(R.styleable.NumericPageIndicator_android_textColor, defaultTextColor);
    mColorPageNumberText = a.getColor(R.styleable.NumericPageIndicator_pageNumberTextColor,
            defaultPageNumberTextColor);
    mPageNumberTextBold = a.getBoolean(R.styleable.NumericPageIndicator_pageNumberTextBold,
            defaultPageNumberTextBold);
    mColorPressedButton = a.getColor(R.styleable.NumericPageIndicator_pressedButtonColor,
            defaultButtonPressedColor);
    mPaddingTop = a.getDimension(R.styleable.NumericPageIndicator_android_paddingTop, defaultTopPadding);
    mPaddingBottom = a.getDimension(R.styleable.NumericPageIndicator_android_paddingBottom,
            defaultBottomPadding);
    mPaintText.setColor(mColorText);
    mShowChangePageButtons = a.getBoolean(R.styleable.NumericPageIndicator_showChangePageButtons,
            defaultShowChangePageButtons);
    mShowStartEndButtons = a.getBoolean(R.styleable.NumericPageIndicator_showStartEndButtons,
            defaultShowStartEndButtons);
    mShowImagesForPageControls = a.getBoolean(R.styleable.NumericPageIndicator_showImagesForPageControls,
            defaultShowImagesForPageControls);

    mPaintButtonBackground.setColor(mColorPressedButton);
    final float textSize = a.getDimension(R.styleable.NumericPageIndicator_android_textSize, defaultTextSize);
    mPaintText.setTextSize(textSize);
    mPaintText.setAntiAlias(true);

    mPaintPageNumberText.setColor(mColorPageNumberText);
    mPaintPageNumberText.setTextSize(textSize);
    mPaintPageNumberText.setAntiAlias(true);
    if (mPageNumberTextBold) {
        mPaintPageNumberText.setTypeface(Typeface.DEFAULT_BOLD);
    }

    final Drawable background = a.getDrawable(R.styleable.NumericPageIndicator_android_background);
    if (background != null) {
        setBackgroundDrawable(background);
    }

    a.recycle();
}

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

private Typeface readFontFamilyTypeface(int resId) {
    final TypedArray a = mView.getContext().obtainStyledAttributes(resId,
            android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN
                    ? new int[] { android.R.attr.fontFamily }
                    : new int[0]);
    try {//  w  w w  .j a v  a  2 s  .co m
        final String family = a.getString(0);
        if (family != null) {
            return Typeface.create(family, Typeface.NORMAL);
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to read font family typeface: " + resId);
    } finally {
        a.recycle();
    }
    return null;
}

From source file:com.dirkgassen.wator.ui.view.RangeSlider.java

/**
 * Read out the attributes from the given attribute set and initialize whatever they represent.
 *
 * @param attributeArray typed array containing the attribute values from the XML file
 *///  w  w  w  .  j  a  v a2s . co  m
private void setupAttributes(TypedArray attributeArray) {
    minValue = attributeArray.getInt(R.styleable.RangeSlider_minValue, 0);
    maxValue = attributeArray.getInt(R.styleable.RangeSlider_maxValue, 100);

    thumbPadding = attributeArray.getDimension(R.styleable.RangeSlider_thumbPadding,
            6 /* dp */ * displayDensity);

    thumbFormat = attributeArray.getString(R.styleable.RangeSlider_thumbFormat);
    if (thumbFormat == null) {
        thumbFormat = "%d";
    }

    String valueSetString = attributeArray.getString(R.styleable.RangeSlider_valueSet);
    if (valueSetString == null) {
        value = attributeArray.getInt(R.styleable.RangeSlider_android_value, minValue);
    } else {
        String[] values = valueSetString.split(",");
        valueSet = new int[values.length];
        for (int no = 0; no < values.length; no++) {
            valueSet[no] = Integer.valueOf(values[no]);
        }
        Arrays.sort(valueSet);
        value = attributeArray.getInt(R.styleable.RangeSlider_android_value, valueSet[0]);
    }

    thumbTextPaint.setTextSize(attributeArray.getDimension(R.styleable.RangeSlider_android_textSize, 12));
    thumbTextPaint.setColor(attributeArray.getColor(R.styleable.RangeSlider_android_textColor,
            ContextCompat.getColor(getContext(), android.R.color.white)));

    thumbBackgroundPaint.setStyle(Paint.Style.FILL);
    thumbBackgroundPaint.setColor(attributeArray.getColor(R.styleable.RangeSlider_android_color,
            ContextCompat.getColor(getContext(), android.R.color.black)));

    sliderPaint.setStrokeWidth(attributeArray.getDimension(R.styleable.RangeSlider_sliderThickness, 2));
    sliderPaint.setStrokeCap(Paint.Cap.ROUND);
    sliderPaint.setColor(thumbBackgroundPaint.getColor());

}

From source file:com.owen.tvrecyclerview.widget.TvRecyclerView.java

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

    init(context);//  ww  w. j av  a  2s  .c  o m

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

    final String name = a.getString(R.styleable.TvRecyclerView_tv_layoutManager);
    if (!TextUtils.isEmpty(name)) {
        loadLayoutManagerFromName(context, attrs, name);
    }
    mSelectedItemCentered = a.getBoolean(R.styleable.TvRecyclerView_tv_selectedItemIsCentered, false);
    mIsInterceptKeyEvent = a.getBoolean(R.styleable.TvRecyclerView_tv_isInterceptKeyEvent, false);
    mIsMenu = a.getBoolean(R.styleable.TvRecyclerView_tv_isMenu, false);
    mIsSelectFirstVisiblePosition = a.getBoolean(R.styleable.TvRecyclerView_tv_isSelectFirstVisiblePosition,
            false);
    mLoadMoreBeforehandCount = a.getInt(R.styleable.TvRecyclerView_tv_loadMoreBeforehandCount,
            DEFAULT_LOAD_MORE_BEFOREHAND_COUNT);
    mSelectedItemOffsetStart = a.getDimensionPixelOffset(R.styleable.TvRecyclerView_tv_selectedItemOffsetStart,
            DEFAULT_SELECTED_ITEM_OFFSET);
    mSelectedItemOffsetEnd = a.getDimensionPixelOffset(R.styleable.TvRecyclerView_tv_selectedItemOffsetEnd,
            DEFAULT_SELECTED_ITEM_OFFSET);

    a.recycle();
}

From source file:com.haibin.calendarview.CalendarView.java

public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CalendarView);
    mCurDayColor = array.getColor(R.styleable.CalendarView_current_day_color, Color.RED);
    mSchemeThemeColor = array.getColor(R.styleable.CalendarView_scheme_theme_color, Color.RED);
    mWeekBackground = array.getColor(R.styleable.CalendarView_week_background, Color.WHITE);
    mWeekTextColor = array.getColor(R.styleable.CalendarView_week_text_color, Color.RED);
    mSelectedColor = array.getColor(R.styleable.CalendarView_selected_color, 0x50CFCFCF);
    mSelectedTextColor = array.getColor(R.styleable.CalendarView_selected_text_color, 0xFF111111);
    mMinYear = array.getInt(R.styleable.CalendarView_min_year, 2010);
    mMaxYear = array.getInt(R.styleable.CalendarView_max_year, 2050);
    mScheme = array.getString(R.styleable.CalendarView_scheme_text);
    if (mMinYear <= 1900)
        mMaxYear = 1900;//from ww  w  .  j a  v  a2s  . com
    if (mMaxYear >= 2099)
        mMaxYear = 2099;
    array.recycle();
    init(context);
}

From source file:com.pavelsikun.seekbarpreference.SeekBarPreference.java

private void init(AttributeSet attrs) {
    setLayoutResource(R.layout.seekbar_preference);

    if (attrs == null) {
        mMinValue = DEFAULT_MIN_VALUE;/*from   w w w  .  java2 s . com*/
        mMaxValue = DEFAULT_MAX_VALUE;
        mInterval = DEFAULT_INTERVAL;
        mMeasurementUnit = DEFAULT_MEASUREMENT_UNIT;
        mValueTextSize = DEFAULT_TEXT_SIZE;
    } else {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.SeekBarPreference);
        try {
            mMinValue = ta.getInt(R.styleable.SeekBarPreference_msbp_minValue, DEFAULT_MIN_VALUE);
            mMaxValue = ta.getInt(R.styleable.SeekBarPreference_msbp_maxValue, DEFAULT_MAX_VALUE);
            mInterval = ta.getInt(R.styleable.SeekBarPreference_msbp_interval, DEFAULT_INTERVAL);

            mDefaultValue = attrs.getAttributeIntValue(android.R.attr.defaultValue, DEFAULT_CURRENT_VALUE);

            mValueTextSize = ta.getDimensionPixelSize(R.styleable.SeekBarPreference_msbp_valueTextSize,
                    (int) (getContext().getResources().getDisplayMetrics().density * DEFAULT_TEXT_SIZE));

            if (mDefaultValue < mMinValue) {
                mDefaultValue = (mMaxValue - mMinValue) / 2;
            }
            mMeasurementUnit = ta.getString(R.styleable.SeekBarPreference_msbp_measurementUnit);
            if (mMeasurementUnit == null) {
                mMeasurementUnit = DEFAULT_MEASUREMENT_UNIT;
            }
        } finally {
            ta.recycle();
        }
    }
}