Modifying nested attributes on an included Layout
Asked Answered
B

1

9

I've been learning about merge and include lately, and I have a question I can't seem to figure out the answer too. Say I have a layout that defines a header component that I want to add to multiple layouts. However, I want to change the title, or icon of each header per each include usage. For example say I have the following layout:

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                style="@style/menu_header">

    <Button android:id="@+id/backButton"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button"
            android:text="@string/back"/>

    <TextView style="@style/headerTitle"
              android:layout_centerInParent="true"
              android:text="${title}"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"/>
</RelativeLayout>

Then I can include that in other layouts using:

<LinearLayout ...>
   <include android:id="@+id/searchHeader" layout="@layout/shared_header" title="Search"/>
   ...
</LinearLayout>

I know I can modify any layout_* attribute of the root element, but can I define other attributes that get substituted into the layout, like say "title" in this example, without having to create my own subclass of View, add declare-styleable definitions in valaues/resources, etc?

Having something like this would make creating reusable views so much simpler, but I can't seem to find any evidence that says if merge + include can do it.

Blare answered 12/7, 2011 at 18:34 Comment(3)
I kind of think that if you want to change the contents of those views, you should be using a custom View made up of those views. The include thing is more a facility for when you want to copy/paste a view/viewgroup elsewhere.Ligulate
But include is of limited use if you can only copy and paste exactly the same code. Custom views require a lot more overhead to author, adding custom style options, creating code to subclass a view, etc. If all I'm doing is tweaking some attributes of nested elements it seems like that could be done without eating the whole elephantBlare
I read the quick "tutorial" on include again ( android-developers.blogspot.com/2009/02/… ), and it seems pretty clear that it's just meant to be a simple copy/paste, especially when you include a common "sub-layout" in 2 different layouts (portrait and landscape). Though I do find your idea pretty cool after all :/Ligulate
B
5

The answer is nope. Unfortunately, Android isn't that powerful. You have to create your own extension of ViewGroup and write more code.

Blare answered 26/7, 2011 at 1:12 Comment(3)
can you provide an example of how to do this?Fluxion
No because writing your own custom component would take more effort and space than is allowed in a comment. You can't do what I describe using layout files alone. You have to write your own customer component by extending ViewGroup if you want to build something that can be reused and parameterized like this.Blare
@Blare hey i have similar situation as yours. did you manage to create your custom viewgroup? if yes, can you shed some light here how did you do that?Schram

© 2022 - 2024 — McMap. All rights reserved.