yo1122
yo1122

Reputation: 321

Parse an android XML layout file using XmlPullParser

This question has been asked a lot in different situations, but for over a day I can't find an answer that fixes my error.

I have an xml file called activity_single_touch.xml inside res/layout folder. This file is used to add views like buttons, but I also want to be able to edit it "live" (the user can draw on screen).

Following android guide, I tried using XmlPullParser in my main java code like so:

public class SingleTouchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_single_touch);
        XmlPullParser parser = getResources().getXml(R.xml.activity_single_touch);
        AttributeSet attributes = Xml.asAttributeSet(parser);
        setContentView(new SingleTouchEventView(this, attributes));
    }

    public class SingleTouchEventView extends View {
        ...
    }
}

The rest of the code uses OnDraw and OnTouchEvent to draw a path according to user movement, and then invalidate.

The error I am getting at the moment is "cannot resolve symbol 'xml'". How can I fix this?

p.s: Things I have tried (and have failed): Using

XmlPullParser parser =getResources().getXml(R.layout.activity_single_touch);

and getting "expected xml element" error,

I've read over

this guide
these answeres
these as well
use of streams
use of context
moving the xml file to a different folder(could not move it)
this guide

and of course searching google and some more stackOverflow questions and answers.

Upvotes: 0

Views: 3041

Answers (1)

Neerajlal K
Neerajlal K

Reputation: 6828

You can use getLayout method to open the layout file and parse it. This is how you can parse it manually.

Try this,

try {
    XmlResourceParser parser = getResources().getLayout(R.layout.activity_single_touch);
    int event = parser.getEventType();
    do{
        String tagName = parser.getName();
        if(event == XmlPullParser.START_TAG){
            Log.e("TagName", tagName);
        }
        event = parser.next();
    } while (event != XmlPullParser.END_DOCUMENT);
} catch (XmlPullParserException e) {
    e.printStackTrace();
} catch (Exception e){
    e.printStackTrace();
}

Usage in your code,

public class SingleTouchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            XmlResourceParser parser = getResources().getLayout(R.layout.activity_single_touch);
            AttributeSet attributes = Xml.asAttributeSet(parser);
            setContentView(new SingleTouchEventView(this, attributes));
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public class SingleTouchEventView extends View {

    }
}

Upvotes: 1

Related Questions