Context context = imageView.getContext(); int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName()); imageView.setImageResource(id);
public static int getDrawable(Context context, String name) { Assert.assertNotNull(context); Assert.assertNotNull(name); return context.getResources().getIdentifier(name, "drawable", context.getPackageName()); }
/** * Returns Identifier of String into it's ID as defined in R.java file. * @param pContext * @param pString defnied in Strings.xml resource name e.g: action_item_help * @return */ public static int getStringIdentifier(Context pContext, String pString){ return pContext.getResources().getIdentifier(pString, "string", pContext.getPackageName()); }
public static int getStringIdentifier(Context context, String name) { return context.getResources().getIdentifier(name, "string", context.getPackageName()); }
Context c = getApplicationContext(); int id = c.getResources().getIdentifier("drawable/"+"abcd_36", null, c.getPackageName()); ((ImageView)v.findViewById(R.id.your_image_on_your_layout)).setImageResource(id);
public static String byIdName(Context context, String name) { Resources res = context.getResources(); return res.getString(res.getIdentifier(name, "string", context.getPackageName())); }
public static int getImageId(Context context, String imageName) { return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName()); }
private Context mContext; public CustomImageListAdapter(Context context, Cursor cursor) { super(context, cursor); inflater = LayoutInflater.from(context); mContext = context; } // Other code ... // Now call getResources() on the Context reference (and getPackageName()) String mDrawableName = "myImageName"; int resID = mContext.getResources().getIdentifier(mDrawableName , "drawable", mContext.getPackageName());
// Image View Object mIv = (ImageView) findViewById(R.id.xidIma); // create context Object for to Fetch image from resourse Context mContext=getApplicationContext(); // getResources().getIdentifier("image_name","res_folder_name", package_name); // find out below example int i = mContext.getResources().getIdentifier("ic_launcher","raw", mContext.getPackageName()); // now we will get contsant id for that image mIv.setBackgroundResource(i);
int resID = getResources().getIdentifier(resourceName, "id", getPackageName()); ImageView im = (ImageView) findViewById(resID); Context context = im.getContext(); int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName()); im.setImageResource(id);
public static int getDrawable(Context context, String name) { Assert.assertNotNull(context); Assert.assertNotNull(name); return context.getResources().getIdentifier(name, "drawable", context.getPackageName()); }
int resID = getResources().getIdentifier(resourceName, "id", getPackageName()); ImageView im = (ImageView) findViewById(resID); Context context = im.getContext(); int id = context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName()); im.setImageResource(id);
private static int getId(String id) { Context targetContext = InstrumentationRegistry.getTargetContext(); String packageName = targetContext.getPackageName(); return targetContext.getResources().getIdentifier(id, "id", packageName); }
public static int getVideo(Context context, String name) { Assert.assertNotNull(context); Assert.assertNotNull(name); return context.getResources().getIdentifier(name, "raw", context.getPackageName()); }
public static int getImageResourceIDFromName(String tag, Context context) { int identifier = context.getResources().getIdentifier (tag,"string",context.getPackageName()); return identifier; }
public static String getStringResource(Context context, String thingie) { try { String[] split = thingie.split("/"); String pack = split[0].replace("@", ""); String name = split[1]; int id = context.getResources().getIdentifier(name, pack, context.getPackageName()); return context.getResources().getString(id); } catch (Exception e) { return thingie; } }
public static Drawable getDrawable(Context context, String resource_name){ try{ int resId = context.getResources().getIdentifier(resource_name, "drawable", context.getPackageName()); if(resId != 0){ return context.getResources().getDrawable(resId); } }catch(Exception e){ Log.e(TAG,"getDrawable - resource_name: "+resource_name); e.printStackTrace(); } return null; }
public static int getResourceId(Context context, String name, String resourceType) { return context.getResources().getIdentifier(name, resourceType, context.getPackageName()); } int iconId = getResourceId(Activity.this, image, "drawable"); ImageView categoryIcon = (ImageView) v.findViewById(R.id.category_icon); categoryIcon.setImageResource(iconId);
private MAtt SetField(Context mContext, String name){ MAtt aField = new MAtt(); //Create object int id = mContext.getResources().getIdentifier(name, "string", mContext.getPackageName()); aField.name=mContext.getString(id); return aField; }
private static int getThemeAccentColor(Context context) { int colorAttr; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { colorAttr = android.R.attr.colorAccent; } else { //Get colorAccent defined for AppCompat colorAttr = context.getResources().getIdentifier("colorAccent", "attr", context.getPackageName()); } TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(colorAttr, outValue, true); return outValue.data; }