public static final int getColor(Context context, int id) { final int version = Build.VERSION.SDK_INT; if (version >= 23) { return ContextCompatApi23.getColor(context, id); } else { return context.getResources().getColor(id); } }
public static float dpToPx(Context context, float valueInDp) { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics); }
public static boolean isTablet(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; }
public static float pixelsToSp(Context context, float px) { float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; return px/scaledDensity; }
public static float dipToPixels(Context context, float dipValue) { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics); }
public class RegularClass(){ private Context context; public RegularClass(Context current){ this.context = current; } public findResource(){ context.getResources().getXml(R.xml.samplexml); } }
/** * Return a drawable object associated with a particular resource ID. * <p> * Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned * drawable will be styled for the specified Context's theme. * * @param id The desired resource identifier, as generated by the aapt tool. * This integer encodes the package, type, and resource entry. * The value 0 is an invalid identifier. * @return Drawable An object that can be used to draw this resource. */ public static final Drawable getDrawable(Context context, int id) { final int version = Build.VERSION.SDK_INT; if (version >= 21) { return ContextCompatApi21.getDrawable(context, id); } else { return context.getResources().getDrawable(id); } }
public static int convertDip2Pixels(Context context, int dip) { return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics()); }
public boolean isTablet(Context context) { boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE); boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE); return (xlarge || large); }
public static Uri resourceToUri(Context context, int resID) { return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getResources().getResourcePackageName(resID) + '/' + context.getResources().getResourceTypeName(resID) + '/' + context.getResources().getResourceEntryName(resID) ); }
public static boolean isTablet(Context context) { return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; }
public static Drawable getAssetImage(Context context, String filename) throws IOException { AssetManager assets = context.getResources().getAssets(); InputStream buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png"))); Bitmap bitmap = BitmapFactory.decodeStream(buffer); return new BitmapDrawable(context.getResources(), bitmap); }
public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) { final float densityMultiplier = context.getResources().getDisplayMetrics().density; int h= (int) (newHeight*densityMultiplier); int w= (int) (h * photo.getWidth()/((double) photo.getHeight())); photo=Bitmap.createScaledBitmap(photo, w, h, true); return photo; }
import android.content.Context; import android.content.res.Resources; import android.test.ActivityTestCase; public class ActivityTestCaseTest extends ActivityTestCase { public void testFoo() { Context testContext = getInstrumentation().getContext(); Resources testRes = testContext.getResources(); assertNotNull(testRes); assertNotNull(testRes.getString(R.string.app_name)); } }
public class Test extends ActivityTestCase { public void testFoo() { // .. test project environment Context testContext = getInstrumentation().getContext(); Resources testRes = testContext.getResources(); InputStream ts = testRes.openRawResource(R.raw.your_res); assertNotNull(testRes); } }
@NonNull Resources getLocalizedResources(Context context, Locale desiredLocale) { Configuration conf = context.getResources().getConfiguration(); conf = new Configuration(conf); conf.setLocale(desiredLocale); Context localizedContext = context.createConfigurationContext(conf); return localizedContext.getResources(); }
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDivider; public SimpleDividerItemDecoration(Context context) { mDivider = context.getResources().getDrawable(R.drawable.line_divider); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); int top = child.getBottom() + params.bottomMargin; int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } }
Context context = imageView.getContext(); int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName()); imageView.setImageResource(id);
public static int isGooglePlayServicesAvailable(Context context) { PackageManager localPackageManager = context.getPackageManager(); try { Resources localResources = context.getResources(); localResources.getString(R.string.common_google_play_services_unknown_issue); } catch (Throwable localThrowable1) { Log.e("GooglePlayServicesUtil", "The Google Play services resources were not found. " + "Check your project configuration to ensure that the resources are included."); } ....
public static Animator loadAnimator(Context context, @AnimatorRes int id) throws NotFoundException { return loadAnimator(context.getResources(), context.getTheme(), id); }