Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Use ContextCompat to retrieve drawables #1422

@ened

Description

@ened

Given I'm injecting a drawable into my fragment like that:

@DrawableRes(R.drawable.bg_button_with_stroke_orange)
protected Drawable backgroundDrawable;

Then Android Annotations generates this code:

private void init_() {
    Resources resources_ = context_.getResources();
    backgroundDrawable = resources_.getDrawable(drawable.bg_button_with_stroke_orange);
}

Which then generates this compiler warning for targetSdk=22 builds:

TheClass_.java:33: warning: [deprecation] getDrawable(int) in Resources has been deprecated
        backgroundDrawable = resources_.getDrawable(drawable.bg_button_with_stroke_orange);

Instead, AndroidAnnoations should, given the class ContextCompat is available, generate this:

private void init_() {
    backgroundDrawable = ContextCompat.getDrawable(context_, drawable.bg_button_with_stroke_orange);
}

Benefits:

  • Obviously, the compiler warning would be gone
  • More importantly, the theme will be applied to the drawable on Lollipop+ devices

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions