get Attribute Drawable - Android Graphics

Android examples for Graphics:Drawable Operation

Description

get Attribute Drawable

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;

import android.graphics.drawable.Drawable;

import android.os.Build;
import android.support.annotation.AttrRes;

import android.support.annotation.DrawableRes;

public class Main {
    public static Drawable getAttrDrawable(Context context,
            @AttrRes int attResId) {
        final Resources.Theme theme = context.getTheme();
        TypedArray typedArray = theme// w  w  w .  jav a  2  s.  co m
                .obtainStyledAttributes(new int[] { attResId });
        return typedArray.getDrawable(0);
    }

    @SuppressWarnings("deprecation")
    public static Drawable getDrawable(Context context,
            @DrawableRes int resId) {
        Drawable drawable;
        final Resources resources = context.getResources();
        final Resources.Theme theme = context.getTheme();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            drawable = resources.getDrawable(resId, theme);
        else
            drawable = resources.getDrawable(resId);
        return drawable;
    }
}

Related Tutorials