Example usage for android.content Context getString

List of usage examples for android.content Context getString

Introduction

In this page you can find the example usage for android.content Context getString.

Prototype

@NonNull
public final String getString(@StringRes int resId) 

Source Link

Document

Returns a localized string from the application's package's default string table.

Usage

From source file:Main.java

static String getString(Context context, int resourceID) {
    String stringResult = "Instagram";
    try {// w w  w . j ava2s. c o m
        Context packageContext = context.createPackageContext("com.ihelp101.instagram",
                Context.CONTEXT_IGNORE_SECURITY);
        stringResult = packageContext.getString(resourceID);
    } catch (Exception e) {

    }
    return stringResult;
}

From source file:Main.java

public static String getStringFromXml(Context context, String name) {
    if (context != null) {
        int id = context.getResources().getIdentifier(name, "string", context.getPackageName());
        String text = context.getString(id);
        return text;
    } else {/*w w  w .  j a v  a 2s.c om*/
        return "";
    }
}

From source file:Main.java

/**
 * Get application name.//from   w  w w.j  av  a 2  s  .co  m
 * 
 * @return
 */
public static String getApplicationName(Context context) {
    String name = "?";
    try {
        PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        name = context.getString(pi.applicationInfo.labelRes);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Package name not found", e);
    }
    ;
    return name;
}

From source file:fr.shywim.antoinedaniel.utils.GcmUtils.java

public static void sendRegistrationIdToBackend(Context context, String regid, int version) {
    try {//from   ww  w  .ja  va  2  s  .c om
        HttpPost post = new HttpPost(context.getString(R.string.api_register));
        List<NameValuePair> params = new ArrayList<>(2);
        params.add(new BasicNameValuePair("regid", regid));
        params.add(new BasicNameValuePair("version", Integer.toString(version)));
        post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(post);

        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode == 200 || responseCode == 201) {
            context.getSharedPreferences(AppConstants.GOOGLE_PREFS, Context.MODE_PRIVATE).edit()
                    .putBoolean(AppConstants.GOOGLE_GCM_REGISTERED, true).apply();
        }
    } catch (IOException e) {
        Crashlytics.logException(e);
    }
}

From source file:Main.java

public static boolean getPrefBool(Context context, int keyId, int defaultValueId) {

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String key = context.getString(keyId);
    boolean defaultValue = false;
    try {//from w ww . j  a v a 2  s . c om
        defaultValue = Boolean.parseBoolean(context.getString(defaultValueId));
    } catch (Exception e) { /* don't care */
    }

    return sharedPreferences.getBoolean(key, defaultValue);
}

From source file:Main.java

public static String getPrefString(Context context, int keyId, int defaultValueId) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String key = context.getString(keyId);
    String defaultValue = context.getString(defaultValueId);

    return sharedPreferences.getString(key, defaultValue);
}

From source file:Main.java

/**
 * Returns the Google API CLIENT SECRET to use in API calls
 * @param applicationContext/* w ww.j a v  a  2  s.co m*/
 * @return
 */
private static String getClientSecret(Context applicationContext) {
    int RClientSecret = applicationContext.getResources().getIdentifier("gdocs_clientsecret", "string",
            applicationContext.getPackageName());
    return applicationContext.getString(RClientSecret);
}

From source file:Main.java

public static String getStringByName(String name, Context context) {
    try {/*from  w  w  w.  j a  v  a  2 s . c om*/
        Field field = Class.forName("com.poetic.emotion.R$string").getField(name);
        int drawableRes = field.getInt(field);
        return context.getString(drawableRes);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:ru.gkpromtech.exhibition.organizations.OrganizationFilesDownloader.java

public static void download(final Context context, final Organization organization) {
    final ProgressDialog dialog = ProgressDialog.show(context, context.getString(R.string.please_wait),
            context.getString(R.string.loading_materials_info), false);

    ServiceClient.getJson("organizations/files?id=" + organization.id, new Callback<JsonNode>() {
        @Override//from w w w.ja  va2  s .c  om
        public void onSuccess(JsonNode data) throws Exception {
            dialog.dismiss();

            String url = data.get("url").asText();
            int size = data.get("size").asInt();
            confirmAndDownload(context, organization, url, size);
        }

        @Override
        public void onError(Throwable exception) {
            dialog.dismiss();
            Toast.makeText(context, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:Main.java

public static String parseString(Context context, Object obj) {
    if (obj != null) {
        if (obj instanceof String) {
            return obj.toString();
        } else if (obj instanceof Integer) {
            return context.getString((Integer) obj);
        }/*from  w w w .ja  v a  2 s.c o  m*/
    }
    return null;
}