Posts Tagged ‘XML Layout’

Simple Example of Making your Android XML talk to your Java Class

Posted on 2008.12.30 by Tom

Many GUI based frameworks are going with using XML or XML based languages to build layouts and for good reason. Besides the obvious benefit of making it easier on the Interface Builders and the people who have to program them it also makes it a bit easier on the developer, once you get the hang of it.

The biggest problem is understanding how to get things back and forth from your layout files and your logic code. My example will be based on Android and a basic project created with Activity Builder. So you have a simple project now and for this tutorial we will only need to look at two of the files, main.xml and your main Java class for the program.

First lets look at the main.xml layout file. We will leave it just as it is with a TextView in it and just add a few lines so we will be able to work with it in our Java class. We should have something that looks like this right now.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

This is all fine for what we need but lets go ahead and give our TextView an ID so we will be able to get it in our Class files. So on the first line under the TextView container let add this.

	android:id="@+id/sender"

Also so we can demonstrate that it actually worked we need one more TextView with an id and an empty text value. So lets also add this below our first TextView container


<TextView
android:id="@+id/receiver"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>

Ok now we have added an ID to that item, actually we haven’t but Android has. When you build your project the compiler will add an ID to the R.java file to make sure we can access that object and it’s properties from outside the XML file.

So far we have a layout built with two TextViews. One has the ID of receiver and the other has the ID of sender. Now all we have to do is build the code to access each ID. But first lets run our program so we can see that one item will have text and the other will not. Again just more proof that our code is actually doing something.

In our Java code we will need to do a few things, I am keeping this simple so I am not going to build in EventListeners, instead when the program loads it will get the text of the sender and populate the text of receiver with the same text. So open your main Java class and lets add a few lines of code.

Your Java class probably looks like this right now

	package org.example.hello;
 
	import android.app.Activity;
	import android.os.Bundle;
 
	public class Hello extends Activity {
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	    }
	}

We can leave all that code in there and just get and set what we need. First of all we need to add some packages so we can get our work done. Add in the following packages:

  • android.widget.TextView

Well that was not a lot of imports especially for Java but anyway below setContentView we want to get the TextView Object so lets add the following two lines of code to get both our sender and our receiver.

	TextView receiver = (TextView)findViewById(R.id.receiver);
	TextView sender = (TextView)findViewById(R.id.sender);

See how we just use findViewById and pass it the id that we want. R is the way Android works for you, just like when getting the layout files when we type R.layout.main you can see how it classifies them based on the directory that are in and then the node from the xml file. So now we have our two TextView objects. So we want to take the text from the send and append a few words to it and then use it to set the receiver. It is easy, just add the following line below what you already have

	receiver.setText(sender.getText() + ": This text is set dynamically");

So all we did here was some getting and setting, well and one concatenation of the text “: This text is set dynamically” Android is pretty good for following standards so you can count on common sense sometimes. Like if you were unsure of how to get the text from a layout object/widget then guessing getText() would not be a bad idea. Here is the screenshot of what it should look like when you build and run

This has been a really simple example but if you have used some of the other frameworks that are using XML/XML Based code for layouts then I am sure you can appreciate this. I had trouble with Flex and figuring out how they do a lot of things because the Interface Builder was nice and I spent too much time messing with it instead of learning how to do it with code and then got stuck trying to figure out how to do simple things without the Interface Builder.

Android has some nice phones that are coming out pretty soon, they have the backing of all the carriers and a good amount of the mobile phone distributors are interested in the platform so I think it has huge potential to grow. On top of that there is talk of many other applications for the Android OS such as in Car PC’s, MP3 players, Educational computers, Replacement for NetBooks(Even though NetBoks are still considered a trend). So it is a technology worth knowing something about

Bookmark and Share