Archive

Posts Tagged ‘content’

Lose EditText content when rotating

November 23, 2011 1 comment

One common problem as I’ve seen much is that, the `EditText` loses its content while rotating the phone.

Technically, it doesn’t lose it content, because a new `EditText` or a new `Activity` is created everytime you try to rotate the phone if your application doesn’t handle the orientation change.

Ok, if you don’t know about this, please refer to one of my old article discussing about the orientation problems.

For current situation, this is the solution to keep the text on `EditText`:

public static final String SAVED_TEXT_KEY = "SavedText";

protected void onCreate( Bundle savedInstanceState ) {
		// back to normal
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

		// ok we back, load the saved text
        if ( savedInstanceState != null ) {
            String savedText = savedInstanceState.getString( SAVED_TEXT_KEY );
            edtMyText.setText( savedText );
        }
}

public void onSaveInstanceState( Bundle savedInstanceState ) {
	// now, save the text if something overlaps this Activity
    savedInstanceState.putString( SAVED_TEXT_KEY, edtMyText.getText().toString() );
}

Cheers,

Pete Houston

Advertisements
Categories: Tricks & Tips Tags: , , ,

Clear EditText content on focus

November 23, 2011 Leave a comment

Feel annoying when everytime you input, you need to delete every word in `EditText`, here the tip:

EditText txtData = (EditText) findViewById( R.id.txtData );
txtData.setOnFocusChangeListener( new View.OnFocusChangeListener() {

	public void onFocusChange( View v, boolean hasFocus ) {
		if( hasFocus ) {
			txtData.setText( "", TextView.BufferType.EDITABLE );
		}
	}

} );

Cheers,
Pete Houston

Android XML Adventure – Parsing XML Data with XmlPullParser

October 16, 2011 10 comments

Article Series: Android XML Adventure

Author: Pete Houston (aka. `xjaphx`)

TABLE OF CONTENTS

  1. What is the “Thing” called XML?
  2. Parsing XML Data w/ SAXParser
  3. Parsing XML Data w/ DOMParser
  4. Parsing XML Data w/ XMLPullParser
  5. Create & Write XML Data
  6. Compare: XML Parsers
  7. Parsing XML using XPath
  8. Parsing HTML using HtmlCleaner
  9. Parsing HTML using JSoup
  10. Sample Project 1: RSS Parser – using SAXParser
  11. Sample Project 1: RSS Parser – using DOM Parser
  12. Sample Project 1: RSS Parser – using XMLPullParser
  13. Sample Project 2: HTML Parser – using HtmlCleaner
  14. Sample Project 2: HTML Parser – using JSoup
  15. Finalization on the “Thing” called XML!

=========================================

The last XML parser, I’d like to introduce to you today is `XmlPullParser`.

The concept is `XmlPullParser` is similar to the `SAXParser`; however, instead of using such callback methods for handling objects like `startDocument()`, `startElement()`, `endElement()` … in `SAXParser`, `XmlPullParser` prefers to order the process as event states.

There are five event states in `XmlPullParser`:

  1. START_DOCUMENT: the pointer of data is current at the beginning of XML document. It’s equivalent to `startDocument()` .
  2. END_DOCUMENT: already reach the end of XML document. It’s equivalent to `endDocument()`.
  3. START_TAG: at the beginning of a tag. It’s equivalent to `startElement()`.
  4. END_TAG: at the end of a tag. It’s equivalent to `endElement()`.
  5. TEXT: pointer is at the content text of a tag. It’s equivalent to `characters()`.

As you see, by using this, you can just parse whole XML document in a loop by jumping to each XML tag on every turn.

Let’s head to a simple example with `XmlPullParser`, still using the template XML and output from previous articles.

package pete.android.study.parser;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import pete.android.study.data.Study;

public class StudyParser {
	public static Study parse(InputStream is) {
		// create new Study object to hold data
		Study study = null;

		try {
			// get a new XmlPullParser object from Factory
			XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
			// set input source
			parser.setInput(is, null);
			// get event type
			int eventType = parser.getEventType();
			// process tag while not reaching the end of document
			while(eventType != XmlPullParser.END_DOCUMENT) {
				switch(eventType) {
					// at start of document: START_DOCUMENT
					case XmlPullParser.START_DOCUMENT:
						study = new Study();
						break;

					// at start of a tag: START_TAG
					case XmlPullParser.START_TAG:
						// get tag name
						String tagName = parser.getName();
						// if <study>, get attribute: 'id'
						if(tagName.equalsIgnoreCase(Study.STUDY)) {
							study.mId = Integer.parseInt(parser.getAttributeValue(null, Study.ID));
						}
						// if <content>
						else if(tagName.equalsIgnoreCase(Study.CONTENT)) {
							study.mContent = parser.nextText();
						}
						// if <topic>
						else if(tagName.equalsIgnoreCase(Study.TOPIC)) {
							study.mTopic = parser.nextText();
						}
						// if <author>
						else if(tagName.equalsIgnoreCase(Study.AUTHOR)) {
							study.mAuthor = parser.nextText();
						}
						// if <date>
						else if(tagName.equalsIgnoreCase(Study.DATE)) {
							study.mDate = parser.nextText();
						}
						break;
				}
				// jump to next event
				eventType = parser.next();
			}
		// exception stuffs
		} catch (XmlPullParserException e) {
			study = null;
		} catch (IOException e) {
			study = null;
		}

		// return Study object
		return study;
	}
}

– Use: `getEventType()` to get the current event state.

– To get attribute, use `getAttributeValue()`.

– To get text content of current tag, use `nextText()`. Why is it? It’s because if current pointer is at a tag name, `<content>`, then the next element is a tag text, which is the content inside between open-close tag; hence, the event END_TAG will be `</content>`.

– To move to next XML tag, use `next()`.
Well, hope you learn something. See you in next article!

Cheers,

Pete Houston