Android Document
How to create a new project?
Follows the following steps to create a new project in Android:
1. Open eclipse.
2. File → New → Android Project. You will get the following window.
Enter the project name.
Select “Create new project in work space”
Select the build target.
Enter “Application Name”, “Package Name”, “Create Activity” and “Min SDK
Version”.
Then click on the Finish Button.
A hello android program
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Hello 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 Android, Learning Android is a great fun.");
setContentView(tv);
}
}
public class Hello extends Activity {
int the above line we are extending the Hello class from Activity class. An Activity is the place
where a user can add the view.
Public void onCreate(...) is a call back function that must be overridden by the derived class of
class Activity.
TextView tv = new TextView(this);
tv.setText("Hello Android, Learning Android is a great fun.");
setContentView(tv);
In the above three lines, we are creating an object of TextView and set the text “Hello Android,
Learning Android is a great fun.” to the TextView. And in the last line we are setting the
Content View as a TextView.
How to run the Android Application?
From the eclipse menu, click the Run → Run. This results the following screen.
Declaring Layout
Your layout is the architecture for the user interface in an Activity. It defines the layout
structure and holds all the elements that appear to the user. You can declare your layout in two
ways:
1. Declare UI elements in XML. Android provides a straightforward XML vocabulary
that corresponds to the View classes and subclasses, such as those for widgets and layouts.
2. Instantiate layout elements at runtime. Your application can create View and
ViewGroup objects (and manipulate their properties) programmatically.
Write the XML
We can quickly design the layout using Android XML files:
For example, here is an XML layout that uses a vartical LinearLayout to hold a TextView and a
Button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
Load the XML Resource
You should load the layout resource from your application code, in your activity.onCreate()
callback implementation.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView.(R.layout.main_layout);
}