- 1
- 2
- 3
Resources res = getResources(); TypedArray ta = res.obtainTypedArray(R.array.my_integers); int firstInt = ta.getInt(0, 0); //first argument is the index of the integer, and the second argument is the default value to return if the index is out of bounds or the value is not an integer. int secondInt = ta.getInt(1, 0); int thirdInt = ta.getInt(2, 0); ta.recycle(); //always call recycle() when done with the TypedArray to avoid memory leaks
int[] attrs = {R.attr.my_color, R.attr.my_dimension, R.attr.my_boolean}; //create an array of the attributes you want to retrieve TypedArray ta = context.obtainStyledAttributes(attrs); //obtain the TypedArray using the Context of your application int color = ta.getColor(0, Color.BLACK); //first argument is the index of the attribute, and the second argument is the default value to return int dimension = ta.getDimensionPixelSize(1, 0); boolean bool = ta.getBoolean(2, false); ta.recycle(); //always call recycle() when done with the TypedArray to avoid memory leaksIn both examples, the package library used is android.content.res.