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

Resources

Csaba Kozák edited this page Mar 16, 2016 · 8 revisions

Since AndroidAnnotations 1.0

All @XXXRes annotations indicate that an activity field should be injected with the corresponding Android resource from your res folder. The resource id can be set in the annotation parameter, ie @StringRes(R.string.hello).

If the resource id is not set, the name of the field will be used. The field must not be private.

@StringRes

The @StringRes annotation can be used to retrieve string resources.

Usage example:

@EActivity
public class MyActivity extends Activity {

  @StringRes(R.string.hello)
  String myHelloString;

  @StringRes
  String hello;

}

@ColorRes

The @ColorRes annotation can be used to retrieve color resources.

Usage example:

@EActivity
public class MyActivity extends Activity {

  @ColorRes(R.color.backgroundColor)
  int someColor;

  @ColorRes
  int backgroundColor;

}

@AnimationRes

@AnimationRes can be used to inject XmlResourceParser fields (not very useful) or Animation fields (much more interesting).

Usage example:

@EActivity
public class MyActivity extends Activity {

  @AnimationRes(R.anim.fadein)
  XmlResourceParser xmlResAnim;
    
  @AnimationRes
  Animation fadein;

}

@DimensionRes

The @DimensionRes annotation can be used to retrieve dimension resources.

Usage example:

@EActivity
public class MyActivity extends Activity {

  @DimensionRes(R.dimen.fontsize)
  float fontSizeDimension;

  @DimensionRes
  float fontsize;

}

@DimensionPixelOffsetRes

The @DimensionPixelOffsetRes annotation can be used to retrieve dimension resources. Retrieves the dimension to its final value as an integer pixel offset. This is the same as @DimensionRes, except the raw floating point value is truncated to an integer (pixel) value.

Usage example:

@EActivity
public class MyActivity extends Activity {

  @DimensionPixelOffsetRes(R.string.fontsize)
  int fontSizeDimension;

  @DimensionPixelOffsetRes
  int fontsize;

}

@DimensionPixelSizeRes

The @DimensionPixelSizeRes annotation can be used to retrieve dimension resources. Retrieves the dimension to its final value as an integer pixel size. This is the same as @DimensionRes, except the raw floating point value is converted to an integer (pixel) value for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size

Usage example:

@EActivity
public class MyActivity extends Activity {

  @DimensionPixelSizeRes(R.string.fontsize)
  int fontSizeDimension;

  @DimensionPixelSizeRes
  int fontsize;

}

Other @XXXRes

Here is the list of other supported resource annotations:

  • @BooleanRes
  • @ColorStateListRes
  • @DrawableRes
  • @IntArrayRes
  • @IntegerRes
  • @LayoutRes
  • @MovieRes
  • @TextRes
  • @TextArrayRes
  • @StringArrayRes

Method based injection

Since AndroidAnnotations 4.0.0

This works for all of the previously mentioned @*Res annotations.

@EActivity
public class MyActivity extends Activity {

  @ColorRes(R.color.backgroundColor)
  void setOneResource(int someColor){
    // do something with someColor
  }
  
   void setMultipleBeans(@StringRes(R.string.hello) String myHelloString, @StringRes String hello){
    // do something with myHelloString and hello
  }

}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally