Resources res = getResources(); TypedArray ta = res.obtainTypedArray(R.array.array0); int n = ta.length(); String[][] array = new String[n][]; for (int i = 0; i < n; ++i) { int id = ta.getResourceId(i, 0); if (id > 0) { array[i] = res.getStringArray(id); } else { // something wrong with the XML } } ta.recycle(); // Important!
@SuppressLint("Recycle") @Provides @Singleton public ResourcesProvider provideResourcesProvider(Context context) { return resId -> context.getResources().obtainTypedArray(resId); } }
private int[] loadStyleList(Context context, int resId){ if(context == null) return null; TypedArray array = context.getResources().obtainTypedArray(resId); int[] result = new int[array.length()]; for(int i = 0; i < result.length; i++) result[i] = array.getResourceId(i, 0); array.recycle(); return result; }
public static int getMaterialColor(Resources resources, int index) { TypedArray colors = resources.obtainTypedArray(R.array.mdcolor_300); final int returnColor = colors.getColor(index % colors.length(), Color.BLACK); colors.recycle(); return returnColor; }
public static Painting[] list(Resources res) { final String[] authors = res.getStringArray(R.array.paintings_authors); final String[] titles = res.getStringArray(R.array.paintings_titles); final String[] links = res.getStringArray(R.array.paintings_links); final TypedArray images = res.obtainTypedArray(R.array.paintings_images); final TypedArray thumbs = res.obtainTypedArray(R.array.paintings_thumbs); final int size = titles.length; final Painting[] paintings = new Painting[size]; for (int i = 0; i < size; i++) { final int imageId = images.getResourceId(i, -1); final int thumbId = thumbs.getResourceId(i, -1); paintings[i] = new Painting(imageId, thumbId, authors[i], titles[i], links[i]); } images.recycle(); thumbs.recycle(); return paintings; }
public CommentItemDecoration(Context context) { mPaint = new Paint(); mPaint.setStrokeWidth(context.getResources().getDimensionPixelSize(R.dimen.divider)); mHorizontalMargin = context.getResources() .getDimensionPixelSize(R.dimen.cardview_horizontal_margin); mLevelIndicatorWidth = AppUtils.getDimensionInDp(context, R.dimen.level_indicator_width); mColors = context.getResources().obtainTypedArray(R.array.color_codes); mColorCodeEnabled = Preferences.colorCodeEnabled(context); mThreadIndicatorEnabled = Preferences.threadIndicatorEnabled(context); }
private static @Nullable Unbinder parseBindArray(Object target, Field field, View source) { BindArray bindArray = field.getAnnotation(BindArray.class); if (bindArray == null) { return null; } validateMember(field); int id = bindArray.value(); Resources resources = source.getContext().getResources(); Object value; Class<?> fieldType = field.getType(); if (fieldType == TypedArray.class) { value = resources.obtainTypedArray(id); } else if (fieldType.isArray()) { Class<?> componentType = fieldType.getComponentType(); if (componentType == String.class) { value = resources.getStringArray(id); } else if (componentType == int.class) { value = resources.getIntArray(id); } else if (componentType == CharSequence.class) { value = resources.getTextArray(id); } else { throw new IllegalStateException(); // TODO } } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
private Drawable[] loadScreenIcons() { TypedArray ta = getResources().obtainTypedArray(R.array.ld_activityScreenIcons); Drawable[] icons = new Drawable[ta.length()]; for (int i = 0; i < ta.length(); i++) { int id = ta.getResourceId(i, 0); if (id != 0) { icons[i] = ContextCompat.getDrawable(this, id); } } ta.recycle(); return icons; }
int[] getLayoutsArray(int arrayResourceId) { TypedArray typedArray= getResources().obtainTypedArray(arrayResourceId); int[] result=new int[typedArray.length()]; for (int i=0;i<typedArray.length();i++) { result[i]=typedArray.getResourceId(i, -1); } return(result); } }
/** * Get color array values form array resource * * @param resources Resources * @param resId Resources id * @return color array */ public static int[] getColorsFromArrayRes(Resources resources, int resId) { try { @SuppressLint("Recycle") TypedArray array = resources.obtainTypedArray(resId); if (array.length() > 0) { final int len = array.length(); final int[] colors = new int[len]; for (int i = 0; i < len; i++) { colors[i] = array.getColor(i, 0); } return colors; } } catch (Resources.NotFoundException ignored) { } return null; }
/** * Override the image ids of this button. */ public void overrideImageIds(int resId) { TypedArray ids = null; try { ids = getResources().obtainTypedArray(resId); mImageIds = new int[ids.length()]; for (int i = 0; i < ids.length(); i++) { mImageIds[i] = ids.getResourceId(i, 0); } } finally { if (ids != null) { ids.recycle(); } } }
/** * Constructor for <code>LetterTileProvider</code> * * @param context The {@link Context} to use */ public LetterTileProvider(Context context) { final Resources res = context.getResources(); mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); mPaint.setColor(Color.WHITE); mPaint.setTextAlign(Paint.Align.CENTER); mPaint.setAntiAlias(true); mColors = res.obtainTypedArray(R.array.letter_tile_colors); mTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size); //mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.); mDefaultBitmap = drawableToBitmap(ContextCompat.getDrawable(context, R.drawable.ic_person_white_24dp)); mWidth = res.getDimensionPixelSize(R.dimen.letter_tile_size); mHeight = res.getDimensionPixelSize(R.dimen.letter_tile_size); }
TypedArray ta = context.getResources().obtainTypedArray(a.getResourceId(attr, 0)); strokeColors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++) mInAnimationDuration = a.getInteger(attr, 0); else if(attr == R.styleable.CircularProgressDrawable_cpd_inStepColors){ TypedArray ta = context.getResources().obtainTypedArray(a.getResourceId(attr, 0)); mInColors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++)
strokeColors(a.getColor(R.styleable.CircularProgressDrawable_cpd_strokeColor, ThemeUtil.colorPrimary(context, 0xFF000000))); if((resId = a.getResourceId(R.styleable.CircularProgressDrawable_cpd_strokeColors, 0)) != 0){ TypedArray ta = context.getResources().obtainTypedArray(resId); int[] colors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++) inAnimDuration(a.getInteger(R.styleable.CircularProgressDrawable_cpd_inAnimDuration, context.getResources().getInteger(android.R.integer.config_mediumAnimTime))); if((resId = a.getResourceId(R.styleable.CircularProgressDrawable_cpd_inStepColors, 0)) != 0){ TypedArray ta = context.getResources().obtainTypedArray(resId); int[] colors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++)
mRootView = (RelativeLayout) inflater.inflate(R.layout.fragment_bloom, container, false); mCircles = getResources().obtainTypedArray(R.array.circles); mRunnable = new CircleSpawn();
@Implementation protected TypedArray obtainTypedArray(int id) throws Resources.NotFoundException { if (isLegacyAssetManager()) { ShadowLegacyAssetManager shadowAssetManager = legacyShadowOf(realResources.getAssets()); TypedArray typedArray = shadowAssetManager.getTypedArrayResource(realResources, id); if (typedArray != null) { return typedArray; } else { throw newNotFoundException(id); } } else { return directlyOn(realResources, Resources.class).obtainTypedArray(id); } }
strokeColors(a.getColor(R.styleable.LinearProgressDrawable_lpd_strokeColor, ThemeUtil.colorPrimary(context, 0xFF000000))); if((resId = a.getResourceId(R.styleable.LinearProgressDrawable_lpd_strokeColors, 0)) != 0){ TypedArray ta = context.getResources().obtainTypedArray(resId); int[] colors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++)
@Test public void withIdReferenceEntry_obtainTypedArray() { TypedArray typedArray = resources.obtainTypedArray(R.array.typed_array_with_resource_id); assertThat(typedArray.length()).isEqualTo(2); assertThat(typedArray.getResourceId(0, 0)).isEqualTo(R.id.id_declared_in_item_tag); assertThat(typedArray.getResourceId(1, 0)).isEqualTo(R.id.id_declared_in_layout); }
getResources().getDisplayMetrics()); final TypedArray circles = getResources().obtainTypedArray(R.array.circles);
@Test public void obtainTypedArray() throws Exception { final TypedArray valuesTypedArray = resources.obtainTypedArray(R.array.typed_array_values); assertThat(valuesTypedArray.getString(0)).isEqualTo("abcdefg"); assertThat(valuesTypedArray.getInt(1, 0)).isEqualTo(3875); .isEqualTo(applyDimension(COMPLEX_UNIT_PT, 18, resources.getDisplayMetrics())); final TypedArray refsTypedArray = resources.obtainTypedArray(R.array.typed_array_references); assertThat(refsTypedArray.getString(0)).isEqualTo("apple"); assertThat(refsTypedArray.getString(1)).isEqualTo("banana");