Can you specify objects in XML when setting attributes/properties?

45 Views Asked by At

I am trying to create a MultiLinkTextView class which allows you to specify several clickable 'link' areas in the text, and when one is clicked, having it call a function with a user-defined ID representing that link. There are already several examples here on SO of how to create clickable links and spans in a TextView so that part is covered.

This question however is specifically about seeing if it's possible to define the required information via attributes in your layout files, not about implementing the link portion.

I'm coming from Microsoft's XAML format where you can easily do such things declaratively because a) they have true properties (of which attributes are automatically inferred) and b) they allow any object to be constructed in their XAML, not just views (provided it's constructed in a place that makes sense.)

I'm trying to see if I can do something similar in Android: declaratively set complex properties on my objects from a layout file.

To facilitate this design, here's what I'm hoping is possible.

First we define a LinkInfo class which can either be created programmatically, or in a layout file via reading attributes, like so (assume the proper attrs.xml file is set-up, etc.):

public class LinkInfo
{
    public LinkInfo(int linkId, int spanStart, int spanEnd)
    {
        this.linkId    = linkId;
        this.spanStart = spanStart
        this.spanEnd   = spanEnd
    }

    public LinkInfo(Context context, AttributeSet attrs)
    {
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.LinkInfo);

        linkId    = styledAttrs.getInteger(R.styleable.LinkInfo_linkId, -1);
        spanStart = styledAttrs.getInteger(R.styleable.LinkInfo_spanStart, -1);
        spanEnd   = styledAttrs.getInteger(R.styleable.LinkInfo_spanEnd, -1);

        styledAttrs.recycle();
    }

    public final int spanId;
    public final int spanStart;
    public final int spanEnd;
}

Then, assuming MultiLinkTextView exposes a "property" (in quotes because Android doesn't appear to have properties per se, only getters, setters and external attributes) which holds a collection/array/list of those LinkInfo objects, below is how we'd like to define it in the XML layout.

<com.example.widgets.MultiLinkTextView
    android:text="This is a test which highlights LinkA, LinkB and LinkC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <MultiLinkTextView.LinkInfos>

        <com.example.widgets.MultiLinkTextView.LinkInfo
            app:linkId="10"
            app:spanStart="32"
            app:spanEnd="37" />

        <com.example.widgets.MultiLinkTextView.LinkInfo
            app:linkId="20"
            app:spanStart="39"
            app:spanEnd="44" />

        <com.example.widgets.MultiLinkTextView.LinkInfo
            app:linkId="30"
            app:spanStart="49"
            app:spanEnd="54" />

    </MultiLinkTextView.LinkInfos>

</com.example.widgets.MultiLinkTextView>

Note, XAML allows you to specify complex attributes using the syntax which sets 'someProperty' on the 'ParentType' instance to whatever is between the tags. In the above example, that means setting an array of LinkInfo objects to the LinkInfo's 'property' (or attribute or whatever).

Again, I'm pretty sure the above way is not possible in Android. But I'm hoping to find some way in Android which does let me use declarative syntax to set values like this, even if the above is wrong.

0

There are 0 best solutions below