Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Saturday, November 7, 2009

Setting defined colors for your Android UI components

I don't think the Android SDK manual is helpful enough when it comes to setting the colors that you defined in the XML file (in my example, res -> values -> color.xml). There is actually a workaround instead of following rigidly the guidelines to use the setTextColor(int color) method.

I believe you must have read the guidelines how to set the color of TextView from this URL and this URL and do not really feel like constructing another XML file as mentioned in the manual.

I also believe that you must have used this approach to set the color from the Java code,
btnInstance.setTextColor(R.color.YOUR_DEFINED_COLOR);
simply because the setTextColor asks for an Integer as the parameter but you don't see the changes taking place.

SOLUTION: this is THE workaround to set the color of your UI components from the codes.
btnInstance.setTextColor(getResources().getColor(R.color.YOUR_DEFINED_COLOR));

Monday, October 5, 2009

Reading XML in Android platform

I find these XmlResourceParser and XmlPullParser classes useful to read XML files when we are dealing with Android platform. Below is the code sample I use in my Android project:Here is an example from the Android Developers website.

Detailed explanation of using XSL to scrape ISO country codes

I apologize if you couldn't understand how I managed to scrape the country codes from the ISO website as mentioned in my previous article. Here are the detailed steps to scrape the codes accordingly. You'll need 3 things to achieve the goal: manual editing (I know it's primitive otherwise you are more than welcomed to write your HTML tag stripper to recognize the elements in the web page), XML file and XSL file.

[1] Copy the table element:
- Copy the entire table element that contains the country names and codes

[2] Editing the XML file:
- Open an empty file
- Save it as "countries.xml"
- Add this line at the top of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="countries.xsl"?>
- Paste the table element you copied earlier
- Remove the table row that contains "Country names" and "ISO 3166-1-alpha-2 code"
- The screenshot is as below:[3] Editing the XSL file:
- Copy the XPath codes I demonstrated in this article
- Save it as "countries.xsl"

[4] You are all set!
- Here is the sample output translating the XML using XPath

Saturday, September 12, 2009

Scraping ISO 3166-1-alpha-2 country codes

I have written a simple XSL to scrape the ISO 2-letter country codes from the ISO website First I copied the table that contains the country names and codes and then I constructed the XSL. This is how the XSL looks like:UPDATE:
Please insert the breakline HTML element in the if block after the translate statement. Because without it, the country codes won't be displayed in separate lines. I have written another blog entry about scraping the codes. Please take a look here.

Thursday, May 28, 2009

Translating XML with XSL in Java

I do not consider the codes in this blog entry as intensive as other entries. This is just merely an example to show you how to translate a XML document with a XSL stylesheet in a Java class. It could be a very handy tool for some Java developers out there that want to translate a given XML content into another completely different XML content on the fly.
Do not forget to set the encoding of your Java codes to UTF-8 if you are manipulating non-English XML content - for example, Traditional Chinese, Simplified Chinese, Russian, Korean or Japanese - if you would want to see the output in your Eclipse IDE, which is my current IDE to write Java codes.

The usefulness of this approach is that you do not need to include a reference of a XSL file in the XML, say for example,

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="myXMLstylesheet.xsl"?>
<catalog/>

Hope you have fun trying this out! :)