0

I want to retrieve some information about custom attributes defined inside the 'attrs.xml' resource file of some library. In detail, I want to get the name and the format of all attributes.

I searched the documentation but I didn't find any example on how to do this. I only found examples on how to retrieve the values of these attributes inside a constructor of a custom view.

For example, given the following XML resource file.

<resources>
   <declare-styleable name="PieChart">
       <attr name="showText" format="boolean" />
       <attr name="labelPosition" format="enum">
           <enum name="left" value="0"/>
           <enum name="right" value="1"/>
       </attr>
   </declare-styleable>
</resources>

I want to get the list of attributes with their names and types. Something like this.

Attribute name: showText, type: boolean
Attribute name: labelPosition, type: enum {left, right}
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22
Fabrizio
  • 41
  • 1
  • 6

1 Answers1

0

you could try a special Factory set on the LayoutInflater that the Activity will use to parse the layout files.

super.onCreate(savedInstanceState);
getLayoutInflater().setFactory(new CustomAttrFactory());
setContentView(R.layout.the_layout);

where the CustomAttrFactory is like this:

public static class CustomAttrFactory implements Factory {

    @Override
    public View onCreateView(String name, Context context,
            AttributeSet attrs) {
        String attributeValue = attrs
                .getAttributeValue(
                        "http://schemas.android.com/apk/res/com.luksprog.droidproj1",
                        "attrnew");
        Log.e("ZXX", "" + attributeValue);
        // if attributeValue is non null then you know the attribute is
        // present on this view(you can use the name to identify the view,
        // or its id attribute)
        return null;
    }
}

Refer here for more details .


Hope it helps !

  • This is not what I want to achieve. I want to read these attributes from the resources files and not their values. – Fabrizio Mar 28 '19 at 08:47