6

I have a custom view (that extends viewgroup) and I have specified some custom attributes defined in attrs.xml....

<declare-styleable name="datascope">
    <attr name="colcount" format="integer" />
    <attr name="titleheaderrows" format="integer" />
    <attr name="colheaderrows" format="integer" />
    <attr name="rowlayout" format="reference" />
</declare-styleable>

The integers I can pickup fine, but the last one - rowlayout - I want to use to refer to a further layout file that I will inflate on demand. But I cannot find the right way to express rowlayout attribute in the main layout file. I have tried:

lui:rowlayout="@layout/sensorvaluesdata">

but this fails at runtime:

E/AndroidRuntime(22092): Caused by: java.lang.NumberFormatException: unable to parse 'res/layout/sensorvaluesdata.xml' as integer

and

lui:rowlayout="?layout/sensorvaluesdata"

which fails

E/AndroidRuntime(22341): Caused by: java.lang.NumberFormatException: unable to parse '?2130903043' as integer

Which is interesting 'cos it seems to have stuck the resource ID in there, but stuck a ? on the front as well.

My R.java files does have a sensible looking line for sensorvaluesdata.

public static final class layout {

    public static final int sensorvaluesdata=0x7f030003;

}

what is the right way to do this?

(I can hard code the info into the java source and it works fine....

View vx = li.inflate(R.layout.sensorvaluesdata, this, false);
1
  • 3
    Well this is wierd, tried this again just now and it all works OK. I've checked back and it all looks identical, and I had cleaned the project several times while working through this before the original post. I can only assume that Eclipse had a wobbly moment - thanks eclipse /) Commented Mar 4, 2012 at 15:17

1 Answer 1

5

Just to add a little more background for someone else. In code use something like this

LinearLayout ViewContainer=(LinearLayout) (LayoutInflater.from(context)).inflate(
    attributes.getResourceId(
        R.styleable.[styleableName]_[attributeName], 
        R.layout.[defaultValue]), 
    null);

In xml attributes under you styleable name...

<attr name="layout" format="reference"/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.