4

most of you will know that there is a possibility to give custom Attributes to custom Views in Android. This is explained quite brilliantly for example in this thread here on Stackoverflow. My question however is:

Is it possible to present such attributes only upon fulfillment of another condition?

What I mean by that is something like this (Pseudo-code):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="isClock" format="boolean" />
    </declare-styleable>

    <if name="isClock" value="true">
        <attr name="timezone" format="string">
    </if>
    <else>
        <attr name="somethingElse" format="string>
    </else>
</resources>

Now one possibility to not have to work with "wrong" attributes is to do this in the Java-Code, obviously:

public class MyCustomView {
    public MyCustomView(Context context) {

        TypedArray styleables = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView);
        boolean choice = styleables.getBoolean(R.styleable.MyCustomView_isClock, false);

        if(choice) {
            // It's a clock, react to the other attrs
        } else {
            // Don't react
        }

        styleables.recycle();
    }
}

Another way would be to do what ilomambo suggested in his answer: create various custom views with different names and let them only have the attributes that belong to them.

But I'm very much asking myself if it's possible to not confuse the programmer of the .xml-File in the first place and offer him only the stuff he really needs combined in one place. After all this is in a way already done by Android (well... the IDE/Lint/the Parser...) when hinting for example that the width or height of a view should be set to 0dp when using layout_weight.

But if I had to guess I'd say it's probably only possible if I rewrite the Android XML-Parser... Can someone please prove me wrong?

Thanks in advance

Community
  • 1
  • 1
avalancha
  • 1,457
  • 1
  • 22
  • 41

1 Answers1

1

If I understand you right, you have one custom view which can get different attributes conditonal on a third attribute.

To keep the XML programmer aware of illegal attributes I suggest one of two approaches:

  1. (Simple approach) Create one custom view for each "name" and for each its own declare-styleable group.

    <resources>
        <declare-styleable name="ClockCustomView">
            <attr . . . />
        </declare-styleable>
        <declare-styleable name="OtherCustomView">
            <attr . . . />
        </declare-styleable>
        <!-- Common attributes are declared outside declare-styleable -->
        <attr . . . />
    </resources>
    
  2. (More complete but more complex) Create an XSD schema for your XML, this way the programmer can validate the XML to your rules. XSD is itself XML, so you only have to learn the elements. See this link for more information on XSD, there are lots of info in the web if you google it too.

ilomambo
  • 8,290
  • 12
  • 57
  • 106
  • Yes, you understood correctly. Approach 1. is what I use myself at the moment as a temporary solution. I will report back as soon as I had time to look into XSD, thank you very much already – avalancha Apr 29 '13 at 09:06
  • @avalancha Keep in mind that with approach #2 you can do what you wanted and much more. The price to pay is that you have to learn how to write it right. If you use Eclipse, then you will notice that when you are editing an XML file, you can validate it from the right-click context menu. – ilomambo Apr 29 '13 at 09:53