AttributeParser

Additional

Language
Kotlin
Version
N/A
Created
Mar 4, 2018
Updated
Mar 19, 2018
Owner
WindSekirun (DongGil, Seo) (WindSekirun)
Contributor
WindSekirun (DongGil, Seo) (WindSekirun)
1
Activity
Badge
Generate
Download
Source code
APK file

Blurb

AttributeParser

Inject automatically your Attribute of CustomView, just with simple annotation

Generate own '*.kt' class to handle attributes using Annotation Processor, setting value by reflection.

Written in Kotlin, both language(Java, Kotlin) are fully supported!

Post

Import

dependencies {
    // AttributeParser
    implementation 'com.github.windsekirun:attribute-parser:1.1.0'
    annotationProcessor 'com.github.windsekirun:attribute-parser-compiler:1.1.0'

    // if your app using Kotlin?
    kapt 'com.github.windsekirun:attribute-parser-compiler:1.1.0'
}

Usages

  1. Declare @AttributeParser("package name") annotation into any class. package name will be your package name.
    • example, i wrote annotation into Application class, like @AttributeParser("com.github.windsekirun.abcd")
  2. Declare @CustomView annotation into CustomView Class
  3. Attach @AttrInt, @AttrColor annotation to field
  4. Clean - Build Project
  5. Done! Attribute class will generated at compile time.
  6. using StyleViewAttributes.apply(this, attributeSet) or StyleViewAttributes.apply(this, typedArray) for set variable from declare-styleable

Advance

Index

AttributeParser will generate index automatically, but sometimes you will need custom index to implement CustomView.

Just adding parameter value to @AttrInt, @AttrBoolean

public @AttrInt("StyleView_intTest") int intTest;

Default Value

public @AttrInt(value = "StyleView_intTest", def = 0) int intTest;

same in all Attr* annotations.

Logging Variables

after using StyleViewAttribute.apply(this, attributeSet), you can use StyleViewAttributes.printVariables(); to print out all name and value of attributes.

Examples

XML (attrs.xml)

 <declare-styleable name="StyleView">
        <attr name="stringTest" format="string" />
        <attr name="intTest" format="integer" />
        <attr name="booleanTest" format="boolean" />
        <attr name="colorTest" format="color" />
        <attr name="dimensionTest" format="dimension" />
        <attr name="flagTest">
            <flag name="none" value="0" />
            <flag name="top" value="1" />
            <flag name="right" value="2" />
            <flag name="bottom" value="4" />
            <flag name="left" value="8" />
            <flag name="all" value="15" />
        </attr>
        <attr name="floatTest" format="float" />
        <attr name="resourceTest" format="reference" />
        <attr name="fractionTest" format="fraction" />
    </declare-styleable>

XML (activity_main.xml)

<pyxis.uzuki.live.attribute.parser.demo.widget.StyleView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:booleanTest="true"
        app:colorTest="@android:color/white"
        app:dimensionTest="@android:dimen/app_icon_size"
        app:floatTest="16"
        app:intTest="1"
        app:resourceTest="@drawable/ic_launcher_background"
        app:stringTest="abc" />

Java, Kotlin (StyleView.java, StyleView.kt)

@CustomView
public class StyleView extends LinearLayout {
     @AttrInt public int intTest;
     @AttrBoolean public boolean booleanTest;
     @AttrColor public int colorTest;
     @AttrDimension public float dimensionTest;
     @AttrFloat public float floatTest;
     @AttrReference public int resourceTest;
     @AttrString public String stringTest;

    public StyleView(Context context) {
        super(context);
        init(null);
    }

    public StyleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attributeSet) {
        StyleViewAttributes.apply(this, attributeSet);
    }
}

Credit

if you find bug or improvement, please send me issue. PR is always welcome!

License

    Copyright 2018 WindSekirun (DongGil, Seo)

    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.