Example usage for android.content.res Resources getStringArray

List of usage examples for android.content.res Resources getStringArray

Introduction

In this page you can find the example usage for android.content.res Resources getStringArray.

Prototype

@NonNull
public String[] getStringArray(@ArrayRes int id) throws NotFoundException 

Source Link

Document

Return the string array associated with a particular resource ID.

Usage

From source file:Main.java

public static String[] getStringArray(Context context, int res) {
    Resources resource = context.getResources();
    return resource.getStringArray(res);
}

From source file:Main.java

/**
 * returns an arrayList of codes that correspond to the array positions with
 * true values/*from   w ww .j a va2 s. co  m*/
 * 
 * @return
 */
public static String[] getSelectedCodes(Context context, boolean[] selectedItems, int codeResourceId) {
    ArrayList<String> codes = new ArrayList<String>();
    Resources res = context.getResources();
    String[] allCodes = res.getStringArray(codeResourceId);
    for (int i = 0; i < selectedItems.length; i++) {
        if (selectedItems[i]) {
            codes.add(allCodes[i]);
        }
    }
    return codes.toArray(new String[codes.size()]);
}

From source file:Main.java

/**
 * returns an arrayList of language codes that are active.
 * //  w ww .  j  av  a 2  s  .  c  om
 * @param bs
 * @return
 */
public static String[] getSelectedLangCodes(Context context, int[] indexes, boolean[] selectedItems,
        int codeResourceId) {
    ArrayList<String> codes = new ArrayList<String>();
    Resources res = context.getResources();
    String[] allCodes = res.getStringArray(codeResourceId);
    for (int i = 0; i < indexes.length; i++) {
        if (selectedItems[i]) {
            codes.add(allCodes[indexes[i]]);
        }
    }
    return codes.toArray(new String[codes.size()]);
}

From source file:Main.java

/**
 * Get String array from Extra or from Meta-data through resources.
 * /*from  w ww.j  a  v a 2  s  .c  om*/
 * @param packagename
 * @param intent
 * @param extra
 * @param metadata
 */
