Every Android Application is composed of following files. In this post, we share How to Build and Run the Android Application.
- AndroidManifest.xml
- R.java and Resources
- Layouts (main.xml)
- Activities (Java Class File)
AndroidManifest.xml
The components and settings of an Android application are described in the AndroidManifest.xml file. For example all Activities and Services of the application must be declared in this file. It must also contain the required permissions for the application. For example if the application requires network access it must be specified here.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.org.net" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".WelcomeActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
To include new Activity use
<activity android:name=“com.org.net.DisplayMessageActivity” />
OR
<activity android:name=“com.org.net.DisplayMessageActivity” > </activity>
To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability of the component, a set of intents that the component is willing to receive. It, in effect, filters in intents of a desired type, while filtering out unwanted intents — but only unwanted implicit intents (those that don’t name a target class). An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component’s filters.
“@string/app_home” means app_home name is define in string,xml file and reference is stored in R.java file.(String.xml file is present in “res/values” folder.
R.java and Resources
R.java is resources file which is automatically generated by Eclipse IDE. Do not try to edit this file. The gen directory in an Android project contains generated values. R.java is a generated class which contains references to certain resources of the project. These resources must be defined in the res directory and can be XML files, icons or pictures. You can define values, menus, layouts or animations via XML files.
If you create a new resource, the corresponding reference is automatically created in R.java via the Eclipse ADT tools. These references are static integer values and define IDs for the resources. The Android system provides methods to access the corresponding resource via these IDs. For example to access a String with the R.string.yourString ID, you would use the getString(R.string.yourString)) method.
R.java is automatically created by the Eclipse development environment, manual changes are not necessary and will be overridden by the tooling.
package com.org.net; public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
Layout (main.xml)
This file will contain the layout of android screen. The user interface for Activities is defined with this layout file. The layout defines the included Views (widgets) and their properties. A layout can be defined via Java code or via XML. In the most cases, the layout is defined as an XML file.
XML based layouts are defined via a resource file in the /res/layout folder. This file specifies the ViewGroups, Views, their relationship and their attributes for this specific layout. If a View needs to be accessed via Java code, you have to give the View a unique ID via the android:id attribute. To assign a new ID to a View use @+id/yourvalue . The following shows an example in which a Button gets the button1 ID assigned.
Path of this file is Project > res > Layout.
<?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>
In your XML files, for example your layout files, you can refer to other resources via the @ sign. For example, if you want to refer to a color which is defined in a XML resource, you can refer to it via @color/your_id. Or if you defined a “hello” string in an XML resource, you could access it via @string/hello.
Activity (Java Class File)
This file contain application logic means which action will performed by your application. This class is subclass of Activity class. When your application is launched it will call it’s onCreate() method.
package com.org.net; import android.app.Activity; import android.os.Bundle; public class WelcomeActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
To run the applications simply click on Run Button
Keyboard shortcut is ctrl + F11. This will show the following screen and here you run your first android program.
Full android web application Tutorial:-
Introduction How to develope Android application
What are the Fundamental Units of Android Application
Step by Step Installation guid for android application developent
How to Build and Run the Android Application
Linear Layout in Android Application Development
Table Layout in Android Application
Relative Layout in Android Application
SQLite Database used in Android Application Development
Music Player Code for Android Application Development
How to make Phone call for Android Application with full code
Status Bar in Android Application Development with Full Code
Hi, just wanted to say i liked this article. it was practical. keep on posting.
Thanks 🙂