Cel
Cel

Reputation: 6659

pass in a layout resource using custom attributes for a custom view

I would like to specify a child layout for a custom view in XML, analogously to the layout attribute of the include element:

<include layout="@layout/all_apps_cling"
         android:id="@+id/all_apps_cling"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>

Furthermore, I need the custom view to render at design-time in Eclipse or IntelliJ graphical layout preview

Using attr name="android:layout" does not work

[at least not in graphical layout preview]

attrs.xml

<declare-styleable name="ShowcaseView">
    <attr name="android:layout"/>
</declare-styleable>

usecase.xml

<com.github.espiandev.showcaseview.ShowcaseView
    style="@style/ShowcaseView"/>

styles.xml

<style name="ShowcaseView" parent="match_fill">
    <item name="android:layout">@layout/handy</item>
</style>

ShowcaseView.java

public ShowcaseView(Context context) {
    this(context, null, R.styleable.CustomTheme_showcaseViewStyle);
}

public ShowcaseView(Context context, AttributeSet attrs) {
    this(context, attrs, R.styleable.CustomTheme_showcaseViewStyle);
}

public ShowcaseView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // Get the attributes for the ShowcaseView
    final TypedArray styled = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShowcaseView, 0, 0);
    TypedValue handLayout = null;
    // Not sure if should be using .getValue in the first place
    // But there is no method TypedValue.getLayout or such 
    if (styled.getValue(R.styleable.ShowcaseView_android_layout, handLayout)) {
        // Does not reach here
    } else {
        // Reaches here instead i.e. getValue failed
    }
    styled.recycle();
}

Any solutions?

And I cannot use <attr name="linkedLayout" format="reference"/>, because I need it to work in the graphical layout preview - see the problem of accessing resources in the designer Resources$NotFoundException in Graphical Layout ADT preview (but app actually Works)

Upvotes: 1

Views: 1282

Answers (1)

pskink
pskink

Reputation: 24720

try to place your layout in master layout file:

<ShowcaseView ...>
    <include .../>
</ShowcaseView> 

of course you can also add children directly instead of include tag:

<ShowcaseView ...>
    <Button .../>
    ...
    <Button .../>
</ShowcaseView> 

Upvotes: 2

Related Questions