@SuppressLint("UseSparseArrays") @SuppressWarnings("ResourceType") public static Map<Integer, Pair<String, String>> obtainBadgeMap(Context context, @ArrayRes int id) { TypedArray badgeArray = context.getResources().obtainTypedArray(id); Map<Integer, Pair<String, String>> badgeMap = new HashMap<>(); for (int i = 0; i < badgeArray.length(); i++) { int resId = badgeArray.getResourceId(i, -1); if (resId != -1) { TypedArray array = context.getResources().obtainTypedArray(resId); badgeMap.put(resId, new Pair<>(array.getString(0), array.getString(1))); array.recycle(); } } badgeArray.recycle(); return badgeMap; }
@NonNull private static String[] getEmoji(Context ctx, @ArrayRes int res) { String[] rawStrings = ctx.getResources().getStringArray(res); String[] emoji = new String[rawStrings.length]; int i = 0; for (String codePoint : rawStrings) { String[] bytes = codePoint.split(","); int[] codePoints = new int[bytes.length]; int j = 0; for (String b : bytes) { codePoints[j] = Integer.valueOf(b, 16); } emoji[i] = new String(codePoints, 0, codePoints.length); i++; } return emoji; }
@NonNull public String[] getStringArray(@ArrayRes int id) throws NotFoundException { Map<Integer, String> idNameMap = getArrayIdTable(); Map<String, List<String>> stringArrayMap = getResourceStringArrayMap(); if (idNameMap.containsKey(id)) { String name = idNameMap.get(id); if (stringArrayMap.containsKey(name)) { List<String> stringList = stringArrayMap.get(name); return stringList.toArray(new String[0]); } } throw new Resources.NotFoundException("String array resource ID #0x" + Integer.toHexString(id)); }
@NonNull public int[] getIntArray(@ArrayRes int id) throws NotFoundException { Map<Integer, String> idNameMap = getArrayIdTable(); Map<String, List<Integer>> intArrayMap = getResourceIntArrayMap(); if (idNameMap.containsKey(id)) { String name = idNameMap.get(id); if (intArrayMap.containsKey(name)) { List<Integer> intList = intArrayMap.get(name); int[] intArray = new int[intList.size()]; for (int i = 0; i < intList.size(); i++) { intArray[i] = intList.get(i); } return intArray; } } return new int[0]; }
public void showColorPicker(View view){ @ArrayRes int pallet = new int[]{ SimpleColorDialog.MATERIAL_COLOR_PALLET, // default if no pallet explicitly set SimpleColorDialog.MATERIAL_COLOR_PALLET_DARK, SimpleColorDialog.MATERIAL_COLOR_PALLET_LIGHT, SimpleColorDialog.BEIGE_COLOR_PALLET, SimpleColorDialog.COLORFUL_COLOR_PALLET }[counter++ % 5]; SimpleColorDialog.build() .title(R.string.pick_a_color) .colors(this, pallet) .colorPreset(color) .allowCustom(true) .show(this, COLOR_DIALOG); /** Results: {@link MainActivity#onResult} **/ }
/** * Converts 3 resource arrays to ArrayList<SelectableColor> of colors. Colors can be sorted by name at runtime, note that colors will be sorted in language displays to user. * Note: all arrays must have equal lengths. * * @param context current context * @param sortByName if true colors will be sorted by name, otherwise colors will be left as they are * @param idsArray array resource id to use as colors ids * @param namesArray array resource id to use as colors names * @param colorsArray array resource id to use as colors, well color values * @return colors ArrayList */ public static ArrayList<SelectableColor> convertResourceArraysToColorsArrayList(Context context, boolean sortByName, @ArrayRes int idsArray, @ArrayRes int namesArray, @ArrayRes int colorsArray) { //get and check arrays String[] ids = context.getResources().getStringArray(idsArray); int[] colors = context.getResources().getIntArray(colorsArray); String[] names = context.getResources().getStringArray(namesArray); if (ids.length != colors.length && ids.length != names.length) { Log.e(LOG_TAG, "convertResourceArraysToColorsArrayList(): Arrays must have equals lengths!"); return null; } //create ArrayList ArrayList<SelectableColor> result = new ArrayList<>(); for (int i = 0; i < ids.length; i++) { result.add(new SelectableColor(ids[i], names[i], colors[i])); } //sort by names if (sortByName) { Collections.sort(result, new SelectableItemNameComparator<SelectableColor>()); } return result; }
/** * Converts 3 resource arrays to ArrayList<SelectableIcons> of icons. Icons can be sorted by name at runtime, note that icons will be sorted in language displays to user. * Note: all arrays must have equal lengths. * * @param context current context * @param sortByName if true colors will be sorted by name, otherwise colors will be left as they are * @param idsArray array resource id to use as icons ids * @param namesArray array resource id to use as icons names * @param drawablesArray array resource id to use as icons drawables * @return icons ArrayList */ public static ArrayList<SelectableIcon> convertResourceArraysToIconsArrayList(Context context, boolean sortByName, @ArrayRes int idsArray, @ArrayRes int namesArray, @ArrayRes int drawablesArray) { //get and check arrays String[] ids = context.getResources().getStringArray(idsArray); int[] drawables = context.getResources().getIntArray(drawablesArray); String[] names = context.getResources().getStringArray(namesArray); if (ids.length != drawables.length && ids.length != names.length) { Log.e(LOG_TAG, "convertResourceArraysToIconsArrayList(): Arrays must have equals lengths!"); return null; } //create ArrayList ArrayList<SelectableIcon> result = new ArrayList<>(); for (int i = 0; i < ids.length; i++) { result.add(new SelectableIcon(ids[i], names[i], drawables[i])); } //sort by names if (sortByName) { Collections.sort(result, new SelectableItemNameComparator<SelectableIcon>()); } return result; }
public static void show(@NonNull Context context, @StringRes int title, @ArrayRes int names, @NonNull final OnSelection callback) { final WeakReference<OnSelection> callbackRef = new WeakReference<>(callback); AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(title); builder.setItems(names, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { OnSelection callbackSafe = callbackRef.get(); if (callbackSafe != null) { callback.onSelection(which); } } }); builder.setNegativeButton(android.R.string.cancel, null); builder.create().show(); }
public static int[] extractColorArray(@ArrayRes int arrayId, Context context) { String[] choicesString = context.getResources().getStringArray(arrayId); int[] choicesInt = context.getResources().getIntArray(arrayId); // If user uses color reference(i.e. @color/color_choice) in the array, // the choicesString contains null values. We use the choicesInt in such case. boolean isStringArray = choicesString[0] != null; int length = isStringArray ? choicesString.length : choicesInt.length; int[] colorChoices = new int[length]; for (int i = 0; i < length; i++) { colorChoices[i] = isStringArray ? Color.parseColor(choicesString[i]) : choicesInt[i]; } return colorChoices; }
private void setSpinnerContents(Spinner spinner, @ArrayRes int spinnerContents, int selectedIndex, int offset, @ArrayRes int spinnerIcons) { List<EventTypeItem> items = new ArrayList<>(); final String[] arrTexts = getResources().getStringArray(spinnerContents); final TypedArray arrIcons = spinnerIcons > 0 ? getResources().obtainTypedArray(spinnerIcons) : null; if (offset >= arrTexts.length) { throw new IllegalArgumentException("Offset >= Array.length"); } else if (offset < 0) { throw new IllegalArgumentException("Offset < 0"); } for (int i = offset; i < arrTexts.length; i++) { //noinspection ResourceType items.add(new EventTypeItem(i, arrTexts[i], (null != arrIcons) ? arrIcons.getResourceId(i, 0) : 0)); } if (null != arrIcons) { arrIcons.recycle(); } final EventTypeArrayAdapter arrayAdapter = new EventTypeArrayAdapter(CreateEditEventActivity.this, items); spinner.setAdapter(arrayAdapter); spinner.setSelection(selectedIndex - offset); }
@Override public void setSummary(@ArrayRes int summaryResId) { try { setSummary(getContext().getResources().getStringArray(summaryResId)); } catch (Exception e) { super.setSummary(summaryResId); } }
public static int[] getColorArray(@NonNull Context context, @ArrayRes int array) { if (array == 0) return null; TypedArray ta = context.getResources().obtainTypedArray(array); int[] colors = new int[ta.length()]; for (int i = 0; i < ta.length(); i++) colors[i] = ta.getColor(i, 0); ta.recycle(); return colors; }
private LocalResourceSimpleAdapter(final Context context, @ArrayRes int arrayId, boolean lazy) { mSrcArray = context.getResources().getStringArray(arrayId); mLazy = lazy; mUris = new Uri[mSrcArray.length]; if (!lazy) { for (int i = 0; i < mSrcArray.length; i++) { mUris[i] = Uri.parse(mSrcArray[i]); } } }
@NonNull @Override public CharSequence[] getTextArray(@ArrayRes int id) throws NotFoundException { int realId = getCorrespondResId(id); if (realId > 0) { return mSkinResources.getTextArray(id); } return super.getTextArray(id); }
@NonNull @Override public String[] getStringArray(@ArrayRes int id) throws NotFoundException { int realId = getCorrespondResId(id); if (realId > 0) { return mSkinResources.getStringArray(realId); } return super.getStringArray(id); }
@NonNull @Override public int[] getIntArray(@ArrayRes int id) throws NotFoundException { int realId = getCorrespondResId(id); if (realId > 0) { return mSkinResources.getIntArray(realId); } return super.getIntArray(id); }
@NonNull @Override public TypedArray obtainTypedArray(@ArrayRes int id) throws NotFoundException { int realId = getCorrespondResId(id); if (realId > 0) { return mSkinResources.obtainTypedArray(realId); } return super.obtainTypedArray(id); }
/** * Gets the string array resource based on the string array resource ID(List) * * @param context context * @param resId resId * @return string array resource */ public static List<String> getStringList(Context context, @ArrayRes int resId) { List<String> strings = new ArrayList<String>(); String[] strs = getStringArray(context, resId); for (String string : strs) { strings.add(string); } return strings; }
public static int[] getResourceIds(Context c, @ArrayRes int array) { final TypedArray typedArray = c.getResources().obtainTypedArray(array); final int[] resourceIds = new int[typedArray.length()]; for (int i = 0; i < typedArray.length(); i++) { resourceIds[i] = typedArray.getResourceId(i, 0); } typedArray.recycle(); return resourceIds; }
public static SortListDialogFragment newInstance(@ArrayRes int sortingChoices, int currentSortIndex) { SortListDialogFragment frag = new SortListDialogFragment(); Bundle args = new Bundle(); args.putInt(KEY_SORT_ARRAY_RES_ID, sortingChoices); args.putInt(KEY_CURRENT_SORT_INDEX, currentSortIndex); frag.setArguments(args); return frag; }
@ColorInt static int[] getColors(Context context, TypedArray array, @StyleableRes int attr, @ArrayRes int defaultColorsRes) { return context.getResources().getIntArray( array.getResourceId(attr, defaultColorsRes)); }
/** * Set layer colors from array resource * * @param arrayId array resource */ public T setLayerColors(@ArrayRes int arrayId) { TypedArray colorsArray = context.getResources().obtainTypedArray(arrayId); int[] colors = new int[colorsArray.length()]; for (int i = 0; i < colorsArray.length(); i++) { colors[i] = colorsArray.getColor(i, Color.TRANSPARENT); } colorsArray.recycle(); return setLayerColors(colors); }
public static MaterialDialog getChooseStartUpPageDialog(Activity activity, String currentlySelectedPageTitle, MaterialDialog.ListCallbackSingleChoice listCallbackSingleChoice) { final Settings settings = new Settings(activity); @ArrayRes int arrayRessource = settings.isLoggedIn() ? R.array.StartupPages : R.array.StartupPagesNoLogin; int indexOfPage = 0; String[] androidStrings = activity.getResources().getStringArray(arrayRessource); for (int i = 0; i < androidStrings.length; i++) { if (androidStrings[i].equals(currentlySelectedPageTitle)) { indexOfPage = i; break; } } return getBaseThemedDialog(activity) .title(R.string.gen_start_page) .items(arrayRessource) .itemsCallbackSingleChoice(indexOfPage, listCallbackSingleChoice) .positiveText(R.string.ok) .negativeText(R.string.cancel) .onNegative(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { dialog.dismiss(); } }) .build(); }
public static MaterialDialog getChooseChatSizeDialog(Activity activity, @StringRes int dialogTitle, @ArrayRes int array, int currentSize, MaterialDialog.ListCallbackSingleChoice callbackSingleChoice) { int indexOfPage = currentSize - 1; String[] sizeTitles = activity.getResources().getStringArray(array); return getBaseThemedDialog(activity) .title(dialogTitle) .itemsCallbackSingleChoice(indexOfPage, callbackSingleChoice) .items(sizeTitles) .positiveText(R.string.done) .build(); }
/** * 获取返回Drawable Array,解决直接使用getIntArray获取Drawable数组时,所有item都为0的问题. * * @param id resource id * @return Drawable Array */ public static Drawable[] getDrawableArray(@ArrayRes int id) { TypedArray typedArray = getTypedArray(id); //获取数量需要用这样的方法, TypedArray.getIndexCount() 获取的一直是0. int count = getTextArray(id).length; Drawable[] drawables = new Drawable[count]; for(int i = 0; i < drawables.length; i++) { drawables[i] = typedArray.getDrawable(i); } typedArray.recycle(); return drawables; }
/** * 获取返回Drawable Id Array,因为主要原因是使用 {@link #getDrawableArray(int)} 时,用Glide直接加载 * Drawable会报异常,这里提供一个获取drawable id array的方法解决这个问题. * * @param id Array id * @return Drawable id Array */ public static int[] getDrawableIdArray(@ArrayRes int id) { TypedArray typedArray = getTypedArray(id); int length = getTextArray(id).length; int[] ids = new int[length]; for(int i = 0; i < ids.length; i++) { ids[i] = typedArray.getResourceId(i, 0); } typedArray.recycle(); return ids; }
/** * Sets the texts to be shuffled using a string array resource * * @param texts The string array resource to use for the texts */ public void setTexts(@ArrayRes int texts) { isSingleText = false; if (getResources().getStringArray(texts).length < 1) throw new IllegalArgumentException("There must be at least one text"); else { this.texts = getResources().getStringArray(texts); stopAnimation(); position = 0; startAnimation(); } }
public static int[] getResourcesIdArray(@ArrayRes int id) { TypedArray typedArray = mApplication.getResources().obtainTypedArray(id); int length = typedArray.length(); int[] resArray = new int[length]; for (int i = 0; i < length; i++) { int resourceId = typedArray.getResourceId(i, 0); resArray[i] = resourceId; } typedArray.recycle(); return resArray; }
public static Integer[] getResourcesIdIntegerArray(@ArrayRes int id) { TypedArray typedArray = mApplication.getResources().obtainTypedArray(id); int length = typedArray.length(); Integer[] resArray = new Integer[length]; for (int i = 0; i < length; i++) { int resourceId = typedArray.getResourceId(i, 0); resArray[i] = resourceId; } typedArray.recycle(); return resArray; }
/** * load any object array from known resource.eg: * @param context the context * @param <T> the object type * @return the object list */ public static <T> List<T> loadObjectArray(Context context, @ArrayRes int arrayId, ObjectFactory<T> factory){ // Get the array of objects from the `warm_up_descs` array final TypedArray statuses = context.getResources().obtainTypedArray(arrayId); final List<T> categoryList = new ArrayList<>(); TypedArray rawStatus = null; try { final int arrSize = statuses.length(); for (int i = 0; i < arrSize; i++) { int statusId = statuses.getResourceId(i, EMPTY_RES_ID); // Get the properties of one object rawStatus = context.getResources().obtainTypedArray(statusId); T[] ts = factory.create(rawStatus); if(!Predicates.isEmpty(ts)){ for(T t: ts){ categoryList.add(t); } } rawStatus.recycle(); rawStatus = null; } } finally { statuses.recycle(); if (rawStatus != null) { rawStatus.recycle(); } } return categoryList; }
private ArrayList<Stat> buildLabelList(@ArrayRes int stringArrayRes) { ArrayList<Stat> statList = new ArrayList<>(); // Used to alternate background colors in foreach int row = 0; for (String label : getResources().getStringArray(stringArrayRes)) { statList.add(new Stat(label, row)); row++; } return statList; }
public static boolean searchOnArray(@ArrayRes int l, String elem, Context context) { Resources res = context.getResources(); String[] array = res.getStringArray(l); for (String s : array) { if (s.equals(elem)) { return true; } } return false; }
public static int[] obtainColorArray(Resources res, @ArrayRes int resId) { TypedArray array = res.obtainTypedArray(resId); final int length = array.length(); int[] resIds = new int[length]; for(int i = 0; i < length; ++i) resIds[i] = array.getColor(i, Color.BLACK/* default color*/); array.recycle(); return resIds; }
/** * Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. * * @param itemsId the resource id of an array i.e. R.array.foo * @param checkedItem specifies which item is checked. If -1 no items are checked. * @param listener notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog. * @return This */ public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem, final DialogInterface.OnClickListener listener) { builder.items(itemsId); builder.itemsCallbackSingleChoice(checkedItem, new TalkDialog.ListCallbackSingleChoice() { @Override public boolean onSelection(TalkDialog dialog, View itemView, int which, CharSequence text) { listener.onClick(dialog, which); return true; } }); return this; }