UIs the Java way

The same thing as we have just done with XML-Code can be achieved by hacking some lines of Java-Code.

Remember how we set our main.xml as the ContentView in the xml-example. We accomplished that by doing the following:

/* Make this application use * the main.xml-layout-file. */ this.setContentView(R.layout.main);

Activity.setContentView(...) also accepts a View as the parameter. We will use that to set a simple TextView as our ContentView.

package org.anddev.android.Hello Android;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class Hello Android extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

/* We want to view some very simple text,

* so we need a TextView associated with this

Activity. */

TextView tv = new TextView(this);

/* Put some text to the newly created TextView */

tv.setText("Hello Android - by: anddev.org \n"

+

"This is soooo simple =D ");

/* Tell our Activity to display that TextView

*/

this.setContentView(tv);

}

0 0

Post a comment