Example usage for android.content.res Resources getColor

List of usage examples for android.content.res Resources getColor

Introduction

In this page you can find the example usage for android.content.res Resources getColor.

Prototype

@ColorInt
public int getColor(@ColorRes int id, @Nullable Theme theme) throws NotFoundException 

Source Link

Document

Returns a themed color integer associated with a particular resource ID.

Usage

From source file:Main.java

@SuppressLint("NewApi")
public static int getColorNew(Resources resources, int colorResourceId) {
    return resources.getColor(colorResourceId, null);
}

From source file:Main.java

@SuppressLint("NewApi")
public static int getColorFromResourcesCompat(Resources res, int resId, @Nullable Resources.Theme theme) {
    return (Build.VERSION.SDK_INT >= 23) ? res.getColor(resId, theme) : res.getColor(resId);
}

From source file:Main.java

public static int getColorFromRessource(Context context, int ressourceId) {
    Resources res = context.getResources();
    if (Build.VERSION.SDK_INT >= 23) {
        return res.getColor(ressourceId, context.getTheme());
    } else {//  w  w w  .  j  ava 2  s  .  c o m
        return res.getColor(ressourceId);
    }
}

From source file:Main.java

/**
 * @see android.content.res.Resources#getColor(int id).
 *//*from w w  w. j  a  v a 2  s .c  o m*/
@SuppressWarnings("deprecation")
public static int getColor(Resources res, int id) throws NotFoundException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return res.getColor(id, null);
    } else {
        return res.getColor(id);
    }
}

From source file:Main.java

public static int getColor(Resources resources, int resId) {
    if (resources == null || resId <= 0)
        return 0;

    try {//from ww w.  j av  a2s.co m
        return Build.VERSION.SDK_INT >= 23 ? resources.getColor(resId, null) : resources.getColor(resId);
    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:com.github.pockethub.ui.view.DotPageIndicator.java

/**
 * A helper method for getting colors from resource id
 *
 * @param res A reference to resources// w ww. j  a va  2  s.  c om
 * @param id Resource id to the color
 * @param theme A theme reference
 * @return The color specified
 */
private int getColor(Resources res, int id, Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        return res.getColor(id, theme);
    else
        return res.getColor(id);
}

From source file:com.github.pockethub.android.ui.view.DotPageIndicator.java

/**
 * A helper method for getting colors from resource id
 *
 * @param res A reference to resources/* w w  w  . j  av a 2 s  .c  o m*/
 * @param id Resource id to the color
 * @param theme A theme reference
 * @return The color specified
 */
private int getColor(Resources res, int id, Resources.Theme theme) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return res.getColor(id, theme);
    } else {
        return res.getColor(id);
    }
}

From source file:com.nick.scalpel.core.AutoFoundWirer.java

