Android getResources().getDrawable() deprecated API 22

You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading: A) drawables with theme attributes ContextCompat.getDrawable(getActivity(), R.drawable.name); You’ll obtain a styled Drawable as your Activity theme instructs. This is probably what you need. B) drawables without theme attributes ResourcesCompat.getDrawable(getResources(), R.drawable.name, null); You’ll … Read more

How to get a resource id with a known resource name?

If I understood right, this is what you want int drawableResourceId = this.getResources().getIdentifier(“nameOfDrawable”, “drawable”, this.getPackageName()); Where “this” is an Activity, written just to clarify. In case you want a String in strings.xml or an identifier of a UI element, substitute “drawable” int resourceId = this.getResources().getIdentifier(“nameOfResource”, “id”, this.getPackageName()); I warn you, this way of obtaining identifiers … Read more

Android, getting resource ID from string?

@EboMike: I didn’t know that Resources.getIdentifier() existed. In my projects I used the following code to do that: public static int getResId(String resName, Class<?> c) { try { Field idField = c.getDeclaredField(resName); return idField.getInt(idField); } catch (Exception e) { e.printStackTrace(); return -1; } } It would be used like this for getting the value of … Read more