Skip to main content


Components can have their props come from regular Android style resources in a similar way to an Android View's AttributeSet constructor. It enables developers to define static prop values or prop defaults directly from style resources.

Style support can be implemented using the @OnLoadStyle method in your component spec. The first argument is a ComponentContext which you can use to retrieve a TypedArray with the style resource values. The rest of the arguments should be Outputs matching the name and type of the props to which you want to set values.

class MyComponentSpec {
static Component onCreateLayout(
ComponentContext c,
@Prop String prop1,
@Prop int prop2) {
return ...;

For example, in order to implement style support for the two props of the MyComponentSpec above, you'd first define the styleable attributes as usual:

<?xml version="1.0" encoding="utf-8"?>
<attr name="prop1" format="string" />
<attr name="prop2" format="integer" />
<declare-styleable name="MyComponent">
<attr name="prop1" />
<attr name="prop2" />

Then you can collect the values of these attributes in your @OnLoadStyle method as follows:

void onLoadStyle(
ComponentContext c,
Output<String> prop1,
Output<Integer> prop2) {
final DataBoundTypedArray a =
c.obtainDataBoundAttributes(R.styleable.Text, 0);
for (int i = 0, size = a.getIndexCount(); i < size; i++) {
final int attr = a.getIndex(i);
if (attr == R.styleable.MyComponent_prop1) {
} else if (attr == R.styleable.MyComponent_prop2) {

With this, you'll be able to define prop1 and prop2 in a style:

<?xml version="1.0" encoding="utf-8"?>
<style name="SomeStyle">
<item name="prop1">@string/some_string</item>

And use it in MyComponent:

MyComponent.create(c, 0,

In which case, prop1 would get the value from the @string/some_string resource.