Android Utililty Methods Context Get

List of utility methods to do Context Get

Description

The list of methods to do Context Get are organized into topic(s).

Method

StringgetString(Context context, String key, String defValue)
get String
return getDefaultSharedPreferences(context)
        .getString(key, defValue);
StringgetString(Context context, int resourceId)
get String
if (context != null) {
    return context.getResources().getString(resourceId);
return null;
StringgetStringFromResource(int resource, Context context)
get String From Resource
InputStream is = context.getResources().openRawResource(resource);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
String contents = "";
try {
    while ((readLine = br.readLine()) != null) {
        contents += readLine + "\n";
    is.close();
    br.close();
} catch (IOException e) {
    e.printStackTrace();
return contents;
intgetStringResourceByName(String aString, Context context)
get String Resource By Name
String packageName = context.getPackageName();
int resId = context.getResources().getIdentifier(aString, "string",
        packageName);
return resId;
FilegetSystemDiskCacheDir(Context context)
get System Disk Cache Dir
final String cachePath = Environment.MEDIA_MOUNTED
        .equals(Environment.getExternalStorageState()) ? getExternalCacheDir(
        context).getPath()
        : context.getCacheDir().getPath();
return new File(cachePath);
TypefacegetTCBoldFont(Context context)
get TC Bold Font
Typeface tf = Typeface.createFromAsset(context.getAssets(),
        fontPath_bold);
return tf;
TypefacegetTCFont(Context context)
get TC Font
Typeface tf = Typeface.createFromAsset(context.getAssets(),
        fontPath);
return tf;
TypefacegetTCItalicFont(Context context)
get TC Italic Font
Typeface tf = Typeface.createFromAsset(context.getAssets(),
        fontPath_italic);
return tf;
FilegetTempFile(Context context)
get Temp File
final File path = new File(
        Environment.getExternalStorageDirectory(),
        context.getPackageName());
if (!path.exists()) {
    path.mkdir();
return new File(path, "image.tmp");
StringgetTimestamp(Context context)
Return a timestamp
String timestamp = "unknown";
Date now = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat
        .getDateFormat(context);
java.text.DateFormat timeFormat = android.text.format.DateFormat
        .getTimeFormat(context);
if (dateFormat != null && timeFormat != null) {
    timestamp = dateFormat.format(now) + ' '
...