private void wireFromContext(Context context, AutoFound.Type type, int idRes, Resources.Theme theme,
        Field field, Object forWho) {
    Resources resources = context.getResources();
    switch (type) {
    case STRING:/*from w  w w  .  jav  a 2s .co  m*/
        setField(field, forWho, resources.getString(idRes));
        break;
    case COLOR:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setField(field, forWho, resources.getColor(idRes, theme));
        } else {
            //noinspection deprecation
            setField(field, forWho, resources.getColor(idRes));
        }
        break;
    case INTEGER:
        setField(field, forWho, resources.getInteger(idRes));
        break;
    case BOOL:
        setField(field, forWho, resources.getBoolean(idRes));
        break;
    case STRING_ARRAY:
        setField(field, forWho, resources.getStringArray(idRes));
        break;
    case INT_ARRAY:
        setField(field, forWho, resources.getIntArray(idRes));
        break;
    case PM:
        setField(field, forWho, context.getSystemService(Context.POWER_SERVICE));
        break;
    case ACCOUNT:
        setField(field, forWho, context.getSystemService(Context.ACCOUNT_SERVICE));
        break;
    case ALARM:
        setField(field, forWho, context.getSystemService(Context.ALARM_SERVICE));
        break;
    case AM:
        setField(field, forWho, context.getSystemService(Context.ACTIVITY_SERVICE));
        break;
    case WM:
        setField(field, forWho, context.getSystemService(Context.WINDOW_SERVICE));
        break;
    case NM:
        setField(field, forWho, context.getSystemService(Context.NOTIFICATION_SERVICE));
        break;
    case TM:
        setField(field, forWho, context.getSystemService(Context.TELEPHONY_SERVICE));
        break;
    case TCM:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setField(field, forWho, context.getSystemService(Context.TELECOM_SERVICE));
        }
        break;
    case SP:
        setField(field, forWho, PreferenceManager.getDefaultSharedPreferences(context));
        break;
    case PKM:
        setField(field, forWho, context.getPackageManager());
        break;
    case HANDLE:
        setField(field, forWho, new Handler(Looper.getMainLooper()));
        break;
    case ASM:
        setField(field, forWho, context.getSystemService(Context.ACCESSIBILITY_SERVICE));
        break;
    case CAP:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setField(field, forWho, context.getSystemService(Context.CAPTIONING_SERVICE));
        }
        break;
    case KGD:
        setField(field, forWho, context.getSystemService(Context.KEYGUARD_SERVICE));
        break;
    case LOCATION:
        setField(field, forWho, context.getSystemService(Context.LOCATION_SERVICE));
        break;
    case SEARCH:
        setField(field, forWho, context.getSystemService(Context.SEARCH_SERVICE));
        break;
    case SENSOR:
        setField(field, forWho, context.getSystemService(Context.SENSOR_SERVICE));
        break;
    case STORAGE:
        setField(field, forWho, context.getSystemService(Context.STORAGE_SERVICE));
        break;
    case WALLPAPER:
        setField(field, forWho, context.getSystemService(Context.WALLPAPER_SERVICE));
        break;
    case VIBRATOR:
        setField(field, forWho, context.getSystemService(Context.VIBRATOR_SERVICE));
        break;
    case CONNECT:
        setField(field, forWho, context.getSystemService(Context.CONNECTIVITY_SERVICE));
        break;
    case NETWORK_STATUS:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setField(field, forWho, context.getSystemService(Context.NETWORK_STATS_SERVICE));
        }
        break;
    case WIFI:
        setField(field, forWho, context.getSystemService(Context.WIFI_SERVICE));
        break;
    case AUDIO:
        setField(field, forWho, context.getSystemService(Context.AUDIO_SERVICE));
        break;
    case FP:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setField(field, forWho, context.getSystemService(Context.FINGERPRINT_SERVICE));
        }
        break;
    case MEDIA_ROUTER:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            setField(field, forWho, context.getSystemService(Context.MEDIA_ROUTER_SERVICE));
        }
        break;
    case SUB:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            setField(field, forWho, context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE));
        }
        break;
    case IME:
        setField(field, forWho, context.getSystemService(Context.INPUT_METHOD_SERVICE));
        break;
    case CLIP_BOARD:
        setField(field, forWho, context.getSystemService(Context.CLIPBOARD_SERVICE));
        break;
    case APP_WIDGET:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setField(field, forWho, context.getSystemService(Context.APPWIDGET_SERVICE));
        }
        break;
    case DEVICE_POLICY:
        setField(field, forWho, context.getSystemService(Context.DEVICE_POLICY_SERVICE));
        break;
    case DOWNLOAD:
        setField(field, forWho, context.getSystemService(Context.DOWNLOAD_SERVICE));
        break;
    case BATTERY:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setField(field, forWho, context.getSystemService(Context.BATTERY_SERVICE));
        }
        break;
    case NFC:
        setField(field, forWho, context.getSystemService(Context.NFC_SERVICE));
        break;
    case DISPLAY:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            setField(field, forWho, context.getSystemService(Context.DISPLAY_SERVICE));
        }
        break;
    case USER:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            setField(field, forWho, context.getSystemService(Context.USER_SERVICE));
        }
        break;
    case APP_OPS:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setField(field, forWho, context.getSystemService(Context.APP_OPS_SERVICE));
        }
        break;
    case BITMAP:
        setField(field, forWho, BitmapFactory.decodeResource(resources, idRes, null));
        break;
    }
}

From source file:com.adkdevelopment.e_contact.utils.UnderlinePageIndicator.java

@TargetApi(Build.VERSION_CODES.M)
public UnderlinePageIndicator(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (isInEditMode())
        return;//from  w w w .ja v  a  2  s  .  co m

    final Resources res = getResources();

    //Load defaults from resources
    final boolean defaultFades = res.getBoolean(R.bool.default_underline_indicator_fades);
    final int defaultFadeDelay = res.getInteger(R.integer.default_underline_indicator_fade_delay);
    final int defaultFadeLength = res.getInteger(R.integer.default_underline_indicator_fade_length);

    int defaultSelectedColor;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        defaultSelectedColor = res.getColor(R.color.white, context.getTheme());
    } else {
        //noinspection deprecation
        defaultSelectedColor = res.getColor(R.color.white);
    }

    //Retrieve styles attributes
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UnderlinePageIndicator, defStyle, 0);

    setFades(a.getBoolean(R.styleable.UnderlinePageIndicator_fades, defaultFades));
    setSelectedColor(a.getColor(R.styleable.UnderlinePageIndicator_selectedColor, defaultSelectedColor));
    setFadeDelay(a.getInteger(R.styleable.UnderlinePageIndicator_fadeDelay, defaultFadeDelay));
    setFadeLength(a.getInteger(R.styleable.UnderlinePageIndicator_fadeLength, defaultFadeLength));

    mCornerRadius = a.getDimension(R.styleable.UnderlinePageIndicator_vpi_corner_radius, 0);

    Drawable background = a.getDrawable(R.styleable.UnderlinePageIndicator_android_background);
    if (background != null) {
        setBackground(background);
    }

    a.recycle();

    final ViewConfiguration configuration = ViewConfiguration.get(context);
    mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
}