RSS

Hello World application

03 Jan

Hello World

  • Writing the Hello world message
  • Changing the Hello world message
  • Styling the Hello world message

Make a new Android project

New Android Project

Lets dive in and make a new project in Eclipse.

  • Start eclipse and goto: File > New > Other ( Ctrl+N )
  • Select Android project and click Next
  • Fill in a project name: HelloWorld
  • Choose a Build target: Android 2.2
  • Scroll down and fill in the Application name: Hello world
  • Fill in a package name: com.pxr.hello_world
  • Name the Activity: MainActivity
  • Click Finish

Here is a description of each field:
(From developers.android.com)

Project Name
This is the Eclipse Project name the name of the directory that will contain the project files.
Application Name
This is the human-readable title for your application the name that will appear on the Android device.
Package Name
This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.Your package name must be unique across all packages installed on the Android system; for this reason, it’s important to use a standard domain-style package for your applications. The example above uses the “com.example” namespace, which is a namespace reserved for example documentation when you develop your own applications, you should use a namespace that’s appropriate to your organization or entity.
Create Activity
This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android’s Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn’t need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
Min SDK Version
This value specifies the minimum API Level required by your application. For more information, seeAndroid API Levels.

Android project file structure

project file structure

Now that you made your project you will see that eclipse made a new project folder in the Package explorer. THe picture on the right shows my new Hello world project . Here is a short explanation of the most important files/folders in the project.

src: This folder holds all the tutorial source files. As you can see in the image, Eclipse made 1 .java file. In this file is some basic code that is used to start up the Android app.

gen: All generated Java files reside here. When you open it you will see 1 file: R.java. the R.java file is an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you’ve included in your project.

assetsDon’t really know what to put in here :) Maby leave me a comment ?

resYou can find 3 very important things here.

  1. All the  layout files ( main.xml )
  2. All resources ( thinks images most of the time icon.png)
  3. All string value’s / color values e.t.c ( strings.xml )

We will dive deeper into these files later on.

AndroidManifest.xml: This is the place where you specify the permission your app needs. We will not alter this file in this tutorial.

Writing text to the screen

Android Tutorial: Hello WorldAlready working ?

So we made a new project for this tutorial and we run by the file structure. When you run your project (Ctrl+F11) you will already see the Hello world message!! ( thanks to Eclipse ). But we want to understand why and how the app draws that onto the screen. Open up MainActivity.java.

MainActivity.java

package com.pxr.hello_world;

import android.app.Activity;

import android.os.Bundle;

public class MainActivity extends Activity {

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

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}


An Activity is a single application entity that is used to perform actions. An application may have many separate activities, but the user interacts with them one at a time. The onCreate() method will be called by the Android system when your Activity starts  it is where you should perform all initialization and UI setup.

The most interesting line of code in this code exerpt is setContentView(R.layout.main);. With this line you tell the application to put build the screen with this file: res>layout>main.xml. Lets open that file.

Another code for using java

package com.javapapers.android;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class HelloWorld extends Activity {

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

Textview tv;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv=(Textview)findviewbyid(R.id.textview1)

tv.setText(“Hello World!”);

}}


Main.xml

This is the main layout for out tutorial project.


<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;

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 layout file exists of 3 things:

  1. The xml declaration
  2. A LinearLayout
  3. A TextView

We want to take a closer look to the TextView. the android:text property contains a reference to another file. @stringis a reference to values>strings.xml. So i think we wanna open up that one :) .

Strings.xml

Our strings.xml file contains 2 string values.


<?xml version=”1.0″ encoding=”utf-8″?>

<resources>

<string name=”hello”>Hello World, MainActivity!</string>

<string name=”app_name”>Hello world</string></resources>

The first hello is the text that is displayed on the screen by calling @string/hello in the main.xml file. The other (app_name) is user for displaying the name of the app!

Well, that wasn’t exactly hard. Still strugling ? Just leave a comment down below.

Changing the Hello world message

Android development is easy!

Just by changing the text in strings.xml you can change the message that is printed on the screen. Lets change both values.

  • hello: Hey it worked =)
  • app_name: My first Android tutorial!

Result will be like the image to the right. You can change this text into whatever you like of course :)

Styling the Hello world message

Just one final adjustment. The text doesn’t look nice!  Lets change the layout like this

  • Put the text in the center of the screen.
  • Change the font
  • Change the color

Layout is changed in the Main.xml file. Open it up if you don’t have it open. After opening select the TextView element and replace it with the following:


<TextView android:layout_width=”fill_parent”android:layout_height=”fill_parent”

android:gravity=”center”

android:textSize=”25dip”

android:textColor=”@color/Gold”

android:text=”@string/hello”/>


After that make a new file in the folder values ( rightclick the folder – file ). Name the file: color.xml. We can use this xml file to store our color values. Paste the code below into the color.xml file:

<?xml version=”1.0″ encoding=”utf-8″?>

<resources>

<color name=”Gold”>#ffd700</color></resources>

After that: save and press Ctrl+F11 to run the project again!

veer well done!

 
Leave a comment

Posted by on January 3, 2012 in Practical

 

Leave a comment