0


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.

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay thanks! That is exactly what i thought. I was just hoping that there is a way of doing this which i havent seen yet.
1

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.

3 Comments

i think the OP's main concern is how to retrieve a child xml tag in a custom view, i.e. <Animation />
As @Karakuri pointed out, if that custom view extends ViewGroup, one can add child views (custom child views with custom parameters for that matter). But supplying an animation that way won't work.
Yes exactly. I already knew how to define simple attributes. The question was whether it is possible to define more complex attributes as XML child elements. But thanks anyway!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.