Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have the same problem while trying to get 4 attributes with 3 different types. Always only one type of attribute pass to the array and this was always first of them. For example if you declare attributes like {int, String, Color}:</p> <pre><code>private static final int[] ATTRS = new int[] { android.R.attr.textSize, android.R.attr.text, android.R.attr.textColor } </code></pre> <p>Method <code>obtainStyledAttributes</code> will pass to your <code>TypedArray</code> only the int attributes, the rest will be null.</p> <p>If you change order to {String, Color, int}:</p> <pre><code>private static final int[] ATTRS = new int[] {android.R.attr.text, android.R.attr.textColor, android.R.attr.textSize } </code></pre> <p>You will get only String attributes to your <code>TypedArray</code>, rest will be null.</p> <p>It looks like the <code>TypedArray</code> can hold only one Type in it so you need to declare more TypeArrays - one for each type of attribute.</p> <p>Like this:</p> <pre><code>private static final int[] stringAttrs = new int[] {android.R.attr.text}; private static final int[] intAttrs = new int[] {android.R.attr.textSize}; private static final int[] colorAttrs = new int[] {android.R.attr.textColor, android.R.attr.background}; private void processAndroidAttributes(final Context context, final AttributeSet attrs) { final TypedArray stringA = context.obtainStyledAttributes(attrs, stringAttrs); final TypedArray intA = context.obtainStyledAttributes(attrs, intAttrs); final TypedArray colorA = context.obtainStyledAttributes(attrs, colorAttrs); myTextView.setText(stringA.getString(0)); myTextView.setTextSize(intA.getDimensionPixelSize(0, DEFAULT_TEXT_SIZE)); myTextView.setTextColor(colorA.getColor(0, Color.WHITE)); myTextView.setBackgroundColor(colorA.getColor(1, Color.WHITE)); stringA.recycle(); intA.recycle(); colorA.recycle(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload