tags:

views:

31

answers:

1

I am building a custom view and trying to figure out how to integrate it with the gui layout editor in eclipse. I have added the code below to my constuctor.

 public baseGrid(Context context, AttributeSet attrs) {
  super(context, attrs);

  if (attrs.getAttributeValue(null, "bufferTop") != null)
   bufferTop = Integer.parseInt(attrs.getAttributeValue(null, "bufferTop"));   
  ...

and it works to read this xml attribute (... bufferTop="10" ...) from the xml layout file. However, is there a way to get bufferTop to show up in the GUI Property Editor as a property that I can set without editting the XML?

Thanks

A: 

Try to add "attrs.xml" file to "res/values" folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="MyAttrs">       
        <attr name="bufferTop"  format="dimension" />
        <attr name="myColor"    format="color" />
        <attr name="myInt"  format="integer" />        
        <attr name="myFloat"    format="float" />  
    </declare-styleable>    
</resources>

Read by this code:

public baseGrid(Context contxt, AttributeSet attrs) {
 TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyAttrs);
 bufferTop  = a.getInt(R.styleable.MyAttrs_bufferTop, 10);
 a.recycle();
}

Define widgets in this way:

<?xml version="1.0" encoding="utf-8"?>
< YOURPACKAGE.BaseGrid
        android:background="@drawable/red"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        app:bufferTop="100"/>
Damian Kołakowski
Thank you for the reply. That got me 90% of the way there. In my layout file, I had to add a name space element as shown below.xmlns:grid="http://schemas.android.com/apk/res/<my package name>"I was then able to add an attribute to the XML (... grid:bufferTop="15dip" ...) and read it through the code you specified.However, the new attribute does not show up in the GUI portion of the layout editor unless I manually add it to the XML first. Once I add it to the XML it does show under Misc. in the GUI properties editor. Is there no way to make it appear without first adding it to the XML?
Steve0212