I am trying to obtain several of the style attributes of the android
namespace from my code. Here I enclose the relevant extract. AttributeSet attrs
is the parameter that is passed in to any custom TextView
.
private static final int[] ATTRS = new int[] { android.R.attr.textSize,
android.R.attr.text, android.R.attr.textColor,
android.R.attr.gravity };
private void processAndroidAttributes(final Context context,
final AttributeSet attrs) {
final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
try {
final String text = a.getString(1);
myTextView.setText(text);
final float textSize = a.getDimensionPixelSize(0, DEFAULT_TEXT_SIZE);
myTextView.setTextSize(textSize);
}
My issue is that I want to read 4 attributes, described in the int[]
ATTRS
. As you see I have put textSize
as first element of this array. The reason for that is simple - if I swapped it for second place in the array its value is not read correctly (instead the provided default value is loaded). On the other hand the text loads correctly on whichever position in the array of ATTRS
I place it. I do not dare experimenting with the position preferences of the gravity
and the textColor
, but with this permutation they do not work.
Can somebody explain why the ustable behavior of obtaining the attrbutes?