Guest User

Untitled

a guest
Nov 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.res.Resources;
  3. import android.content.res.TypedArray;
  4. import android.os.Build;
  5. import android.util.AttributeSet;
  6.  
  7. import com.smax.appkit.R;
  8.  
  9. import java.lang.reflect.Field;
  10.  
  11. /**
  12. * Created by ThoiNguyen on 10/4/2018.
  13. */
  14. public class ResourceUtils {
  15. public static int getIdRes(Context context, String name) {
  16. return context.getResources().getIdentifier(name, "id", context.getPackageName());
  17. }
  18.  
  19. public static String getStringRes(Context context, String name) {
  20. return context.getString(context.getResources().getIdentifier(name, "string", context.getPackageName()));
  21. }
  22.  
  23. public static int getLayoutRes(Context context, String name) {
  24. return context.getResources().getIdentifier(name, "layout", context.getPackageName());
  25. }
  26.  
  27. public static int getDrawableRes(Context context, String name) {
  28. return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
  29. }
  30.  
  31. public static int getDimenRes(Context context, String name) {
  32. return context.getResources().getIdentifier(name, "dimen", context.getPackageName());
  33. }
  34.  
  35. public static int getColorRes(Context context, String name) {
  36. return context.getResources().getIdentifier(name, "color", context.getPackageName());
  37. }
  38.  
  39. public static TypedArray getTypeArray(Context context, String name, AttributeSet attrs, int defStyle) {
  40. try {
  41. Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();
  42. for (Field f : fields2) {
  43. if (f.getName().equals(name)) {
  44. int[] styleableArray = (int[]) f.get(null);
  45. return context.obtainStyledAttributes(attrs, styleableArray, defStyle, 0);
  46. }
  47. }
  48. } catch (Throwable t) {
  49. t.printStackTrace();
  50. }
  51.  
  52. return null;
  53. }
  54.  
  55. public static TypedArray getTypeArray(Context context, String name, AttributeSet attrs) {
  56. try {
  57. Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();
  58. for (Field f : fields2) {
  59. if (f.getName().equals(name)) {
  60. int[] styleableArray = (int[]) f.get(null);
  61. return context.obtainStyledAttributes(attrs, styleableArray);
  62. }
  63. }
  64. } catch (Throwable t) {
  65. t.printStackTrace();
  66. }
  67.  
  68. return null;
  69. }
  70.  
  71. public static int getColorByResourceId(Context context, int colorResourceId) {
  72. try {
  73. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  74. return context.getColor(colorResourceId);
  75. }
  76. return context.getResources().getColor(colorResourceId);
  77. } catch (Resources.NotFoundException e) {
  78. return colorResourceId;
  79. }
  80. }
  81.  
  82. public static int getColorByName(Context context, String name) {
  83. int colorId = 0;
  84. try {
  85. Class res = R.color.class;
  86. Field field = res.getField(name);
  87. colorId = field.getInt(null);
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91.  
  92. return getColorByResourceId(context, colorId);
  93. }
  94.  
  95. public static int getPlaceholderIconResourceId(Context context) {
  96. return ResourceUtils.getDrawableRes(context, "smax_all_ic_placeholder_icon");
  97. }
  98.  
  99. public static int getPlaceholderBannerResourceId(Context context) {
  100. return ResourceUtils.getDrawableRes(context, "smax_all_ic_placeholder_banner");
  101. }
  102. }
Add Comment
Please, Sign In to add comment