Sunday, March 4, 2012

Hello, Android

In this tutorial you will write your first Android application entitled Hello, World.If you aren't sure how to create an application check out this tutorial.
After you have created your project open the .java file located in the src folder. To create the TextView that displays "Hello, World" you could drag and drop from the Graphical layout but in this tutorial we will create it in the .java file.  A TextView is simply text that cannot be edited by the user.

The code we are about to write will be placed beneath the automatically generated text, like this:

package com.test.chris; 

import android.app.Activity;
import android.os.Bundle;

import android.widget.TextView;

public class HelloAndroidActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);


    TextView tv = new TextView(this);
    tv.setText("Hello, world");
    setContentView(tv);
  }
}
You will probably notice that TextView is underlined in red, meaning there is an error. When you hover over the location of the errors, it will pull up a list of quick fixes. In this case select "Import 'TextView'(android.widget)" then in your code it will say import android.widget.TextView;. We gave the TextView a name, tv, and specified what the text will be. By setting the view to tv, it makes the TextView visible. Save and run your project to see the results. You have successfully created your first Android application.

No comments:

Post a Comment