Recap
In this article we will learn how to create a Android application. I will tell you about eclipse development window and some basic about the android application.Till now we have create Android development environment and create the Android emulator successfully,
Start with new Android Project
1. Start Eclipse.
2. Select File New Project. ‘New project’ window pop up will be displayed If you don’t see the Android folder, that means your eclipse has not ADT plugin, you need to install first see the ‘’
3. Expand the Android folder, select Android Project and click Next.
4. ‘Create Android project‘ window pop up will be display.Enter a project name and click Next.
5. Select a build target and click Next.
6. Android Application info window will be display, enter the your application information in the required fields.
Application name is the friendly name for your application and the Project name is the name of your project.The Package name specifies its package and Activity name is the name of the class that is your initial Activity
7. Click Finish.
You have done friends…
Your Android project is ready to work. you can see in the Package Explorer on the left of the Eclipse.
It is same as Solution Explorer in .Net visual studio.
Package Explorer enables us to see your application file system. you can see the following folders that contains folders or files.
Setting a Run Configurations
Run Configurations specify the runtime options for running applications. We can set the input and output settings, Android emulator options etc.
The following are the steps for Run Configurations:
1. Select Run–Open Run Configurations
2. Right-click Android Application on the project type list, and select New.
3. Enter a name for the run configuration. You can create multiple configurations for each project.
4. from the left panel – you select the project and Activity from the first (Android) tab lets that you want to start when you run (or debug) the application.
5. Use the Target tab to configure the Android emulator.
6. Set any additional properties in the Common tab.
7. Click Apply, and your Run configuration will be saved.
Write layout in XML
Now we can create a layout of the application same as create a windows form in .net or in the same way you create web pages in HTML. you can declare the layout in main.xml that is located in res/layout directory in package explorer.
it looks like:
-
<?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:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/hello" />
-
</LinearLayout>
Each layout file must contain exactly one root element. In the above main.xml there is one root element linearlayout that contains textview element. Basically linearlayout is the object of the View or ViewGroup. we will describe view and viewgroup later. Linearlayout arranges its children in a single column or a single row. The direction of the row can be set by calling setOrientation attribute. And android:layout_width, android:layout_height attributes specifies the basic width and height of the view respectively.
fill_parent is the constant value (1 (0xffffffff) ) that means the view wants to be as big as its parent.
Our main.xml contain a child element Textview that we can use to display text to the user and also optionally allows them to edit it. we are using layout_width and layout_height to set the width and height.
After you’ve declared your layout in XML, save the file with the .xml extension, in your Android project’s res/layout/ directory.
Load the XML Resource
And Now open the MyProjectActivity.java file, located inside src/my.authorcode direcotory. It should look like this:
-
package my.authorcode;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
-
public class MyProjectActivity extends Activity {
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
}
-
}
After completing work on XML layout file. we can load this layout XML file from our application code.
the above java class is based on the Activity class and we are using setContentView() method in onCreate() method that is called by Android system when our Activity starts.see the following line :
Suppose if we are using another XML file named newmain.xml for layout then you can load declared layout as
-
setContentView(R.layout.newmain);
Run the Application
you can run your created Android application by following steps:
1. Select Run from Run menu.
2. Select “Android Application”.
The Your Eclipse plugin automatically creates a new run configuration for your Android project and then launches the Android Emulator. you can also set the run configuration manually.
your Android emulator might take several minutes to show the output…
And the following are the steps if you want to run your android application in the real device such as your mobile.
1. Connect your device to your machine. (if you are working on Windows OS, you need to install USB drivers, if you have not then you need to install the appropriate USB driver for your device.)
2. Enable USB debugging in the device Settings.
3. Run the application from Run menu.
Eclipse installs the app on your connected device and starts it.
Thanks for reading, check out AuthorCode for more Authorcode contents