Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.content.res.Resources;
- import android.content.res.TypedArray;
- import android.os.Build;
- import android.util.AttributeSet;
- import com.smax.appkit.R;
- import java.lang.reflect.Field;
- /**
- * Created by ThoiNguyen on 10/4/2018.
- */
- public class ResourceUtils {
- public static int getIdRes(Context context, String name) {
- return context.getResources().getIdentifier(name, "id", context.getPackageName());
- }
- public static String getStringRes(Context context, String name) {
- return context.getString(context.getResources().getIdentifier(name, "string", context.getPackageName()));
- }
- public static int getLayoutRes(Context context, String name) {
- return context.getResources().getIdentifier(name, "layout", context.getPackageName());
- }
- public static int getDrawableRes(Context context, String name) {
- return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
- }
- public static int getDimenRes(Context context, String name) {
- return context.getResources().getIdentifier(name, "dimen", context.getPackageName());
- }
- public static int getColorRes(Context context, String name) {
- return context.getResources().getIdentifier(name, "color", context.getPackageName());
- }
- public static TypedArray getTypeArray(Context context, String name, AttributeSet attrs, int defStyle) {
- try {
- Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();
- for (Field f : fields2) {
- if (f.getName().equals(name)) {
- int[] styleableArray = (int[]) f.get(null);
- return context.obtainStyledAttributes(attrs, styleableArray, defStyle, 0);
- }
- }
- } catch (Throwable t) {
- t.printStackTrace();
- }
- return null;
- }
- public static TypedArray getTypeArray(Context context, String name, AttributeSet attrs) {
- try {
- Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();
- for (Field f : fields2) {
- if (f.getName().equals(name)) {
- int[] styleableArray = (int[]) f.get(null);
- return context.obtainStyledAttributes(attrs, styleableArray);
- }
- }
- } catch (Throwable t) {
- t.printStackTrace();
- }
- return null;
- }
- public static int getColorByResourceId(Context context, int colorResourceId) {
- try {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- return context.getColor(colorResourceId);
- }
- return context.getResources().getColor(colorResourceId);
- } catch (Resources.NotFoundException e) {
- return colorResourceId;
- }
- }
- public static int getColorByName(Context context, String name) {
- int colorId = 0;
- try {
- Class res = R.color.class;
- Field field = res.getField(name);
- colorId = field.getInt(null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return getColorByResourceId(context, colorId);
- }
- public static int getPlaceholderIconResourceId(Context context) {
- return ResourceUtils.getDrawableRes(context, "smax_all_ic_placeholder_icon");
- }
- public static int getPlaceholderBannerResourceId(Context context) {
- return ResourceUtils.getDrawableRes(context, "smax_all_ic_placeholder_banner");
- }
- }
Add Comment
Please, Sign In to add comment