Android code , Android Phone, Android Developers

Android code , Android Phone, Android Developers and New Google Android Technologies

Archive for April 12th, 2011

Hello World! Complete

Posted by admin April - 12 - 2011 - Tuesday ADD COMMENTS

Your next step is to import the package TextView from android.widget. This will
give you access to the TextView and let you create your own instance of it. Place this
code near the top of your current HelloWorldText.java file, where the existing import
statements are
import android.widget.TextView;
Now, create an instance of TextView. By creating the TextView instance, you can use
it to display text to the screen without directly modifying main.xml. Place the following
code after the onCreate( ) statement is fired:
TextView HelloWorldTextView = new TextView(this);

NOTE
TextView takes a handle to the current context as an argument. Pass this to the
TextView to associate it with the current context. If you follow the hierarchy
through the SDK, HelloWorldText extends Activity, which extends
ApplicationContext, which in turn extends Context. This is how you can
pass this to your TextView.
The preceding line creates an instance of TextView named HelloWorldTextView
and then instantiates HelloWorldTextView, by setting it to a new TextView. The new
TextView is passed the context of this to be fully instantiated.
Now that the TextView is defined, you can add your text to it. The following line of
code assigns the text “Hello World!” to the TextView:
HelloWorldTextView.setText(“Hello World!”);
This line lets you set the text of your TextView. setText( ) lets you assign a string to the
TextView.

Your TextView has been created and now contains the message that you want to
display. However, simply passing “Hello World!” to the TextView does not display
anything to the screen. As discussed previously, you need to set the ContentView to

 

display something to the screen. You have to use the following code to set TextView
to the context and display it to the screen:
setContentView(HelloWorldTextView);
Examining this line, you can see that you pass to setContentView your TextView. The
preceding three lines of code are what it takes to make your Hello World! application.
You created a TextView, assigned your text to it, and set it to the screen. All things
considered, this is not very complicated at all.
The full contents of your HelloWorldText.java file should look like the following:
package android_programmers_guide.HelloWorldText;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldText extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/**Hello World JFD */
/**BEGIN           */
/**Create TextView */
TextView HelloWorldTextView = new TextView(this);
/**Set text to Hello World */
HelloWorldTextView.setText(“Hello World!”);

/**Set ContentView to TextView */
setContentView(HelloWorldTextView);
/**END             */
}
}
Now compile and run your new Hello World! application in the Android Emulator.
Choose Run | Run or press CTRL-F11 to launch the application in the Android Emulator. The following illustration depicts the results of your Hello World! application.

You have just created your first full Android Activity. This small project demonstrated
a fairly common execution of a Hello World! application. You set a TextView to the
Activity’s ContentView and displayed the “Hello World!” message to a cell phone screen
in the Android Emulator. The following section looks at a slightly different way of
implementing Hello World!, using an image.