public static String[] getStringArrayExtraOrMetadata(final Context context, final String packagename,
        final Intent intent, final String extra, final String metadata) {
    if (intent.hasExtra(extra) && intent.getStringArrayExtra(extra) != null) {
        return intent.getStringArrayExtra(extra);
    } else {
        //Try meta data of package
        Bundle md = null;
        try {
            md = context.getPackageManager().getApplicationInfo(packagename,
                    PackageManager.GET_META_DATA).metaData;
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }

        if (md != null) {
            String[] array = null;
            try {
                int id = md.getInt(metadata);
                Resources resources = context.getPackageManager().getResourcesForApplication(packagename);
                array = resources.getStringArray(id);
            } catch (NameNotFoundException e) {
                Log.e(TAG, "Package name not found ", e);
            } catch (NumberFormatException e) {
                Log.e(TAG, "Metadata not valid id.", e);
            } catch (Resources.NotFoundException e) {
                Log.e(TAG, "Resource not found.", e);
            }

            if (array != null) {
                return array;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}

From source file:paulscode.android.mupen64plusae.preference.PrefUtil.java

public static void validateListPreference(Resources res, SharedPreferences prefs, String key, int defaultResId,
        int arrayResId) {
    String value = prefs.getString(key, null);
    String defValue = res.getString(defaultResId);
    String[] validValues = res.getStringArray(arrayResId);
    if (!ArrayUtils.contains(validValues, value)) {
        prefs.edit().putString(key, defValue).commit();
    }//from  w  w w . j  a v  a  2  s.  c  om
}

From source file:com.lottodroid.widgets.wikiarticle.WikiarticleHelper.java

/**
 * Build the page title where it is located today's featured article, like 
 * "Wikipedia:Today's_featured_article/March_21,_2009". It uses the actual date
 * //from w  w w.  j a  v  a  2  s.c o m
 * @param context The context of the application
 * @return Today's page resource
 */
public static String buildTodayPageTitle(Context context) {
    // Pick out month names from resources
    Resources res = context.getResources();
    String[] monthNames = res.getStringArray(R.array.month_names);

    // Find current month and day
    Time today = new Time();
    today.setToNow();

    // Build today's page title, like "Wikipedia:Today's_featured_article/March_21,_2009"
    String pageName = res.getString(R.string.template_wotd_title, monthNames[today.month], today.monthDay,
            today.year);

    return pageName;
}

From source file:tw.com.ksmt.cloud.libs.Utils.java

public static String getArrayKey(Context context, int keyId, int valId, String target) {
    String result = "";
    Resources res = context.getResources();
    String[] keys = res.getStringArray(keyId);
    String[] vals = res.getStringArray(valId);
    for (int i = 0; i < vals.length; i++) {
        if (vals[i].equals(target) && keys[i] != null) {
            return keys[i];
        }/* w ww.  ja v  a 2 s. co  m*/
    }
    return result;
}

From source file:com.drc.wiktionary.ExtendedWikiHelper.java

/**
 * Get the WOTD for a random date. Note there are only 365 possible
 * responses for any given day. Random future dates wrap back to the
 * /*  w  w w .j a v  a  2  s.  c om*/
 * @return Random dictionary word, or null if no valid word was found.
 * @throws ApiException
 *             If any connection or server error occurs.
 * @throws ParseException
 *             If there are problems parsing the response.
 */
public static String getRandomWord(Context context) throws ApiException, ParseException {
    Resources res = context.getResources();
    String[] monthNames = res.getStringArray(R.array.month_names);

    // Keep trying a few times until we find a valid word
    int tries = 0;
    while (tries++ < RANDOM_TRIES) {
        // pick random day of year, 1-365
        int rDoy = RANDOM.nextInt(364) + 1;
        Calendar c = Calendar.getInstance();
        // set cal's day of year to random day of year
        // the corresponding random month and day of month
        // are used below to build the page title
        c.set(Calendar.DAY_OF_YEAR, rDoy);

        // random month, day, year
        String rMonth = monthNames[c.get(Calendar.MONTH)];
        int rDay = c.get(Calendar.DAY_OF_MONTH);
        int rYear = c.get(Calendar.YEAR) - RANDOM.nextInt(4);

        // Build the page title for the archive month, year
        String pageName = res.getString(R.string.template_wotd_archive_title, rYear, rMonth);
        String pageContent = null;

        try {
            // query for the archive page
            // archive page contains all words for that year / month
            SimpleWikiHelper.prepareUserAgent(context);
            pageContent = SimpleWikiHelper.getPageContent(pageName, false);
        } catch (ApiException e) {
            Log.e("WordWidget", "Couldn't contact API", e);
        } catch (ParseException e) {
            Log.e("WordWidget", "Couldn't parse API response", e);
        }

        Matcher matcher = null;
        if (pageContent != null) {
            matcher = Pattern.compile(WordWidget.WOTD_PATTERN).matcher(pageContent);
        }
        if (matcher != null) {
            // iterate to randomly selected day
            for (int i = 0; i < rDay && matcher.find(); i++) {
                if (i != rDay - 1) {
                    continue;
                }
                String foundWord = matcher.group(1);
                if (foundWord != null && !sInvalidWord.matcher(foundWord).find()) {
                    return foundWord;
                }
            }
        }
    }

    // No valid word found in number of tries, so return null
    return null;
}

From source file:com.appsimobile.appsii.timezonepicker.TimeZoneData.java

private static void populateDisplayNameOverrides(SimpleArrayMap<String, TimeZoneInfo> timeZonesById,
        Resources resources) {

    String[] ids = resources.getStringArray(R.array.timezone_rename_ids);
    String[] labels = resources.getStringArray(R.array.timezone_rename_labels);

    int length = ids.length;
    if (ids.length != labels.length) {
        Log.e(TAG, "timezone_rename_ids len=" + ids.length + " timezone_rename_labels len=" + labels.length);
        length = Math.min(ids.length, labels.length);
    }/*  w ww .  j  av a  2 s . c o  m*/

    for (int i = 0; i < length; i++) {
        TimeZoneInfo tzi = timeZonesById.get(ids[i]);
        if (tzi != null) {
            tzi.mDisplayName = labels[i];
        } else {
            Log.e(TAG, "Could not find timezone with label: " + labels[i]);
        }
    }
}

From source file:com.github.michalbednarski.intentslab.editor.IntentGeneralFragment.java

static String[] getMethodNamesArray(Resources res, int componentType) {
    return res.getStringArray(componentType == IntentEditorConstants.ACTIVITY ? R.array.activitymethods
            : componentType == IntentEditorConstants.BROADCAST ? R.array.broadcastmethods
                    : componentType == IntentEditorConstants.SERVICE ? R.array.servicemethods : 0);
}