Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 436
Miklos Jakab Avatar asked Dec 06 '25 19:12

Miklos Jakab


1 Answers

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" />
like image 147
Miklos Jakab Avatar answered Dec 08 '25 14:12

Miklos Jakab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!