Paris processor

Paris is a system for creating and applying styles to views in Android.

License

License

GroupId

GroupId

com.bandyer
ArtifactId

ArtifactId

paris-processor
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Paris processor
Paris is a system for creating and applying styles to views in Android.
Project URL

Project URL

https://github.com/bandyer/paris
Source Code Management

Source Code Management

https://github.com/bandyer/paris

Download paris-processor

How to add to project

<!-- https://jarcasting.com/artifacts/com.bandyer/paris-processor/ -->
<dependency>
    <groupId>com.bandyer</groupId>
    <artifactId>paris-processor</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.bandyer/paris-processor/
implementation 'com.bandyer:paris-processor:1.0.0'
// https://jarcasting.com/artifacts/com.bandyer/paris-processor/
implementation ("com.bandyer:paris-processor:1.0.0")
'com.bandyer:paris-processor:jar:1.0.0'
<dependency org="com.bandyer" name="paris-processor" rev="1.0.0">
  <artifact name="paris-processor" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.bandyer', module='paris-processor', version='1.0.0')
)
libraryDependencies += "com.bandyer" % "paris-processor" % "1.0.0"
[com.bandyer/paris-processor "1.0.0"]

Dependencies

runtime (5)

Group / Artifact Type Version
com.bandyer : paris-annotations jar 1.0.0
com.android.support » support-annotations jar 27.1.1
com.squareup : javapoet jar 1.11.1
com.squareup : kotlinpoet jar 0.7.0
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.2.60

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
io.kotlintest : kotlintest jar 2.0.7

Project Modules

There are no modules declared in this project.

Paris

Paris lets you define and apply styles programmatically to Android views, including custom attributes.

  • Apply styles programmatically at any time.
  • Combine multiple styles together.
  • Create styles programmatically (as opposed to using XML).
  • Use annotations to easily support custom attributes (inspired by Barber).
  • Declare explicitly supported styles for your custom views.
  • And much more...

Installation

In your project's build.gradle:

dependencies {
    implementation 'com.bandyer:paris:1.0.0'
    // If you are using Paris annotations
    implementation 'com.bandyer:paris-annotations:1.0.0'
    annotationProcessor 'com.bandyer:paris-processor:1.0.0'
}

To use Paris in a library module see Library Modules.

Quick Start

Applying an XML-Defined Style

Paris.style(myView).apply(R.style.MyStyle);

Where myView is an arbitrary view instance and MyStyle an XML-defined style. Many but not all attributes are supported, for more see Supported View Types and Attributes.

Combining 2 or More Styles

Paris.styleBuilder(myView)
        .add(R.style.StyleA)
        .add(R.style.StyleB)
        ...
        .apply();

In cases where there's some overlap the attribute value from the last style added prevails. For more see Combining Styles.

Defining Styles Programmatically

Paris.styleBuilder(textView)
        .textColor(Color.GREEN) // Using an actual value
        .textSizeRes(R.dimen.my_text_size_small) // Or a resource
        .apply();

Can be combined with style resources as well:

Paris.styleBuilder(textView)
        .add(R.style.MyGreenTextView)
        .textSizeRes(R.dimen.my_text_size_small)
        .apply();

For more see Defining Styles Programmatically.

Custom View Attributes

Attributes are declared as followed:

<declare-styleable name="MyView">
    <attr name="title" format="string" />
    <attr name="image" format="reference" />
    <attr name="imageSize" format="dimension" />
</declare-styleable>

The custom view is annotated with @Styleable and @Attr:

// The value here corresponds to the name chosen in declare-styleable
@Styleable("MyView")
public class MyView extends ViewGroup {

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

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

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // Enables the custom attributes when used in XML layouts
        Paris.style(this).apply(attrs);
    }

    @Attr(R.styleable.MyView_title)
    public void setTitle(String title) {
        ...
    }

    @Attr(R.styleable.MyView_image)
    public void setImage(Drawable image) {
        ...
    }

    @Attr(R.styleable.MyView_imageSize)
    public void setImageSize(@Px int imageSize) {
        ...
    }
}

The @Attr-annotated methods will be called by Paris when the view is inflated with an AttributeSet or when a style is applied.

For more see Custom View Attributes.

Styling Subviews

Attributes are declared as followed for the 2 subviews we'd like to be able to style:

<declare-styleable name="MyHeader">
    <attr name="titleStyle" format="reference" />
    <attr name="subtitleStyle" format="reference" />
    ...
</declare-styleable>

The subview fields are annotated with @StyleableChild:

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

    @StyleableChild(R.styleable.MyHeader_titleStyle)
    TextView title;
    
    @StyleableChild(R.styleable.MyHeader_subtitleStyle)
    TextView subtitle;
    
    ...
    // Make sure to call Paris.style(this).apply(attrs) during initialization
}

The title and subtitle styles can now be part of MyHeader styles:

<MyHeader
    ...
    app:titleStyle="@style/Title2"
    app:subtitleStyle="@style/Regular" />
Paris.styleBuilder(myHeader)
        .titleStyle(R.style.Title2) // Defined in XML
        .subtitleStyle((builder) -> builder // Defined programmatically
                .textColorRes(R.color.text_color_regular)
                .textSizeRes(R.dimen.text_size_regular))
        .apply();

For more see Styling Subviews.

Linking Styles to Views

@Styleable
public class MyView extends View {

    // For styles defined in XML
    @Style
    static final int RED_STYLE = R.style.MyView_Red;

    // For styles defined programmatically
    @Style
    static void greenStyle(MyViewStyleApplier.StyleBuilder builder) {
        builder.background(R.color.green);
    }

    ...
}

Helper methods are generated for each linked style:

Paris.style(myView).applyRed(); // Equivalent to .apply(R.style.MyView_Red)
Paris.style(myView).applyGreen(); // No equivalent

Paris.styleBuilder(myView)
        .addRed() // Equivalent to .add(R.style.MyView_Red)
        .addGreen() // No equivalent
        ...
        .apply();

For more see Linking Styles to Custom Views.

Documentation

See examples and browse complete documentation at the Paris Wiki.

If you still have questions, feel free to create a new issue.

Contributing

We love contributions! Check out our contributing guidelines and be sure to follow our code of conduct.

License

Copyright 2018 Airbnb, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
com.bandyer

Bandyer

Versions

Version
1.0.0