Question


I've got a question on custom view XML-declarations.
I created my own View with the custom attributes as normal. Now I want to add more complex attributes like this: (This is non-working code)

<com.xxx.yyy.CustomTextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/customTextView1"
    android:layout_marginBottom="22dp"
    android:layout_toRightOf="@+id/buttonBlack"
    android:text="TextView" >
        <Animation
            animation:property1="123"
            animation:property2="456" />
        <Animation
            animation:property1="789"
            animation:property2="012" >
        </Animation>
</com.xxx.yyy.CustomTextView>

I didnt find a way to do this on my own but maybe someone has got an idea.

Thanks!


Edit:

I just solved the problem more or less nicely. I created a new .xml file in my /res/xml folder called animations.xml

<animations>
    <animation
        name="Animation name 1"
        float1="1.1"
        float2="1.2"
        integer1="11"
        integer2="12" />
    <animation
        name="Animation name 2"
        float1="2.1"
        float2="2.2"
        integer1="21"
        integer2="22" />
</animations>

My custom view in attrs.xml contains an attribute which is used to reference the animations.xml file from above:

<declare-styleable name="MyTextView">
    <attr name="animations" format="reference" />
</declare-styleable>

Now i parse the referenced .xml file in the constructor of the MyTextView as described here: http://thedevelopersinfo.com/2009/12/14/using-xml-file-resources-in-android/

Maybe this helps somebody at some time.

Was it helpful?

Solution

Unless CustomTextView extends ViewGroup (or has it in it's class hierarchy) and Animation is a custom view you've defined, this will not work. Only Views and ViewGroups are allowed in layout XML files (and some special tags defined by Android like include and merge), and only ViewGroup elements can have child elements.

OTHER TIPS

You can add custom XML attributes to your custom view like this:

<resources>

<declare-styleable name="YourCustomClass">
    <attr name="someCustomAnimationAttribute" format="reference" />
</declare-styleable>

<resources>

How you obtain it is described here:

http://developer.android.com/training/custom-views/create-view.html#applyattr

In your layout you have to declare a namespace:

xmlns:app="http://schemas.android.com/apk/res-auto"

and then use it like this:

<com.xxx.yyy.CustomTextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/customTextView1"
android:layout_marginBottom="22dp"
android:layout_toRightOf="@+id/buttonBlack"
android:text="TextView" 
app:someCustomAnimationAttribute="@drawable/someAnimation">

That way you can supply those animations via XML instead of adding them as as child elements which is not possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top