Miklos Jakab
Miklos Jakab

Reputation: 2034

Indirect reference (alias) to custom xml attribute in android

sI have a custom android view. It has a custom attribute:

    <com.my.CustomView
        custom:attribute="value_1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

custom:attribute is a flag, defined by:

    <resources>
        <attr name="attribute">
            <flag name="value_1" value="8"/>
            <flag name="value_2" value="9"/>
        </attr>

        <declare-styleable name="CustomView">
            <attr name="lobby_orientation"/>
        </declare-styleable>

    </resources>

What I'd like to do is to modify this value per config. For example it should be value_1 in portrait, and value_2 in landscape. For this I'd create a file in values-port and and other file in values-land:

    <resources>
        <what_to_write name="here">value_1</what_to_write>
    </resources>

And so the layout could be this:

    <com.my.CustomView
        custom:attribute="@what_to_write/here"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

Is this approach feasible in android?

Upvotes: 3

Views: 573

Answers (1)

Miklos Jakab
Miklos Jakab

Reputation: 2034

It seems (according to the http://developer.android.com/guide/topics/resources/providing-resources.html page), only simple values (and layouts and drawables) can have aliases. The way I've found is to put the value to the styles, and the style file can be defined per configuration.

    <resources>
        <style name="CustomViewStyle">
            <item name="what_to_write">value_1</item>
        </style>
    </resources>

    <com.my.CustomView
        style="CustomViewStyle"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

Upvotes: 1

Related Questions