Variables in XML resources – pass values from parent to child

I’m trying to reuse primitive shapes and compose much of my user interface using these declarative XML elements.

How to make a variable Android attribute?

But I do not want to create a separate XML file for each attribute value, and their permutations, and in the process duplicate much of the work.

  • Tutorials and libraries for OpenGL-ES games on Android
  • How to open layout on button click (android)
  • Dagger 2 and android Studio: working but can't see generated classes
  • Exact procedure to add “jfeinstein10 / SlidingMenu” in android project
  • geocoder.getFromLocation(); Timed out waiting for response from server
  • Detect application heap size in Android
  • For instance, I would like the consumer of this shape be able to define the android:radius value?

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270"/>
        <stroke
                android:width="1dp"
                android:color="#2f6699"/>
        <corners
                android:radius="3dp"/>
    </shape>
    

    Set attribute from consuming XML parent?

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/shape_box_round_blue_uniform" />
        <!-- How to set the corner radius here? -->
        <item android:drawable="@drawable/shape_box_round_blue" />
    </selector>
    

    Solutions?

    • If at all possible, I would like to not use any Java code-behind / avoid the need to create custom controls / classes
    • Using dimension resources may be a fruitful avenue?

  • Android ==> Memory Analysing ==> Eclipse memory analyzer?
  • Android MediaRecorder stop() not being called
  • How to get a vCard (.vcf file) into Android contacts from website
  • Android Studio - mergeDebugResources exception
  • How to send synthesized MotionEvent through the system?
  • Convert HTML5 into standalone Android App
  • One Solution collect form web for “Variables in XML resources – pass values from parent to child”

    You can create

    style attributes

    , which I think is what you are looking for. They basically are variable attributes.
    E.g. you can use this in your themes.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- You can define attributes here -->
        <attr name="button_radius" format="reference" />
    </resources>
    

    Which defines a variable reference called: button_radius
    This can be used in Styles, or in layout xml files:

    <!-- You can use them like so: -->
    <style name="MyApp.Theme1" parent="android:Theme.Holo.Light">
        <item name="button_radius">12</item>
    </style>
    
    <style name="MyApp.Theme2" parent="android:Theme.Holo.Light">
        <item name="button_radius">36</item>
    </style>
    

    This way, by changing the theme, you can use the different values for your radius. Below is an UNTESTED example of how you would change your shape drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#449def"
            android:endColor="#2f6699"
            android:angle="270"/>
        <stroke
            android:width="1dp"
            android:color="#2f6699"/>
        <corners
            android:radius="?button_radius"/> <!-- NOTE the ?button_radius-->
    </shape>
    

    Users can simply apply a different style to use different attributes.
    I don’t know if this example answers your question completele, but there’s a lot you can do by declaring attributes in your themes. These attributes are dynamic references. For more info about the possibilities see this article

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.