package com.ponyvillelive.pvlmobile.util; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; import android.content.res.TypedArray; import android.support.annotation.AttrRes; import android.support.annotation.ColorRes; /** * Created by berwyn on 19/03/15. */ public class ResourceHelper { /** * Get a color value from a theme attribute. * * @param context used for getting the color. * @param attribute theme attribute. * @param defaultColor default to use. * @return color value */ public static int getThemeColor(Context context, @AttrRes int attribute, @ColorRes int defaultColor) { int themeColor = 0; String packageName = context.getPackageName(); try { Context packageContext = context.createPackageContext(packageName, 0); ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0); packageContext.setTheme(applicationInfo.theme); Resources.Theme theme = packageContext.getTheme(); TypedArray ta = theme.obtainStyledAttributes(new int[]{attribute}); themeColor = ta.getColor(0, defaultColor); ta.recycle(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return themeColor; } }