0

My question is about Android/Java.

How can I build a XmlPullParser object that can be read with the Xml.asAttributeSet method?

My main.xml contains among other things:

<org.javaforum.input
        android:layout_width="wrap_content"
        android:inputType="textEmailAddress"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your E-Mail"
        />

So I want to build a XmlPullParser object that contains the attributes and their values you see above so that in the end I have an AttributeSet object. How can I do this?

I read this question: https://stackoverflow.com/a/56062808/16627950

But I didn't quite get it. Is it even possible to do something like that? Maybe you can explain that to me...

5
  • 1
    Can't do it. Even if you could construct and instantiate your own class that could act as an XmlPullParser/AttributeSet, it wouldn't do you any good inside your View's constructor, 'cause you can't pass a new instance "back out" from that, and you can't modify the one passed in.
    – Mike M.
    Commented Aug 16, 2021 at 0:14
  • @Mike M. So only the Android System can build an XmlPullParser that can be read by the Xml.asAttributeSet method?
    – Button
    Commented Aug 16, 2021 at 0:20
  • @Mike M. I tried to see how it is been done. I took a look at the loadXmlResourceParser method here: android.googlesource.com/platform/frameworks/base/+/a175a5b/…
    – Button
    Commented Aug 16, 2021 at 0:22
  • 1
    Yep. I don't remember exactly which step is the ultimate roadblock (I think it's when you get down to somehow implementing XmlBlock, IIRC), but Android's inflation mechanism relies on XML being pre-compiled into a special binary format, and a good chunk of reading that back involves native code and data structures that are opaque at our level.
    – Mike M.
    Commented Aug 16, 2021 at 0:25
  • 1
    @Mike M. It's fascinating how the system compiles XML or other data. Your short, simple and above all understandable explanation is certainly very helpful for other users who try to do the same as me.
    – Button
    Commented Aug 16, 2021 at 0:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.