Example usage for android.content.res TypedArray getText

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

Introduction

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

Prototype

public CharSequence getText(@StyleableRes int index) 

Source Link

Document

Retrieves the styled string value for the attribute at index.

Usage

From source file:de.mrapp.android.util.ThemeUtil.java

/**
 * Obtains the text, which corresponds to a specific resource id, from a specific theme. If the
 * given resource id is invalid, a {@link NotFoundException} will be thrown.
 *
 * @param context//www. j a  va  2  s  .c om
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param themeResourceId
 *         The resource id of the theme, the attribute should be obtained from, as an {@link
 *         Integer} value or -1, if the attribute should be obtained from the given context's
 *         theme
 * @param resourceId
 *         The resource id of the attribute, which should be obtained, as an {@link Integer}
 *         value. The resource id must corresponds to a valid theme attribute
 * @return The text, which has been obtained, as an instance of the type {@link CharSequence}
 */
public static CharSequence getText(@NonNull final Context context, @StyleRes final int themeResourceId,
        @AttrRes final int resourceId) {
    TypedArray typedArray = null;

    try {
        typedArray = obtainStyledAttributes(context, themeResourceId, resourceId);
        CharSequence text = typedArray.getText(0);

        if (text == null) {
            throw new NotFoundException("Resource ID #0x" + Integer.toHexString(resourceId) + " is not valid");
        }

        return text;
    } finally {
        if (typedArray != null) {
            typedArray.recycle();
        }
    }
}

From source file:mil.nga.giat.mage.help.OpensourceItemFragment.java

private void initView(Context context, AttributeSet attrs) {
    View view = inflate(getContext(), R.layout.fragment_opensource_item, null);

    if (attrs != null) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OpensourceItemFragment);
        CharSequence name = a.getText(R.styleable.OpensourceItemFragment_name);
        CharSequence copyright = a.getText(R.styleable.OpensourceItemFragment_copyright);
        CharSequence license = a.getText(R.styleable.OpensourceItemFragment_license);
        a.recycle();//from   w w  w.  ja  v  a2 s. c o m

        TextView nameTextView = (TextView) view.findViewById(R.id.opensource_item_name);
        if (StringUtils.isBlank(name)) {
            nameTextView.setVisibility(View.GONE);
        } else {
            nameTextView.setText(name);
        }

        TextView copyrightTextView = (TextView) view.findViewById(R.id.opensource_item_copyright);
        if (StringUtils.isBlank(copyright)) {
            copyrightTextView.setVisibility(View.GONE);
        } else {
            copyrightTextView.setText(copyright);
        }

        TextView licenseTextView = (TextView) view.findViewById(R.id.opensource_item_license);
        if (StringUtils.isBlank(license)) {
            licenseTextView.setVisibility(View.GONE);
        } else {
            licenseTextView.setText(license);
        }
    }

    addView(view);
}

From source file:com.auth0.android.lock.views.CheckableOptionView.java

private void init(AttributeSet attrs) {
    final View v = inflate(getContext(), R.layout.com_auth0_lock_checkable_option, this);
    icon = (ImageView) v.findViewById(R.id.com_auth0_lock_checkable_text_icon);
    description = (TextView) v.findViewById(R.id.com_auth0_lock_checkable_text_description);

    if (attrs == null) {
        return;//from ww  w.ja v  a  2  s .c  om
    }

    int[] set = { android.R.attr.text };
    TypedArray a = getContext().obtainStyledAttributes(attrs, set);
    CharSequence text = a.getText(0);
    a.recycle();
    description.setText(text);
    updateStatus();
}

From source file:co.paulburke.android.textviewpager.TextViewPager.java

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

    mPagerAdapter = new TextViewPagerAdapter(context, mMeasureListener);

    if (attrs != null) {
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TextViewPager);

        int layout = attributes.getResourceId(R.styleable.TextViewPager_textViewLayout, -1);
        CharSequence text = attributes.getText(R.styleable.TextViewPager_android_text);

        mPagerAdapter.setTextViewLayout(layout);
        mPagerAdapter.setText(text);//  ww  w.java2 s  .co m

        attributes.recycle();
    }

    setAdapter(mPagerAdapter);
}

From source file:org.wso2.iot.nfcprovisioning.uielements.TextInputLayoutWithHelpText.java

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

    final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextInputLayoutWithHelpText, 0,
            0);/*from   w  ww.ja  v a2 s. c  o m*/
    try {
        mHelperTextColor = a.getColorStateList(R.styleable.TextInputLayoutWithHelpText_helperTextColor);
        mHelperText = a.getText(R.styleable.TextInputLayoutWithHelpText_helperText);
    } finally {
        a.recycle();
    }
}

From source file:androidx.navigation.NavDestination.java

/**
 * Called when inflating a destination from a resource.
 *
 * @param context local context performing inflation
 * @param attrs attrs to parse during inflation
 *///from   w ww  . j  a  va  2 s  .  com
