0

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.

1 Answer 1

1

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 {

    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I'm not sure how you want me to use this - I tried changing this line: XmlPullParser parser = getResources().getXml(R.xml.activity_single_touch); to the block of code you gave me and it didn't work. I also tried switching this line to: XmlResourceParser parser = getResources().getLayout(R.layout.activity_single_touch); and it didn't work. What am I doing wrong?
What do you mean by it doesn't work? Is the TagName not being printed?
First of all - it tells me on this line: AttributeSet attributes = Xml.asAttributeSet(parser); "cannot resolve symbol 'parser'". I thought it's a problem with the scope, so I changed the first few lines of your code to: XmlResourceParser parser ; try { parser = getResources().getLayout(R.layout.activity_main); ... But then it says "variable 'parser' might not have been initialized". What else should I do? Btw - just to make sure I'm using this code in the right place, this is all in the OnCreate method instead of the line quoted in the above comment.
I do in my code, and I asked few times if you mean that I just change that one line of my code into this block you gave me and keep the rest, otherwise I don't know how to use this block. Otherwise how do I use this parser? How do I implement it so that the ui includes both the SingleTouchEventView from the java file, and different views from the xml file?
compiled with no errors, but I can't see the button from my xml view.. only a blank screen I can draw on (SingleTouchEventView). Maybe I need to add something to the xml code? I'll add the first few lines to my question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.