Skip to content

How do I...

Nathanael Silverman edited this page Apr 9, 2018 · 2 revisions

This page answers common how-to questions that may come up when using Paris. You should read and understand the Overview and the Core documentation first.

Contents

... extend a @Styleable class?

Nothing tricky here, extending views annotated with @Styleable should just work.

@Styleable("MyView")
public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs, defStyle);
        Paris.style(this).apply(attrs);
    }

    ...
}

@Styleable("MyViewExtension")
public class MyViewExtension extends MyView {
    
    public MyViewExtension(Context context) {
        super(context);
    }

    public MyViewExtension(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyViewExtension(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs, defStyle);
        // Every view in the hierarchy needs to call this for himself
        Paris.style(this).apply(attrs);
    }

    ...
}

Each view in the hierarchy needs to call Paris.style(this).apply(attrs) during initialization.

However when using Paris.style(myViewExtension).apply(R.style...) with a style resource all attributes throughout the view hierarchy will be applied (from MyViewExtension, MyView, and View). Similarly when using a style builder all attribute methods throughout the view hierarchy will be available.

... apply a style to a subview's subview?

As we've seen, Paris generates helper methods to style annotated subviews:

@Styleable("MyView")
public class MyView extends View {

    @StyleableField(R.styleable.MyView_labelStyle)
    TextView label;
}

// Applying a style to a MyView instance
Paris.style(myView).apply(R.style...);
// Applying a style to the label
Paris.styleBuilder(myView)
        .labelStyle(R.style...)
        .apply();

But what if the subview has styleable subviews of its own? Paris lets you chain calls so you can style any annotated subview in the hierarchy:

@Styleable("MyHeader")
public class MyHeader extends ViewGroup {

    @StyleableField(R.styleable.MyHeader_myViewStyle)
    MyView myView;

    @StyleableField(R.styleable.MyHeader_titleStyle)
    TextView title;
}

// Applying a style to a MyHeader instance
Paris.style(myHeader).apply(R.style...);
// Applying a style to the MyView subview
Paris.style(myHeader)
        .myViewStyle(R.style...)
        .apply();
// Applying a style to MyView's label
Paris.style(myHeader)
        .myViewStyle((builder) -> builder
                .labelStyle(R.style...)
        )
        .apply();

... add support for an Android view attribute?

Support for Android view attributes can be added using View Proxies.