Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:10:43+00:00 2026-05-27T16:10:43+00:00

Is there a way to create a custom XML data type for Android? I

  • 0

Is there a way to create a custom XML data type for Android?

I have a class Model that contains all of the statistics of my entities. I want to be able to inflate Model class from xml similar – well, exaclty as View’s do. Is this possible?

Example:

<?xml version="1.0" encoding="utf-8"?>
<models xmlns:android="http://schemas.android.com/apk/res/android">
    <model name="tall_model"
        type="@string/infantry"
        stat_attack="5"
        >Tall Gunner</model>

    <model name="short_model"
        type="@string/infantry"
        stat_attack="3"
        ability="@resource/scout"
        >Short Gunner</model>

    <model name="big_tank"
        type="@string/vehicle"
        stat_attack="7"
        armour="5"
        >Big Tank</model>
</models>

And the class I would like to inflate.

class Model [extends Object] {
    public Model(Context context, AttributeSet attrs) {
        // I think you understand what happens here.
    }
    // ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:10:43+00:00Added an answer on May 27, 2026 at 4:10 pm

    With some custom code using carefully selected APIs, you can mimic the way Android inflates layout XML files and still benefit from the XML optimizations and goodies that Android has like compiled XML files and references to arbitrary resources within your custom XML files. You can’t directly hook in into the existing LayoutInflater because that class can only deal with inflating Views. In order for the below code to work, put your XML file in ‘res/xml’ in your application.

    First, here’s the code that parses the (compiled) XML file and invokes the Model constructor. You might want to add some registering mechanism so that you can easily register a class for any tag, or you might want to use ClassLoader.loadClass() so that you can load classes based on their name.

    public class CustomInflator {
        public static ArrayList<Model> inflate(Context context, int xmlFileResId) throws Exception {
            ArrayList<Model> models = new ArrayList<Model>();
    
            XmlResourceParser parser = context.getResources().getXml(R.xml.models);
            Model currentModel = null;
            int token;
            while ((token = parser.next()) != XmlPullParser.END_DOCUMENT) {
                if (token == XmlPullParser.START_TAG) {
                    if ("model".equals(parser.getName())) {
                        // You can retrieve the class in other ways if you wish
                        Class<?> clazz = Model.class;
                        Class<?>[] params = new Class[] { Context.class, AttributeSet.class };
                        Constructor<?> constructor = clazz.getConstructor(params);
                        currentModel = (Model)constructor.newInstance(context, parser);
                        models.add(currentModel);
                    }
                } else if (token == XmlPullParser.TEXT) {
                    if (currentModel != null) {
                        currentModel.setText(parser.getText());
                    }
                } else if (token == XmlPullParser.END_TAG) {
                    // FIXME: Handle when "model" is a child of "model"
                    if ("model".equals(parser.getName())) {
                        currentModel = null;
                    }
                }
            }
    
            return models;
        }
     }
    

    With this in place, you can put the “parsing” of the attributes inside the Model class, much like View does it:

    public class Model {
        private String mName;
        private String mType;
        private int mStatAttack;
        private String mText;
    
        public Model(Context context, AttributeSet attrs) {
            for (int i = 0; i < attrs.getAttributeCount(); i++) {
                String attr = attrs.getAttributeName(i);
                if ("name".equals(attr)) {
                    mName = attrs.getAttributeValue(i);
                } else if ("type".equals(attr)) {
                    // This will load the value of the string resource you
                    // referenced in your XML
                    int stringResource = attrs.getAttributeResourceValue(i, 0);
                    mType = context.getString(stringResource);
                } else if ("stat_attack".equals(attr)) {
                    mStatAttack = attrs.getAttributeIntValue(i, -1);
                } else {
                    // TODO: Parse more attributes
                }
            }
        }
    
        public void setText(String text) {
            mText = text;
        }
    
        @Override
        public String toString() {
            return "model name=" + mName + " type=" + mType + " stat_attack=" + mStatAttack + " text=" + mText;
        }
    }
    

    Above I have referenced attributes through the string representation. If you want to go further, you can define application specific attribute resources and use those instead, but that will complicate things quite a bit (see Declaring a custom android UI element using XML). Anyway, with all resources setup and this in a dummy activity:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            for (Model m : CustomInflator.inflate(this, R.xml.models)) {
                Log.i("Example", "Parsed: " + m.toString());
            }
        } catch (Exception e) {
            Log.e("Example", "Got " + e);
        }
    }
    

    you’ll get this output:

    I/Example ( 1567): Parsed: model name=tall_model type=Example3 stat_attack=5 text=Tall Gunner
    I/Example ( 1567): Parsed: model name=short_model type=Example3 stat_attack=3 text=Short Gunner
    I/Example ( 1567): Parsed: model name=big_tank type=Example2 stat_attack=7 text=Big Tank
    

    Note that you can’t have @resource/scout in your XML file since resource is not a valid resource type, but @string/foo works fine. You should also be able to use for example @drawable/foo with some trivial modifications to the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way to create custom post variables when a user presses submit,
Is there some way to create a custom (WoW64) shared registry key? By default
I wonder if there is a way to create a Custom List in Sharepoint,
Is there a way to create an instance of a class based on the
Is there any way to customize or control which type of collection class types
Is there any way to store a custom data object as persistent data without
Is there an easy way to create custom project templates? I did a quick
On Android, I would like to create a button that contains some other Views.
Alright, I have some data that I need to assign an int type identifier
I have an custom class for an Core Data entity, called 'Friends'. As I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.