Example usage for android.support.v4.content ContextCompat getDrawable

List of usage examples for android.support.v4.content ContextCompat getDrawable

Introduction

In this page you can find the example usage for android.support.v4.content ContextCompat getDrawable.

Prototype

public static final Drawable getDrawable(Context context, int i) 

Source Link

Usage

From source file:com.aqtc.bmobnews.util.ResourcesUtils.java

/**
 * ?AndroidAPI?Drawable/*w  ww  . j a  v a2 s  .c o  m*/
 * According to the Android version, calls the method for drawable of version API
 *
 * @param context context
 * @param resId resource id
 * @return color
 */
public static Drawable getDrawable(Context context, int resId) {
    return ContextCompat.getDrawable(context, resId);
}

From source file:com.github.leonardoxh.recyclerviewutils.DividerItemDecorator.java

public DividerItemDecorator(@NonNull Context context, @DividerOrientation int orientation,
        @DrawableRes int drawableRes) {
    divider = ContextCompat.getDrawable(context, drawableRes);
    setOrientation(orientation);/*from w  w  w . j a  v a 2  s .co m*/
}

From source file:com.example.android.materialme.SportsAdapter.java

/**
 * Constructor that passes in the sports data and the context
 * @param sportsData ArrayList containing the sports data
 * @param context Context of the application
 *///from www  . j  av a  2s  . c o  m
SportsAdapter(Context context, ArrayList<Sport> sportsData) {
    this.mSportsData = sportsData;
    this.mContext = context;

    //Prepare gray placeholder
    mGradientDrawable = new GradientDrawable();
    mGradientDrawable.setColor(Color.GRAY);

    //Make the placeholder same size as the images
    Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.img_badminton);
    if (drawable != null) {
        mGradientDrawable.setSize(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    }
}

From source file:cn.djangoogle.pull2load.internal.IndicatorLayout.java

public IndicatorLayout(Context context, Pull2LoadBase.Mode mode) {
    super(context);
    mArrowImageView = new ImageView(context);

    Drawable arrowD = ContextCompat.getDrawable(context, R.drawable.indicator_arrow);
    mArrowImageView.setImageDrawable(arrowD);

    final int padding = getResources().getDimensionPixelSize(R.dimen.indicator_internal_padding);
    mArrowImageView.setPadding(padding, padding, padding, padding);
    addView(mArrowImageView);/*from   www . jav  a 2s .  c o m*/

    int inAnimResId, outAnimResId;
    switch (mode) {
    case PULL_FROM_END:
        inAnimResId = R.anim.slide_in_from_bottom;
        outAnimResId = R.anim.slide_out_to_bottom;
        setBackgroundResource(R.drawable.indicator_bg_bottom);

        // Rotate Arrow so it's pointing the correct way
        mArrowImageView.setScaleType(ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        matrix.setRotate(180f, arrowD.getIntrinsicWidth() / 2f, arrowD.getIntrinsicHeight() / 2f);
        mArrowImageView.setImageMatrix(matrix);
        break;
    default:
    case PULL_FROM_START:
        inAnimResId = R.anim.slide_in_from_top;
        outAnimResId = R.anim.slide_out_to_top;
        setBackgroundResource(R.drawable.indicator_bg_top);
        break;
    }

    mInAnim = AnimationUtils.loadAnimation(context, inAnimResId);
    mInAnim.setAnimationListener(this);

    mOutAnim = AnimationUtils.loadAnimation(context, outAnimResId);
    mOutAnim.setAnimationListener(this);

    final Interpolator interpolator = new LinearInterpolator();
    mRotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mRotateAnimation.setInterpolator(interpolator);
    mRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
    mRotateAnimation.setFillAfter(true);

    mResetRotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mResetRotateAnimation.setInterpolator(interpolator);
    mResetRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
    mResetRotateAnimation.setFillAfter(true);

}

From source file:com.github.shareme.gwsmaterialuikit.library.mscrollbar.Indicator.java

void linkToScrollBar(MaterialScrollBar msb, boolean addSpace) {
    this.addSpace = addSpace;
    if (Build.VERSION.SDK_INT >= 16) {
        setBackground(ContextCompat.getDrawable(context, R.drawable.mscrollbar_indicator));
    } else {/*from  ww  w  .j ava2 s .c om*/
        setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.mscrollbar_indicator));
    }
    LayoutParams lp = new LayoutParams(Utils.getDP(getIndicatorWidth(), this),
            Utils.getDP(getIndicatorHeight(), this));
    if (addSpace) {
        lp.setMargins(0, 0, Utils.getDP(15, this) + msb.handle.getWidth(), 0);
    } else {
        lp.setMargins(0, 0, Utils.getDP(2, this) + msb.handle.getWidth(), 0);
    }

    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getTextSize());
    LayoutParams tvlp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    tvlp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

    addView(textView, tvlp);

    ((GradientDrawable) getBackground()).setColor(msb.handleColour);

    lp.addRule(ALIGN_RIGHT, msb.getId());
    ((ViewGroup) msb.getParent()).addView(this, lp);

    materialScrollBar = msb;
}

