..
We are now going to put into practice what has been said in the last part of the previous chapter.
As a starting point we can use the sample project we have already created or we can create a new one by following the same procedure shown in the first chapters of the guide.
Our view sample contains two objects of type TextView which will replace the initialization string in the file main.xml through source code. Let's see how.
First open the file and main.xml posizioniamoci in the window that shows the listing of the xml file. Now replace the code with the following:
<? 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: text = "text by default TextView1" android: id = "+ @ id/testo1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </ TextView> <TextView android: text = "text by default TextView2" android: id = "+ @ id/testo2" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </ TextView> </ LinearLayout>
As we can see we chose a LinearLayout (we will see later in the guide other types of layouts available) and we have declared two TextView. Through the string android: text we set the default value of the string and TextView android: id element we associate a unique id and name (Text1 and Text2) we will use the source code to make the necessary connections.
At this point the file main.xml does not require other configurations, and then we can save it and close it.
Now we open the source file. Java (if you use the old project file HelloWorld.java) and replace the contents with the following code:
MrWebMaster.android package;
android.app.Activity imports;
android.os.Bundle imports;
android.widget.TextView imports;
public class HelloWorld extends Activity {
private TextView textView1;
private TextView textView2;
/ ** Called When The activity is first created.
* /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
textView1 = (TextView) findViewById (R.id.testo1);
textView1.setText ("Text changed by code 1");
textView2 = (TextView) findViewById (R.id.testo2);
textView2.setText ("Text changed by code 2");
}
}
As we can see the class contains two attributes of type TextView and method onCreate invoke the same method on the superclass and the file layout as inidichiamo main.xml.
Without this, using the method findViewById, we associate the elements declared in main.xml with the attributes of the class.
With the method setText change the default value of the two TextView replacing it with the string passed as argument to the method.
Launching the application will be displayed on the screen of the device, the following window:

| |
Flash MX and ActionScript (Course)
Become a developer of Web sites from 29 €. |
| |
Java (Course)
OOP Programming in Java SUN. -15% Discount until 12/10/2011. |
| |
Visual Basic 6 (Course)
Make Desktop Applications with VB6. From 39 €. |