@CallSuper
public void onInflate(@NonNull Context context, @NonNull AttributeSet attrs) {
    final TypedArray a = context.getResources().obtainAttributes(attrs, R.styleable.Navigator);
    setId(a.getResourceId(R.styleable.Navigator_android_id, 0));
    setLabel(a.getText(R.styleable.Navigator_android_label));
    a.recycle();
}

From source file:im.ene.ribbon.MenuParser.java

/**
 * Called when the parser is pointing to an item tag.
 *///from  www.j av  a 2s .  c o m
public void readItem(Context mContext, AttributeSet attrs) {
    TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.BottomNavigationMenuItem);
    item = new MenuItem();
    item.itemId = a.getResourceId(R.styleable.BottomNavigationMenuItem_android_id, 0);
    item.itemTitle = a.getText(R.styleable.BottomNavigationMenuItem_android_title);
    item.itemIcon = a.getResourceId(R.styleable.BottomNavigationMenuItem_android_icon, 0);
    item.itemEnabled = a.getBoolean(R.styleable.BottomNavigationMenuItem_android_enabled, true);
    item.itemColor = a.getColor(R.styleable.BottomNavigationMenuItem_android_color, 0);
    a.recycle();
}

From source file:com.albedinsky.android.setting.SettingSelectionDialogPreference.java

/**
 *///from w w w.  j  av  a 2  s.  c  o m
@Override
protected Object onGetDefaultValue(@NonNull TypedArray typedArray, int index) {
    return typedArray.getText(index);
}

From source file:com.iangclifton.android.floatlabel.FloatLabel.java

private void init(Context context, AttributeSet attrs, int defStyle) {
    // Load custom attributes
    final int layout;
    final CharSequence text;
    final CharSequence hint;
    final ColorStateList hintColor;
    final int floatLabelColor;
    final int inputType;

    if (attrs == null) {
        layout = R.layout.float_label;
        text = null;//  w  w w. ja va2s.  c  om
        hint = null;
        hintColor = null;
        floatLabelColor = 0;
        inputType = 0;
    } else {
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FloatLabel, defStyle, 0);

        layout = a.getResourceId(R.styleable.FloatLabel_android_layout, R.layout.float_label);
        text = a.getText(R.styleable.FloatLabel_android_text);
        hint = a.getText(R.styleable.FloatLabel_android_hint);
        hintColor = a.getColorStateList(R.styleable.FloatLabel_android_textColorHint);
        floatLabelColor = a.getColor(R.styleable.FloatLabel_floatLabelColor, 0);
        inputType = a.getInt(R.styleable.FloatLabel_android_inputType, InputType.TYPE_CLASS_TEXT);
        a.recycle();
    }

    inflate(context, layout, this);
    mEditText = (EditText) findViewById(R.id.edit_text);
    if (mEditText == null) {
        throw new RuntimeException("Your layout must have an EditText whose ID is @id/edit_text");
    }
    mEditText.setHint(hint);
    mEditText.setText(text);
    if (hintColor != null) {
        mEditText.setHintTextColor(hintColor);
    }
    if (inputType != 0) {
        mEditText.setInputType(inputType);
    }

    mLabel = (TextView) findViewById(R.id.float_label);
    if (mLabel == null) {
        throw new RuntimeException("Your layout must have a TextView whose ID is @id/float_label");
    }
    mLabel.setText(mEditText.getHint());
    if (floatLabelColor != 0)
        mLabel.setTextColor(floatLabelColor);

    // Listen to EditText to know when it is empty or nonempty
    mEditText.addTextChangedListener(new EditTextWatcher());

    // Check current state of EditText
    if (mEditText.getText().length() == 0) {
        ViewHelper.setAlpha(mLabel, 0);
        mLabelShowing = false;
    } else {
        mLabel.setVisibility(View.VISIBLE);
        mLabelShowing = true;
    }

    // Mark init as complete to prevent accidentally breaking the view by
    // adding children
    mInitComplete = true;
}

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

/**
 * Called from one of constructors of this view to perform its initialization.
 * <p>/* w w  w  .j av a  2s .c  om*/
 * 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>.
 */
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    /**
     * Process attributes.
     */
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Ui_ActionTextButton,
            defStyleAttr, defStyleRes);
    if (typedArray != null) {
        final int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            final int index = typedArray.getIndex(i);
            if (index == R.styleable.Ui_ActionTextButton_android_text) {
                setText(typedArray.getText(index));
            } else if (index == R.styleable.Ui_ActionTextButton_android_paddingLeft) {
                setPadding(typedArray.getDimensionPixelSize(index, 0), getPaddingTop(), getPaddingRight(),
                        getPaddingBottom());
            } else if (index == R.styleable.Ui_ActionTextButton_android_paddingRight) {
                setPadding(getPaddingLeft(), getPaddingTop(), typedArray.getDimensionPixelSize(index, 0),
                        getPaddingBottom());
            }
        }
        typedArray.recycle();
    }
}