From source file:com.bayapps.android.robophish.ui.MediaItemViewHolder.java

static View setupView(Activity activity, View convertView, ViewGroup parent, MediaDescriptionCompat description,
        int state) {

    if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
        initializeColorStateLists(activity);
    }/*from   ww w .  j  a  va2  s  . c  om*/

    MediaItemViewHolder holder;

    Integer cachedState = STATE_INVALID;

    if (convertView == null) {
        convertView = LayoutInflater.from(activity).inflate(R.layout.media_list_item, parent, false);
        holder = new MediaItemViewHolder();
        holder.mImageView = (ImageView) convertView.findViewById(R.id.play_eq);
        holder.mTitleView = (TextView) convertView.findViewById(R.id.title);
        holder.mDescriptionView = (TextView) convertView.findViewById(R.id.description);
        convertView.setTag(holder);
    } else {
        holder = (MediaItemViewHolder) convertView.getTag();
        cachedState = (Integer) convertView.getTag(R.id.tag_mediaitem_state_cache);
    }

    holder.mTitleView.setText(description.getTitle());
    holder.mDescriptionView.setText(description.getSubtitle());

    // If the state of convertView is different, we need to adapt the view to the
    // new state.
    if (cachedState == null || cachedState != state) {
        switch (state) {
        case STATE_PLAYABLE:
            Drawable pauseDrawable = ContextCompat.getDrawable(activity, R.drawable.ic_play_arrow_black_36dp);
            DrawableCompat.setTintList(pauseDrawable, sColorStateNotPlaying);
            holder.mImageView.setImageDrawable(pauseDrawable);
            holder.mImageView.setVisibility(View.VISIBLE);
            break;
        case STATE_PLAYING:
            AnimationDrawable animation = (AnimationDrawable) ContextCompat.getDrawable(activity,
                    R.drawable.ic_equalizer_white_36dp);
            DrawableCompat.setTintList(animation, sColorStatePlaying);
            holder.mImageView.setImageDrawable(animation);
            holder.mImageView.setVisibility(View.VISIBLE);
            animation.start();
            break;
        case STATE_PAUSED:
            Drawable playDrawable = ContextCompat.getDrawable(activity, R.drawable.ic_equalizer1_white_36dp);
            DrawableCompat.setTintList(playDrawable, sColorStatePlaying);
            holder.mImageView.setImageDrawable(playDrawable);
            holder.mImageView.setVisibility(View.VISIBLE);
            break;
        default:
            holder.mImageView.setVisibility(View.GONE);
        }
        convertView.setTag(R.id.tag_mediaitem_state_cache, state);
    }

    return convertView;
}

From source file:com.android.example.leanback.fastlane.BackgroundHelper.java

public void prepareBackgroundManager() {
    mBackgroundManager = BackgroundManager.getInstance(mActivity);
    mBackgroundManager.attach(mActivity.getWindow());
    mBackgroundTarget = new PicassoBackgroundManagerTarget(mBackgroundManager);
    mDefaultBackground = ContextCompat.getDrawable(mActivity, R.drawable.default_background);
    mMetrics = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
}

