Android Tutorial #4.2: Passing custom attributes via XML resource files

In tutorial #4.1, I mentioned that we passed custom attributes for the text and image variables from the XML resource file to our custom class. This is a critical skill for performing true object-oriented programming and how to do it wasn’t obvious from Google’s Android API Demos.

Luckily I was pointed to the solution myself by an experienced Android programmer in Guatemala by the username of cadlg (thanks again!). If you want to see the official Google Android example though, look at Android’s APIDemos’ custom LabelView example.

So here we go. We’ll use the same code as Tutorial 4.1 to keep this simple.

Setting Up Your Custom Class’s XML Resource Files

We’ll only review the code for the TextOnlyButton as it’s identical in concept to the ImageOnlyButton.

First we’ll create a new file in /res/values called attrs.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<declare-styleable name=”TextOnlyButton”>

<attr name=”textColorNotFocused” format=”integer”/>
<attr name=”textColorFocused” format=”integer”/>

</declare-styleable>

</resources>

As you see, we first declared a ’styleable’ with the name of our custom Class. Two attributes were then added to contain the values of our focused & unfocused text colors. By default, attributes have values of String, but in our case, we needed integers to represent the resource id’s we’ll declare in our colors.xml file. You can also declare formats such as “boolean” & others if that suits the requirements of your own project.

Next, we declare values for these custom attributes in our layout’s XML file: tutorial4.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:pj=”http://schemas.android.com/apk/res/com.pocketjourney.tutorials”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:padding=”10px”>

<com.pocketjourney.view.TextOnlyButton

android:id=”@+id/text_only_button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”5px”
style=”?android:attr/buttonStyleSmall”
android:text=”Text Button”
pj:textColorNotFocused=”@drawable/white”
pj:textColorFocused=”@drawable/android_orange”/>

</LinearLayout>

Referring to our new attributes is actually a two step process. First we declared a new namespace (in our case called ‘pj’ as short for PocketJourney) in the parent layout of our custom class:

xmlns:pj=”http://schemas.android.com/apk/res/com.pocketjourney.tutorials”

Next we specified the values of our new attributes in the XML usage of our TextOnlyButton:

pj:textColorNotFocused=”@drawable/white”
pj:textColorFocused=”@drawable/android_orange”

Now you can see why we specified our format=”integer”. Our custom attributes point to the resource id’s of colors specified in our colors.xml file.

Retrieving Custom Attributes During Class Instantiation

Since our Activity has many constructors, we delegate the attribute parsing to an init() method to keep our code clean.

int notFocusedTextColor, focusedTextColor;

private void init(AttributeSet attrs) {

Resources.StyledAttributes a = getContext().obtainStyledAttributes(attrs,R.styleable.TextOnlyButton);
notFocusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorNotFocused, 0xFF000000);
focusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorFocused, 0xFF000000);

}

By now you’ve undoubtedly seen the AttributeSet that is always passed into an Activity. Well now you get to use it. First we obtain the StyledAttributes instance by requesting just the StyledAttributes for our custom Class. Next, we call the getColor() and pass two variables: the name of the attribute we want along with a default value in case the user did not specify one.

Take note of our styled attribute’s name as it’s a combination of our custom class’s name and the attribute we specified in the attrs.xml file (e.g. TextOnlyButton_textColorNotFocused).

And That’s It

You can now readily pass your own custom attributes and keep your View variables cleanly enclosed in your XML files. You can download the source to see for yourself. Just look at Tutorial #4.

Prior Tutorials

Tutorial 1: Transparent Panel (Linear Layout) On MapView (Google Map)
Tutorial 2: “Hit” testing on a View (MapView)
Tutorial 3: Custom Media Streaming with MediaPlayer
Tutorial 4.1: Image and Text-Only Buttons

10 Responses to “Android Tutorial #4.2: Passing custom attributes via XML resource files”


  1. 1 Hanjo July 4, 2008 at 4:29 am

    Seems to be, that the server is down where tutorial4.zip is located… Could you fix it?

    Thanks for the Tutorial

  2. 2 Biosopher July 4, 2008 at 3:29 pm

    The server’s back up now, Hanjo.

    We had someone probing our server by querying “phpmyadmin/main.php”. We don’t use phpmyadmin although we did use PHP. Still not sure why a simple call to “phpmyadmin/main.php” should crash the server…seemed to only result in an IOException on our Tomcat server.

    We’re back up now though…and have removed PHP from the site for now.

  3. 3 Hanjo July 5, 2008 at 12:10 am

    Hey,

    thanks for bringing the Server up again, now I had the chance to have a look at your tutorial files:
    But it is still not working for me: The text changes the color if I click on the button, but it does not change it back (even killing the programm and restarting it does not help), only after a restart of the emulator the color is reset. The ImageOnlyButton however does not change its image at all. It just stays the way it is. And that’s the real problem, which I not only have in my implementation, but also in the original tutorial files. I’m using the latest version of the sdk m5-rc15.

    Any ideas?

  4. 4 Biosopher July 7, 2008 at 7:04 pm

    Hi Hanjo,

    That is the expected behavior. Wish I could have told you otherwise, but tou need to add the additional button behavior if you want the button to change the image or such.

    Cheers,
    Anthony

  5. 5 siva August 9, 2008 at 4:52 am

    hi
    i am siva
    i have a problem ,can you please help me,
    how to pass an xml file as a parameter to class file in command prompt to execute a java class file

    like
    i tried this ” java classfilename inputfilename(.XML file) “

  6. 6 Bose Pandian February 4, 2009 at 11:52 pm

    Hello..
    Pocket Journey Team..
    Thankyou very much for this useful tutorial..
    Regards,
    Bose.C

  7. 7 Joel March 21, 2009 at 8:08 am

    Thanks for the tutorial. It has helped me as well.

  8. 8 Sushil April 9, 2009 at 10:13 pm

    Hi Hanjo,

    How can I use these customized attributes in styles.xml.

    I’m getting error “attribute not found”

    Thanks in advance.

  9. 9 Jiang August 10, 2009 at 11:29 pm

    Thank you for the tip. I was wondering how I was to use these attributes ;p

  10. 10 asad November 23, 2009 at 11:38 am

    how do i install the andriod over the air ?


Leave a Reply




a