From source file:com.alexive.graphicalutils.demo.CardsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //On a real project, you'd define the layout in an xml file
    this.mLinearLayout = new LinearLayout(this);
    this.mLinearLayout.setOrientation(LinearLayout.VERTICAL);

    ScrollView sv = new ScrollView(this);
    sv.addView(this.mLinearLayout);
    setContentView(sv);/* www  .ja  va 2s.c o  m*/

    this.mCardBuilder = new CardBuilder(CardBuilder.CardType.FULL_WIDTH_IMAGE);
    this.params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    int sixteenDP = ViewUtils.convertDPtoPixels(this, 16);
    this.params.setMargins(sixteenDP, sixteenDP / 2, sixteenDP, sixteenDP / 2);

    Drawable cardImage = ContextCompat.getDrawable(this, R.drawable.lisbon);
    this.mCardBuilder.setTitle("Card title here").setSubTitle("subtitle here")
            .addSupplementalAction(new CardBuilder.CardAction("A1", 1))
            .addSupplementalAction(new CardBuilder.CardAction("A2", 2)).addActionClickListener(this)
            .setText("Text here! - To create a card like this use the " + "FULL_WIDTH_IMAGE card type")
            .setImage(cardImage);

    addCardToLayout();

    this.mCardBuilder.setType(CardBuilder.CardType.IMAGE_AS_BACKGROUND).useLightTheme(false) //This one supports dark theme only
            .setText("Text here. Use IMAGE_AS_BACKGROUND for a card like this one");

    addCardToLayout();

    this.mCardBuilder.setType(CardBuilder.CardType.IMAGE_FILLS_WITH_ACTIONS_ON_LEFT).useLightTheme(true)
            .setText("Text here. IMAGE_FILLS_WITH_ACTIONS_ON_LEFT. " + "Notice this one doesn't display text");

    addCardToLayout();

    this.mCardBuilder.setType(CardBuilder.CardType.IMAGE_NEXT_TO_TITLE)
            .setText("Notice the previous one (IMAGE_FILLS_WITH_ACTIONS_ON_LEFT) "
                    + "doesn't show any text. This one's a IMAGE_NEXT_TO_TITLE sample");

    addCardToLayout();

    this.mCardBuilder.reset(); //This clears the builder, you'll have to set it up again
    this.mCardBuilder.setType(CardBuilder.CardType.IMAGE_FILLS_WITH_ACTIONS_ON_LEFT).setImage(cardImage)
            .addSupplementalAction(new CardBuilder.CardAction(
                    ContextCompat.getDrawable(this, R.drawable.ic_favorite_black_24dp), 3))
            .addSupplementalAction(new CardBuilder.CardAction(
                    ContextCompat.getDrawable(this, R.drawable.ic_info_outline_black_24dp), 4))
            .addActionClickListener(this);

    addCardToLayout();

    this.mCardBuilder.setType(CardBuilder.CardType.FULL_WIDTH_IMAGE).setTitle("Look, icons!")
            .setText("You can use icons as actions instead of text (or mix both)")
            .addSupplementalAction(new CardBuilder.CardAction("A5", 5));

    addCardToLayout();

    this.mCardBuilder.setType(CardBuilder.CardType.NO_IMAGE).setTitle("<Title>")
            .setText("This is the simplest one: NO_IMAGE");
    addCardToLayout();

}

From source file:com.google.samples.apps.iosched.ui.widget.NavDrawerItemView.java

public void setContent(@DrawableRes int iconResId, @StringRes int titleResId) {
    if (iconResId > 0) {
        Drawable icon = DrawableCompat.wrap(ContextCompat.getDrawable(getContext(), iconResId));
        if (mIconTints != null) {
            DrawableCompat.setTintList(icon, mIconTints);
        }//from ww w .  j a v a2s.  c  o  m
        ((ImageView) findViewById(R.id.icon)).setImageDrawable(icon);
    }
    ((TextView) findViewById(R.id.title)).setText(titleResId);
}

From source file:am.project.x.business.widgets.statelayout.StateLayoutActivity.java

@Override
protected void initializeActivity(@Nullable Bundle savedInstanceState) {
    setSupportActionBar(R.id.sl_toolbar);
    mVState = findViewById(R.id.sl_lyt_state);
    final RadioGroup state = findViewById(R.id.sl_rg_state);
    final RadioGroup mode = findViewById(R.id.sl_rg_mode);
    mDLoading = ContextCompat.getDrawable(this, R.drawable.ic_statelayout_loading);
    mDError = ContextCompat.getDrawable(this, R.drawable.ic_statelayout_error);
    mDEmpty = ContextCompat.getDrawable(this, R.drawable.ic_statelayout_empty);
    final AppCompatTextView loading = new AppCompatTextView(this);
    loading.setText(R.string.sl_change_state_loading);
    loading.setTextColor(0xfff2f71c);/*from  ww  w  . j  av  a2 s  .c  o m*/
    loading.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 64);
    mVLoading = loading;
    final AppCompatTextView error = new AppCompatTextView(this);
    error.setText(R.string.sl_change_state_error);
    error.setTextColor(0xffff4081);
    error.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 64);
    mVError = error;
    final AppCompatTextView empty = new AppCompatTextView(this);
    empty.setText(R.string.sl_change_state_empty);
    empty.setTextColor(0xff092d6d);
    empty.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 64);
    mVEmpty = empty;

    mVState.setOnStateClickListener(this);
    state.setOnCheckedChangeListener(this);
    state.check(R.id.sl_rb_normal);
    mode.setOnCheckedChangeListener(this);
    mode.check(R.id.sl_rb_drawable);
}